mam4dali / laravel-wp-api
Laravel package for the Wordpress JSON REST API
Requires
- php: ^7.4|^8.0
- ext-json: *
- guzzlehttp/guzzle: ^7.3
- illuminate/support: ^8.12
Requires (Dev)
- phpunit/phpunit: 4.8.*
README
This is a fork of threesquared/laravel-wp-api
laravel-wp-api
Laravel 9 package for the Wordpress JSON REST API
Install
Run:
composer require mam4dali/laravel-wp-api
Configuration
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
Usage
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.
Posts
$wp_api->posts($page);
Pages
$wp_api->pages($page);
Post
$wp_api->post($slug);
$wp_api->postId($id);
Categories
$wp_api->categories();
Tags
$wp_api->tags();
Category posts
$wp_api->categoryPosts($slug, $page);
Author posts
$wp_api->authorPosts($slug, $page);
Tag posts
$wp_api->tagPosts($slug, $page);
Search
$wp_api->search($query, $page);
Archive
$wp_api->archive($year, $month, $page);