Git Product home page Git Product logo

flutter_wordpress's Introduction

Flutter Wordpress

This library uses WordPress REST API V2 to provide a way for your application to interact with your WordPress website.

Screenshots

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

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: 'chiefeditor@123',
);

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.

flutter_wordpress's People

Contributors

sachinganesh avatar surajshettigar avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.