PHP code example of rickwest / laravel-wordpress-api
1. Go to this page and download the library: Download rickwest/laravel-wordpress-api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rickwest / laravel-wordpress-api example snippets
// Without the package 👎
Http::get('https://example.com/wp-json/wp/v2/posts', [
'search' => 'potatoes',
'_embed' => 1,
'orderby' => 'date',
'order' => 'desc'
'_fields' => 'title',
]);
// Using the package 👌
WordPress::posts()
->search('potatoes')
->embed()
->latest()
->get('title');
// Resolve service directly from container and access the Posts API
app(WordPress::class)->posts();
// Resolve via Facade and access the Posts API
WordPress::posts();
// Resolve service via helper and access the Posts API
wordpress()->posts();
// Supported resources
WordPress::categories(); // Access the Categories API
WordPress::comments(); // Access the Comments API
WordPress::media(); // Access the Media API
WordPress::pages(); // Access the Pages API
WordPress::posts(); // Access the Posts API
WordPress::users(); // Access the Users API
Wordpress::tags(); // Access the Tags API
Wordpress::plugins(); // Access the Plugins API
// You can also access resources as properties
wordpress()->posts
WordPress::posts()->find(1);
// All WordPress resources share a handful of global parameters. https://developer.wordpress.org/rest-api/using-the-rest-api/global-parameters/
// You can use the relevant fluent builder methods to add these to your query
WordPress::posts()->embed()->fields('title')->find(1);
// Some resources also accept a limited number of resource specific parameters. These can be passed as a second argument to the find method
WordPress::posts()->find(1, ['password' => 'pa55w0rd']);
WordPress::posts()->get();
// All WordPress resources share a handful of global parameters, https://developer.wordpress.org/rest-api/using-the-rest-api/global-parameters/,
// along with a number of filtering, ordering and pagination options. You can use the relevant fluent builder methods to build your query.
WordPress::posts()
->embed(array|string $relations) // Embed linked resources into response. Reduces need for extra HTTP requests for related resources
->fields(array|string $fields) // Specify a subset fields to return in a response
->with(array|string $fields) // Alias for fields method above
->page(int $page) // Current page of the collection
->perPage(int $perPage) // Maximum number of items to be returned in result set
->search(string $term) // Limit results to those matching a string
->offset(int $offset) // Offset the result set by a specific number of items
->exlclude(int|array $ids) // Ensure result set excludes specific IDs
->inlude(int|array $ids) // Limit result set to specific IDs
->orderBy(string $field, string $direction) // Sort collection by object attribute, either ascending or descending
// Resources with authors
->author() // Limit result set to resources assigned to specific authors
->authorExclude() // Ensure result set excludes resources assigned to specific authors
// Resources with dates
->after(Carbon $after) // Limit response to resources published after a given ISO8601 compliant date
->before(Carbon $before) // Limit response to resources published before a given ISO8601 compliant date
->latest() // Order by date, descending
->oldest() // Order by date, ascending
// Resources with slugs
->slug(string $slug)
// When a utility doesn't exist for the parameter
->parameter(string $key, mixed $value) // Add a custom parameter to the query
// Send it!
->get();
// Conditionally adding parameters
WordPress::posts()
->when($onlyIncludeTitle, function($query) {
$query->fields('title');
})
->get();
WordPress::posts()->send(string $method, int $id, array $options);
// For example, updating a post might look like...
WordPress::posts()->send('POST', 1, [
'json' => ['title' => 'My New Title'],
]);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.