@@ -5,7 +5,7 @@ This library uses [WordPress REST API V2](https://developer.wordpress.org/rest-a
55## Requirements
66For authentication and usage of administrator level REST APIs, you need to use either of the two popular authentication plugins in your WordPress site:
771 . [ Application Passwords] ( https://wordpress.org/plugins/application-passwords/ )
8- 2 . [ JWT Authentication for WP REST API] ( https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/ )
8+ 2 . [ JWT Authentication for WP REST API] ( https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/ ) < strong >(recommended)</ strong >
99
1010## Getting Started
1111
@@ -22,68 +22,114 @@ wp.WordPress wordPress;
2222
2323// adminName and adminKey is needed only for admin level APIs
2424wordPress = wp.WordPress(
25- baseUrl: 'http://localhost',
26- authenticator: wp.WordPressAuthenticator.ApplicationPasswords ,
27- adminName: '',
28- adminKey: '',
25+ baseUrl: 'http://localhost',
26+ authenticator: wp.WordPressAuthenticator.JWT ,
27+ adminName: '',
28+ adminKey: '',
2929);
3030```
3131
3232### 3. Authenticate User
3333
3434``` dart
3535Future<wp.User> response = wordPress.authenticateUser(
36- username: 'username ',
37- password: 'password ',
38- );
36+ username: 'ChiefEditor ',
37+ password: 'chiefeditor@123 ',
38+ );
3939
4040response.then((user) {
41- print (user.toString() );
41+ createPost (user);
4242}).catchError((err) {
43- print(err.toString() );
43+ print('Failed to fetch user: $ err' );
4444});
4545```
4646
4747### 4. Fetch Posts
4848
4949``` dart
5050Future<List<wp.Post>> posts = wordPress.fetchPosts(
51- params: wp.ParamsPostList(
52- context: wp.WordPressContext.view,
53- pageNum: 1,
54- perPage: 20,
55- order: wp.Order.desc,
56- orderBy: wp.PostsOrderBy.date,
57- ),
58- );
51+ params: wp.ParamsPostList(
52+ context: wp.WordPressContext.view,
53+ pageNum: 1,
54+ perPage: 20,
55+ order: wp.Order.desc,
56+ orderBy: wp.PostsOrderBy.date,
57+ ),
58+ );
5959```
6060
6161### 5. Fetch Users
6262
6363``` dart
6464Future<List<wp.User>> users = wordPress.fetchUsers(
65- params: wp.ParamsUserList(
66- context: wp.WordPressContext.view,
67- pageNum: 1,
68- perPage: 30,
69- order: wp.Order.asc,
70- orderBy: wp.UsersOrderBy.name,
71- role: wp.UserRole.subscriber,
72- ),
73- );
65+ params: wp.ParamsUserList(
66+ context: wp.WordPressContext.view,
67+ pageNum: 1,
68+ perPage: 30,
69+ order: wp.Order.asc,
70+ orderBy: wp.UsersOrderBy.name,
71+ role: wp.UserRole.subscriber,
72+ ),
73+ );
7474```
7575
7676### 6. Fetch Comments
7777
7878``` dart
7979Future<List<wp.Comment>> comments = wordPress.fetchComments(
80- params: wp.ParamsCommentList(
81- context: wp.WordPressContext.view,
82- pageNum: 1,
83- perPage: 30,
84- includePostIDs: [1],
85- ),
86- );
80+ params: wp.ParamsCommentList(
81+ context: wp.WordPressContext.view,
82+ pageNum: 1,
83+ perPage: 30,
84+ includePostIDs: [1],
85+ ),
86+ );
87+ ```
88+ ### 7. Create Post
89+
90+ ``` dart
91+ void createPost(wp.User user) {
92+ final post = wordPress.createPost(
93+ post: new wp.Post(
94+ title: 'First post as a Chief Editor',
95+ content: 'Blah! blah! blah!',
96+ excerpt: 'Discussion about blah!',
97+ author: user.id,
98+ commentStatus: wp.PostCommentStatus.open,
99+ pingStatus: wp.PostPingStatus.closed,
100+ status: wp.PostPageStatus.publish,
101+ format: wp.PostFormat.standard,
102+ sticky: true,
103+ ),
104+ );
105+
106+ post.then((p) {
107+ print('Post created successfully with ID: ${p.id}');
108+ postComment(user, p);
109+ }).catchError((err) {
110+ print('Failed to create post: $err');
111+ });
112+ }
113+ ```
114+ ### 8. Post Comment
115+
116+ ``` dart
117+ void postComment(wp.User user, wp.Post post) {
118+ final comment = wordPress.createComment(
119+ comment: new wp.Comment(
120+ author: user.id,
121+ post: post.id,
122+ content: "First!",
123+ parent: 0,
124+ ),
125+ );
126+
127+ comment.then((c) {
128+ print('Comment successfully posted with ID: ${c.id}');
129+ }).catchError((err) {
130+ print('Failed to comment: $err');
131+ });
132+ }
87133```
88134
89135## Future Work
0 commit comments