Hussian Shaik
Hussian Shaik

Reputation: 2669

how to call rest API in WP for custom routing

I am new to wordPress, I have a doubt that how to call rest API in WP for custom routing, can any one please provide sample or give me suggestions that how to do this, any help much appreciated

Upvotes: 1

Views: 115

Answers (2)

irfanengineer
irfanengineer

Reputation: 1300

Make first a Virtual link :

 //Add a Virtual Links
    add_action( 'init', 'wpse9870_init_internal' );
    function wpse9870_init_internal()
    {
      global $wp_rewrite;

        $wp_rewrite->add_external_rule( 'my-api.php', 'index.php?api=1', 'top');
    }

    add_filter( 'query_vars', 'wpse9870_query_vars' );
    function wpse9870_query_vars( $query_vars )
    {
        $query_vars[] = 'getrequest';
        return $query_vars;
    }

    add_action( 'parse_request', 'wpse9870_parse_request' );
    function wpse9870_parse_request( &$wp )
    {
        if ( array_key_exists( 'getrequest', $wp->query_vars ) ) {
            include 'my-api.php';
            exit();
        }
        return;
    }

After that you can write your code on my-api.php for restApi and your request url is: http://siteURL.com/?getrequest

Upvotes: 1

Related Questions