WordPress.org

Ready to get started?Download WordPress

Forums

JSON API
External controllers in a plugin (Error) (4 posts)

  1. Lucas
    Member
    Posted 8 months ago #

    Hello
    I am currently developing and building a plugin based upon the JSON-API Plugin.
    However I have run into a problem where the controller is not registered.

    At the admin screen (where you select your controllers i get this error):

    Error: Cannot find controller class 'json_api_hello_controller'

    It is a very basic plugin so here you will get the sample code.
    Most of it is reused from the documentation with one small exception the controller's path.

    Here is the code to the files:

    api-extend.php

    <?php
    /**
     * Plugin Name: API Extend plugin
     * Plugin URI: http://localhost/
     * Description: API Extender Plugin.
     * Version: 1.0
     * Author: Lucas Dechow
     * Author URI: http://localhost/
     * License: GPL2+
     */
    
    // Add a custom controller
    add_filter('json_api_controllers', 'add_my_controller');
    
    function add_my_controller($controllers) {
      // Corresponds to the class JSON_API_Hello_Controller
      $controllers[] = 'hello';
      return $controllers;
    }
    
    // Register the source file for JSON_API_Hello_Controller
    add_filter('json_api_hello_controller_path', 'hello_controller_path');
    
    function hello_controller_path($default_path) {
      return plugins_url( 'hello.php' , __FILE__ );
    }

    hello.php

    <?php
    /*
    Controller name: Hello World Controller
    Controller description: Basic "Hello World" method
    */
    class JSON_API_Hello_controller {
    
      public function hello_world() {
        return array(
          "message" => "Hello, world"
        );
      }
    
    }

    http://wordpress.org/plugins/json-api/

  2. Lucas
    Member
    Posted 8 months ago #

    I found a solution but it is indeed ugly (IMHO)
    You have to set a path relative to the plugin itself.
    That means that you cannot use a "absolute" (http://) or another path.

    So instead of having this:

    return plugins_url( 'hello.php' , __FILE__ );

    You got to have this:

    return "../wp-content/plugins/api-extend/hello.php";

    @dphiffer - is there any way possible that I can use the standard absolute path through my plugin?
    A constant that needs to be changed or?

  3. Manuel Abarca
    Member
    Posted 3 months ago #

    Thanks Lucas, you just saved me hours of wandering around

    Cheers!

  4. ColinD
    Member
    Posted 3 months ago #

    You need to return a file path. By calling
    plugins_url( 'hello.php' , __FILE__ )
    You are returning a URL

    You might try something like
    plugin_dir_path( __FILE__ ) . 'hello.php'

    Or if you're doing it from within a theme
    get_stylesheet_directory() . 'hello.php'

Reply

You must log in to post.

About this Plugin

About this Topic