This is a fork of threesquared/laravel-wp-api
Laravel 9 package for the Wordpress JSON REST API
Run:
composer require mam4dali/laravel-wp-api
You will need to add the service provider and optionally the facade alias to your config/app.php
:
'providers' => array(
mam4dali\LaravelWpApi\ServiceProvider::class
)
'aliases' => Facade::defaultAliases()->merge([
'WpApi' => mam4dali\LaravelWpApi\Facade::class,
])->toArray(),
And publish the package config files to configure the location of your Wordpress install:
php artisan vendor:publish
You need to install the following plugin in WordPress:
https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/
Example:
$wp_api = new WpApi('http://localhost/wp/wp-json/wp/v2/', new \GuzzleHttp\Client(), null);
$jwt_token = $wp_api->jwtTokenGenerate('username', 'password');
$wp_api->SetJwtToken($jwt_token['token']);
$get_post = $wp_api->postId(1);
**Important: No need to continuously generate tokens. Each token can work up to 7 days. you can save token for 7 days
**
The package provides a simplified interface to some of the existing api methods documented here.
You can either use the Facade provided or inject the AstritZeqiri\LaravelWpApi\WpApi
class.
$wp_api->posts($page);
$wp_api->pages($page);
$wp_api->post($slug);
$wp_api->postId($id);
$wp_api->categories();
$wp_api->tags();
$wp_api->categoryPosts($slug, $page);
$wp_api->authorPosts($slug, $page);
$wp_api->tagPosts($slug, $page);
$wp_api->search($query, $page);
$wp_api->archive($year, $month, $page);