Require this package in your composer.json
and update composer.
composer require madeitbelgium/wordpress-php-sdk
After updating composer, add the ServiceProvider to the providers array in config/app.php
MadeITBelgium\WordPress\WordPressServiceProvider::class,
You can use the facade for shorter code. Add this to your aliases:
'WP' => MadeITBelgium\WordPress\WordPressFacade::class,
Add the config files
php artisan vendor:publish --provider="MadeITBelgium\WordPress\WordPressServiceProvider"
To use authenticated requests you have to sign in to the WordPress website to generate a JWT token. WordPress doesn't support this by default, you have to install a JWT compatible plugin. This package is tested with: https://nl.wordpress.org/plugins/jwt-authentication-for-wp-rest-api/
use MadeITBelgium\WordPress\WordPressFacade as WordPress;
$token = WordPress::login($request->get('email'), $request->get('password'));
// {'token': '...'}
Now you can save the $token->token
to your database.
use MadeITBelgium\WordPress\WordPressFacade as WordPress;
WordPress::setAccessToken($token);
You can use the newly introduced Application Password (in WordPress 5.6).
$wp = WordPress::setServer('https://www.example.com')
->setUsername('username')
->setApplicationPassword('application password');
WordPress Rest API documentation: https://developer.wordpress.org/rest-api/reference/users/
use MadeITBelgium\WordPress\WordPressFacade as WordPress;
$users = WordPress::user()->list();
$result = WordPress::user()->create($data);
$user = WordPress::user()->get($id);
$result = WordPress::user()->update($id, $data);
$result = WordPress::user()->delete($id);
WordPress Rest API documentation: https://developer.wordpress.org/rest-api/reference/posts/
use MadeITBelgium\WordPress\WordPressFacade as WordPress;
$users = WordPress::post()->list();
$result = WordPress::post()->create($data);
$user = WordPress::post()->get($id);
$result = WordPress::post()->update($id, $data);
$result = WordPress::post()->delete($id);
WordPress Rest API documentation: https://developer.wordpress.org/rest-api/reference/posts/
use MadeITBelgium\WordPress\WordPressFacade as WordPress;
$users = WordPress::customPost('custom_post_type')->list();
$result = WordPress::customPost('custom_post_type')->create($data);
$user = WordPress::customPost('custom_post_type')->get($id);
$result = WordPress::customPost('custom_post_type')->update($id, $data);
$result = WordPress::customPost('custom_post_type')->delete($id);
WordPress Rest API documentation: https://developer.wordpress.org/rest-api/reference/tags/
use MadeITBelgium\WordPress\WordPressFacade as WordPress;
$users = WordPress::tag()->list();
$result = WordPress::tag()->create($data);
$user = WordPress::tag()->get($id);
$result = WordPress::tag()->update($id, $data);
$result = WordPress::tag()->delete($id);
$data = [
'title' => 'title',
'parent' => 0,
'slug' => Str::slug('title', '-'),
'content' => 'content',
];
$post = WordPress::post()->create($data);
// {'id': ...}
use Illuminate\Support\Facades\Storage;
use MadeITBelgium\WordPress\WordPressFacade as WordPress;
$media = WordPress::postCall('/wp-json/wp/v2/media', [
'multipart' => [
[
'name' => 'file',
'contents' => Storage::disk('local')->get($fileLocation),
'filename' => 'filename.jpg',
],
[
'name' => 'title',
'contents' => 'Title of attachment',
],
[
'name' => 'alt_text',
'contents' => 'Alt text',
],
],
]);
Read more about the WordPress rest API: https://developer.wordpress.org/rest-api/
$url = "";
$result = WordPress::getCall('/wp-json'.$url);
$result = WordPress::putCall('/wp-json'.$url, $data);
$result = WordPress::postCall('/wp-json'.$url, $data);
$result = WordPress::deleteCall('/wp-json'.$url);
The complete documentation can be found at: https://www.tpweb.org/my-projects/wordpress-php-sdk/
Support github or mail: [email protected]
Please try to follow the psr-2 coding style guide. http://www.php-fig.org/psr/psr-2/
This package is licensed under LGPL. You are free to use it in personal and commercial projects. The code can be forked and modified, but the original copyright author should always be included!