diegocosta/wp-rest-helper

This package is abandoned and no longer maintained. No replacement package was suggested.

v1.0.0 2017-02-17 01:09 UTC

This package is not auto-updated.

Last update: 2020-01-19 16:08:31 UTC


README

This is a library for simplify the creation of WordPress REST endpoints with WP-JSON

Installation

Execute the following command to get the latest version of the package:

composer require diegocosta/wp-rest-helper

Usage

	use DiegoCosta\WP\RestHelper\Rest;
	use DiegoCosta\WP\RestHelper\Route;
	use DiegoCosta\WP\RestHelper\Validate;
	use DiegoCosta\WP\RestHelper\Response;

	function getPosts($data)
    {
        $args = (isset($data['id'])) ? array('p' => $data['id']) : array();
            
        $the_query = new WP_Query($args);
        $posts = $the_query->get_posts();
        
        if($the_query->found_posts) {
            return Response::success($posts);
        }
        
        return Response::error('Posts not found');
    }

    Rest::init("my-namespace", function(){
        
        Route::readable("/posts", 'getPosts');
        
        Route::readable("/posts/(?P<id>\d+)", 'getPosts', array(
            'id' => Validate::isNumeric()
        ));
        
    });