Created
January 19, 2021 03:59
-
-
Save codetot/f0e2680fe6ff0b4e6d5d26815de98967 to your computer and use it in GitHub Desktop.
New class WordPress API for register custom routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( ! defined( 'ABSPATH' ) ) exit; | |
class Codetot_Api { | |
/** | |
* Singleton instance | |
* | |
* @var Codetot_Api | |
*/ | |
private static $instance; | |
/** | |
* @var string | |
*/ | |
private $route_name; | |
/** | |
* Get singleton instance. | |
* | |
* @return Codetot_Api | |
*/ | |
public final static function instance() { | |
if ( is_null( self::$instance ) ) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
/** | |
* Class constructor | |
*/ | |
private function __construct() | |
{ | |
$this->route_name = 'codetot/v1'; | |
add_action('rest_api_init', array($this, 'register_custom_routes')); | |
} | |
public function register_custom_routes() { | |
register_rest_route($this->route_name, 'get_menu_html', array( | |
'methods' => 'GET', | |
'callback' => array($this, 'get_menu_html_callback'), | |
'permission_callback' => '__return_true' | |
)); | |
} | |
/** | |
* @param WP_REST_Request $request | |
* @return WP_REST_Response | |
*/ | |
public function get_menu_html_callback($request) { | |
$html = ''; | |
return new WP_REST_Response( | |
array( | |
'html' => $html | |
), | |
400 | |
); | |
} | |
} | |
Codetot_Api::instance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment