New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

madeitbelgium/wordpress-php-sdk

WordPress Laravel PHP SDK
37,381 44
Install
composer require madeitbelgium/wordpress-php-sdk
Latest Version:1.7.0
PHP:>=5.6
Laravel:4 – 12
License:LGPL-3.0-or-later
Last Updated:May 6, 2025
Links: GitHub  ·  Packagist
Maintainer: MadeITBelgium

WordPress API PHP SDK

Build Status Coverage Status Latest Stable Version Latest Unstable Version Total Downloads License

Installation

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"

Documentation

Authentication

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);

Application Password

You can use the newly introduced Application Password (in WordPress 5.6).

$wp = WordPress::setServer('https://www.example.com')
    ->setUsername('username')
    ->setApplicationPassword('application password');

Interact with objects

User

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);

Post

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);

Post from custom post type

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);

Tags

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);

Create new post

$data = [
    'title' => 'title',
    'parent' => 0,
    'slug' => Str::slug('title', '-'),
    'content' => 'content',
];
$post = WordPress::post()->create($data);
// {'id': ...}

Uploading media

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',
        ],
    ],
]);

Execute any request

Read more about the WordPress rest API: https://developer.wordpress.org/rest-api/

Get

$url = "";
$result = WordPress::getCall('/wp-json'.$url);

Put

$result = WordPress::putCall('/wp-json'.$url, $data);

Post

$result = WordPress::postCall('/wp-json'.$url, $data);

Delete

$result = WordPress::deleteCall('/wp-json'.$url);

The complete documentation can be found at: https://www.tpweb.org/my-projects/wordpress-php-sdk/

Support

Support github or mail: tjebbe.lievens@madeit.be

Contributing

Please try to follow the psr-2 coding style guide. http://www.php-fig.org/psr/psr-2/

License

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!

Related Packages

valorin/random

Random is a simple helper package designed to make it easy to generate a range o...

423,305 234
php
josh-taylor/sage-svg

WordPress package to enable a blade directive to use SVG images inline with Sage...

46,556 18
lunar-build/sage-lib

Library files for Sage Starter Theme, forked by Lunar Build to support PHP8.1

25,571 0
cline/variable-keys

Laravel Blueprint macros for variable primary key and morph types

23,466 0
php
cline/struct

Lean, attribute-driven Laravel data objects with explicit validation and Eloquen...

18,346 0
php

Version History

Version Released PHP Laravel License
1.7.0 >=5.6 * LGPL-3.0-or-later
1.6.0 >=5.6 * LGPL-3.0-or-later
1.5.1 >=5.6 * LGPL-3.0-or-later
1.5.0 >=5.6 * LGPL-3.0-or-later
1.4.0 >=5.6 * LGPL-3.0-or-later