WPRestClient is a PHP library for seamless interaction with WordPress sites via the WordPress RESTful API, simplifying authentication, data retrieval, and content management tasks
- Connect to WordPress sites and authenticate using REST API authentication methods.
- Retrieve posts, pages, custom post types, categories, tags and other WordPress entities.
- Create, update and delete posts and pages.
- Perform advanced queries and filter responses using the power of the WordPress RESTful API.
- Use the APIClient for low level calls
- Use Repositories and gain WordPress entity objects
- Configurable API Prefix
- Create custom entities or extend the existing ones
- Create custom repositories for any endpoint
PHP 7.4+
You can install WPRestClient via Composer. Run the following command in your project directory:
composer require angelxmoreno/wprestclient
-
Create a new instance of the WPRestClient:
use WPRestClient\Core\ApiClient; $client = new ApiClient('https://example.com');
-
Retrieve a raw array of posts via the client:
$posts = $client->sendRequest('get','/posts'); foreach ($posts as $post) { echo $post['title']['rendered']; }
-
Retrieve an array
PostEntity
objects via thePostsRepository
:use WPRestClient\Repository\PostsRepositor; PostsRepository::setApiClient($client); $posts = PostsRepository::fetch(); foreach ($posts as $post) { echo $post->getTitle(); }
-
Create a new Page via the
PagesRepository
using theRepositoryRegistry
: By creating an instance ofRepositoryRegistry
, you have access to all the registered repositories with theApiClient
already set.use WPRestClient\Repository\PagesRepository; use WPRestClient\Core\RepositoryRegistry; use WPRestClient\Entity\PageEntity; $registry = new RepositoryRegistry($client); $page = new PageEntity(['title' => 'A New Page']); $registry->pages()->create($page);
-
Delete a post via the
PostsRepository
using theRepositoryRegistry
:use WPRestClient\Repository\PagesRepository; use WPRestClient\Core\RepositoryRegistry; use WPRestClient\Entity\PageEntity; $registry = new RepositoryRegistry($client); $post = $registry->posts()->get(123); $registry->posts()->delete($post);
For more detailed usage examples and available methods, please refer to the documentation.
Contributions are welcome! If you find a bug, have suggestions for improvements, or would like to add new features, please submit an issue or a pull request. Make sure to follow our contribution guidelines
WPRestClient is open-source software licensed under the MIT license
For any questions or inquiries, please contact WPRestClient@gmail.com
For bugs and feature requests, please use the issues section of this repository.