laravel-wordpress maintained by jeffersongoncalves

Laravel WordPress
WordPress REST API integration for Laravel. A thin wrapper around the WordPress REST API covering posts, pages, media, categories, tags, comments and users, built on Laravel's HTTP client.
Installation
You can install the package via composer:
composer require jeffersongoncalves/laravel-wordpress
Publish the config file with:
php artisan vendor:publish --tag="laravel-wordpress-config"
This is the contents of the published config file:
return [
'base_url' => env('WORDPRESS_BASE_URL'),
'username' => env('WORDPRESS_USERNAME'),
'application_password' => env('WORDPRESS_APPLICATION_PASSWORD'),
'namespace' => env('WORDPRESS_NAMESPACE', 'wp-json/wp/v2'),
];
Generate an Application Password in WordPress under Users > Your Profile > Application Passwords, then add your credentials to .env:
WORDPRESS_BASE_URL=https://example.com
WORDPRESS_USERNAME=your-wordpress-username
WORDPRESS_APPLICATION_PASSWORD=xxxx xxxx xxxx xxxx xxxx xxxx
Requests are authenticated with HTTP Basic (Authorization: Basic base64(username:application_password)), so the site must be served over HTTPS.
Usage
You can use the Wordpress facade, or inject Jeffersongoncalves\Wordpress\Wordpress wherever you need it.
Posts
use Jeffersongoncalves\Wordpress\Facades\Wordpress;
Wordpress::listPosts(['per_page' => 10, 'status' => 'publish']);
Wordpress::getPost(123);
Wordpress::createPost([
'title' => 'Post Title',
'content' => '<p>Post content here</p>',
'status' => 'draft',
'categories' => [1],
'tags' => [5, 6],
]);
Wordpress::updatePost(123, ['title' => 'Updated Title', 'status' => 'publish']);
Wordpress::deletePost(123); // moves to trash
Wordpress::deletePost(123, force: true); // deletes permanently
Post statuses: publish, draft, pending, private, future, trash.
Pages
Wordpress::listPages(['per_page' => 20]);
Wordpress::getPage(9);
Wordpress::createPage(['title' => 'About', 'content' => '<p>Hello</p>', 'status' => 'publish']);
Wordpress::updatePage(9, ['title' => 'About us']);
Wordpress::deletePage(9);
Media
Wordpress::listMedia(['per_page' => 20]);
Wordpress::getMedia(42);
Wordpress::uploadMedia(
filename: 'image.jpg',
contents: file_get_contents('/path/to/image.jpg'),
mimeType: 'image/jpeg',
attributes: ['post' => 123],
);
Wordpress::updateMedia(42, ['alt_text' => 'A description']);
Wordpress::deleteMedia(42);
Categories and tags
Wordpress::listCategories();
Wordpress::getCategory(1);
Wordpress::createCategory('Category Name', 'category-name');
Wordpress::updateCategory(1, ['description' => 'Updated']);
Wordpress::deleteCategory(1);
Wordpress::listTags();
Wordpress::getTag(5);
Wordpress::createTag('Tag Name', 'tag-name');
Wordpress::updateTag(5, ['description' => 'Updated']);
Wordpress::deleteTag(5);
Comments
Wordpress::listComments(['post' => 123]);
Wordpress::getComment(7);
Wordpress::createComment(['post' => 123, 'content' => 'Nice post']);
Wordpress::updateComment(7, ['status' => 'approved']);
Wordpress::deleteComment(7);
Users
Wordpress::listUsers(['per_page' => 10]);
Wordpress::getUser(1);
Wordpress::currentUser();
Every method returns an Illuminate\Http\Client\Response, so you can use ->json(), ->successful(), ->status(), etc. as usual. $filters and $attributes arrays are passed straight through to the REST API, so any parameter documented by WordPress is supported.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.