GithubHelp home page GithubHelp logo

pinchoalex / wp-custom-endpoints Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marcopal/wp-custom-endpoints

0.0 1.0 0.0 12 KB

WP Custom Endpoints, a blazing fast way to add custom endpoints to your Wordpress REST API, inspired by Laravel routing.

License: MIT License

PHP 100.00%

wp-custom-endpoints's Introduction

WP Custom Endpoints

A plugin to add custom endpoints to your WordPress REST API

  1. Edit route.php to add your custom endpoints.
  2. Add your controllers to manage the routes
  3. (optional) Edit config.php to setup your API namespace and version.

You'll find all your defined routes in the main JSON at yourdomain.com/wp-json

Example

File route.php

Route::get('posts', 'PostsController@getAllPosts');
Route::get('posts/{id?}', 'PostsController@getPostById');
Route::get('posts/{category}/{id}', 'PostsController@getPostById');

Your controller:

class PostsController extends WP_Custom_Endpoints
{

    public static function getAllPosts()
    {
        $posts = get_posts(['posts_per_page' => 5]);
        return array_map(function ($post) {
            return self::post_schema($post);
        }, $posts);
    }
    
    
    public static function getPostById($request)
        {
            if (!empty($request['category'])) {
                // Filter all post by category
            }
    
            if (empty($request['id']))
                return self::getAllPosts([]);
    
            $post = get_post($request['id']);
    
            return self::post_schema($post);
        }

}

   

By default you have 3 methods that you can extend in your controller:

public function get_permission_callback();

If this function returns true, the response will be proccessed If it returns false, a default error message will be returned and the request will not proceed with processing

   

public function get_validate_callback($param, $request, $key)

This function should return true if the value is valid, and false if not.

   

public function get_sanitize_callback($param, $request, $key)

Used to sanitize the value of the argument before passing it to the main callback.

               

TODO: Add a 3rd parameter to the routes declaration to define custom callback methods

wp-custom-endpoints's People

Contributors

marcopal avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.