Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

flutter wordpress rest

this library uses wordpress rest api v2 to provide a way for your application to interact with your wordpress website.

requirements

for authentication and usage of administrator level rest apis, you need to use either of the two popular authentication plugins in your wordpress site:

  1. application passwords
  2. jwt authentication for wp rest api (recommended)

getting started with wordpress rest

1. import library

https://pub.dartlang.org/packages/flutter_wordpress

import 'package:flutter_wordpress/flutter_wordpress.dart' as wp;

2. instantiate wordpress class

wp.wordpress wordpress;

// adminname and adminkey is needed only for admin level apis
wordpress = wp.wordpress(
  baseurl: 'http://localhost',
  authenticator: wp.wordpressauthenticator.jwt,
  adminname: '', 
  adminkey: '',
);

3. authenticate user

future<wp.user> response = wordpress.authenticateuser(
  username: 'chiefeditor',
  password: '[email protected]',
);

response.then((user) {
  createpost(user);
}).catcherror((err) {
  print('failed to fetch user: $err');
});

4. fetch posts

future<list<wp.post>> posts = wordpress.fetchposts(
  params: wp.paramspostlist(
    context: wp.wordpresscontext.view,
    pagenum: 1,
    perpage: 20,
    order: wp.order.desc,
    orderby: wp.postsorderby.date,
  ),
  fetchauthor: true,
  fetchfeaturedmedia: true,
  fetchcomments: true,
);

5. fetch users

future<list<wp.user>> users = wordpress.fetchusers(
  params: wp.paramsuserlist(
    context: wp.wordpresscontext.view,
    pagenum: 1,
    perpage: 30,
    order: wp.order.asc,
    orderby: wp.usersorderby.name,
    role: wp.userrole.subscriber,
  ),
);

6. fetch comments

future<list<wp.comment>> comments = wordpress.fetchcomments(
  params: wp.paramscommentlist(
    context: wp.wordpresscontext.view,
    pagenum: 1,
    perpage: 30,
    includepostids: [1],
  ),
);

7. create post

void createpost(wp.user user) {
  final post = wordpress.createpost(
    post: new wp.post(
      title: 'first post as a chief editor',
      content: 'blah! blah! blah!',
      excerpt: 'discussion about blah!',
      author: user.id,
      commentstatus: wp.postcommentstatus.open,
      pingstatus: wp.postpingstatus.closed,
      status: wp.postpagestatus.publish,
      format: wp.postformat.standard,
      sticky: true,
    ),
  );

  post.then((p) {
    print('post created successfully with id: ${p.id}');
    postcomment(user, p);
  }).catcherror((err) {
    print('failed to create post: $err');
  });
}

8. post comment

void postcomment(wp.user user, wp.post post) {
  final comment = wordpress.createcomment(
    comment: new wp.comment(
      author: user.id,
      post: post.id,
      content: "first!",
      parent: 0,
    ),
  );

  comment.then((c) {
    print('comment successfully posted with id: ${c.id}');
  }).catcherror((err) {
    print('failed to comment: $err');
  });
}

future work

  1. implementing oauth 2.0 authentication.

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.