This tutorial demonstrates using the wp-api-angular
library to connect Angular 2 applications with the WordPress REST API. The library supports major WP resources (users, posts, comments, media, taxonomies). We'll build features showcasing its ease of use: JWT authentication, user and post listing, and post CRUD (Create, Read, Update, Delete) operations. The complete source code is on GitHub. While this tutorial uses Angular 5, the concepts apply to Angular 2 .
Key Concepts:
wp-api-angular
.Setting Up:
This tutorial uses a self-hosted WordPress instance. Enable permalinks (see the WordPress Codex for instructions; for Nginx, add try_files $uri $uri/ /index.php?$args;
to nginx.conf
). Install the JWT Authentication plugin for secure API access.
Angular Application Setup:
ng new wp-api
cd wp-api && npm install wp-api-angular --save
src/app/app.module.ts
:import { Http } from '@angular/http'; import { HttpClientModule, HttpClient } from '@angular/common/http'; import { WpApiModule, WpApiLoader, WpApiStaticLoader } from 'wp-api-angular'; @NgModule({ // ... imports: [ BrowserModule, FormsModule, HttpClientModule, WpApiModule.forRoot({ provide: WpApiLoader, useFactory: WpApiLoaderFactory, deps: [Http] }) ], // ... }) export function WpApiLoaderFactory(http: Http) { return new WpApiStaticLoader(http, 'http://YOUR_DOMAIN_HERE/wp-json/wp/v2/', ''); }
Replace YOUR_DOMAIN_HERE
with your WordPress domain. Also, add necessary imports to app.component.ts
(including NgForm
, HttpClientModule
, Headers
).
Authentication (JWT):
token
variable to app.component.ts
.authentication
component (ng generate component authentication
).authentication.component.ts
, create a user
object and an auth()
method:auth() { this.http.post('http://YOUR_DOMAIN/wp-json/jwt-auth/v1/token', { username: this.user.login, password: this.user.password }).subscribe(data => { if (data['token']) { this.token = data['token']; this.tokenChange.emit(this.token); } }); }
authentication.component.html
.Listing Users:
user-list
component (ng generate component user-list
).user-list.component.ts
, inject WpApiUsers
and use getList()
to fetch users.Working with Posts (CRUD):
post-new
component. Use WpApiPosts.create()
with JWT authorization headers.post-list
component. Use WpApiPosts.getList()
to fetch posts.post-list
. Use WpApiPosts.delete()
with JWT authorization headers.post-edit
component. Use WpApiPosts.update()
with JWT authorization headers.Conclusion:
This tutorial provides a foundation for integrating Angular with the WordPress REST API. The wp-api-angular
library simplifies this process, allowing for efficient management of WordPress content within your Angular applications. Remember to handle authentication securely and manage asynchronous operations effectively. The provided code snippets and explanations should help you build more complex interactions with the WordPress API.
The above is the detailed content of Connecting Angular and the WordPress API with wp-api-angular. For more information, please follow other related articles on the PHP Chinese website!