Skip to content
Advertisement

WordPress custom endpoint – rest_invalid_handler

I am completely new with PHP and WordPress, however I start to write my first plugin that should be able to expose a custom endpoint. I managed to register and activate the plugin and the route, but when I am trying to access this endpoint I am getting the following error:

// http://localhost/myWp/wp-json/myCustomPlugin/v2/lastpost

{
  "code": "rest_invalid_handler",
  "message": "The handler for the route is invalid",
  "data": {
    "status": 500
  }
}

If I access the: http://localhost/myWp/wp-json/myCustomPlugin/v2/ it looks like my endpoint is successfully registered:

// 20190420173834
// http://localhost/myWp/wp-json/myCustomPlugin/v2/

{
  "namespace": "myCustomPlugin/v2",
  "routes": {
    "/myCustomPlugin/v2": {
      "namespace": "myCustomPlugin/v2",
      "methods": [
        "GET"
      ],
      "endpoints": [
        {
          "methods": [
            "GET"
          ],
          "args": {
            "namespace": {
              "required": false,
              "default": "myCustomPlugin/v2"
            },
            "context": {
              "required": false,
              "default": "view"
            }
          }
        }
      ],
      "_links": {
        "self": "http://localhost/myWp/wp-json/myCustomPlugin/v2"
      }
    },
    "/myCustomPlugin/v2/lastpost": {
      "namespace": "myCustomPlugin/v2",
      "methods": [
        "GET"
      ],
      "endpoints": [
        {
          "methods": [
            "GET"
          ],
          "args": [

          ]
        }
      ],
      "_links": {
        "self": "http://localhost/myWp/wp-json/myCustomPlugin/v2/lastpost"
      }
    }
  },
  "_links": {
    "up": [
      {
        "href": "http://localhost/myWp/wp-json/"
      }
    ]
  }
}

and finally I am providing the code of my plugin:

<?php

/**
 * @package myCustomPlugin
 *
 * /
 *
 /*
  Plugin Name: My Custom Plugin
  Plugin URI: https://mypage.net
  Description: my custom plugin
  Version: 1.0.0
  Author: My Name
  Author URI: https://mypage.net
  License: Proprierty
  Text Domain: myCustomPlugin

  */
  /*it means someone outside wp is accessing the file, in this case kill it.
  */

  if(!defined('ABSPATH'))
  {
      die ('You can not access this file!');
  }

  class MyCustomPlugin
  {

      //methods
      function activate(){
          flush_rewrite_rules();
      }

      function register(){
          add_action('rest_api_init', function () {
            register_rest_route('myCustomPlugin/v2', 'lastpost',array(
                          'methods'  => 'GET',
                          'callback' => 'get_last_post'
                ));
          });
      }

      function get_last_post($request) {
        return "it works";
      }

      function deactivate(){
           echo 'The MyCustomPlugin was deactivated';
           flush_rewrite_rules();
      }
  }

  register_activation_hook( __FILE__, array($myCustomPlugin,'activate'));
  register_activation_hook( __FILE__, array($myCustomPlugin,'deactivate'));

  $myCustomPlugin = new MyCustomPlugin();
  $myCustomPlugin->register();

I am using: WordPress 5.1.1 on Windows 10 XAMPP for Windows 7.3.3 Apache/2.4.38 (Win64) OpenSSL/1.1.1b PHP/7.3.3 mysqlnd 5.0.12-dev – 20150407

Thanks to All in advance!

Advertisement

Answer

try this

<?php

/**
 * @package myCustomPlugin
 *
 *
  /*
  Plugin Name: My Custom Plugin
  Plugin URI: https://mypage.net
  Description: my custom plugin
  Version: 1.0.0
  Author: My Name
  Author URI: https://mypage.net
  License: Proprierty
  Text Domain: myCustomPlugin

 */
/* it means someone outside wp is accessing the file, in this case kill it.
 */

if (!defined('ABSPATH')) {
die('You can not access this file!');
}

class MyCustomPlugin {

  function __construct() {
    add_action('rest_api_init', array($this, 'register'));
  }

  //methods
  function activate() {
    flush_rewrite_rules();
  }

  function register() {

    register_rest_route('myCustomPlugin/v2', '/lastpost', array(
        'methods' => 'GET',
        'callback' => array($this,'get_last_post')
    ));
  }

  function get_last_post() {
    return new WP_REST_Response("ok", 200);
  }

  function deactivate() {
    echo 'The MyCustomPlugin was deactivated';
    flush_rewrite_rules();
  }

}

register_activation_hook(__FILE__, array($myCustomPlugin, 'activate'));
register_activation_hook(__FILE__, array($myCustomPlugin, 'deactivate'));

$myCustomPlugin = new MyCustomPlugin();
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement