Add custom url for my webservice in wordpress

I am new to wordpress and I am creating an extension for wordpress. I searched a lot but couldn't find a solution. Perhaps my question is repeated, but I haven't found a solution. My problem:

I want to create a custom url for my api extension. similarly some of them will publish data to this url and I will save the data to the database. My url should bewww.example.com/custom_url

There will be no page and no template for my url, this is a kind of web service.

So, in my extension, I want that when any user installs this extension, this custom_url will be automatically active for them.

I've tried add_rewrite_rule

, add_rewrite_endpoints

etc. But there was no success.

please suggest me or point me to any link where this is described.

+3


source to share


2 answers


According to a tutorial on the WordPress forum:

What are endpoints?

Using endpoints allows you to easily create rewrite rules to allow normal WordPress URLs, but with a little extra at the end. For example, you can use an endpoint to match all URLs followed by "gallery" and display all images used in the post, for example. http://example.com/my-fantastic-post/gallery/ .

The complete tutorial is here: https://make.wordpress.org/plugins/2012/06/07/rewrite-endpoints-api/

So it is not useful for creating virtual pages as needed.

However, as per the following post on the WordPress Stack Exchange, it is possible to create virtual pages using add_rewrite_rule



How to create a "virtual" page in WordPress

Here are some excerpts from the original article:

first we will add a rewrite rule that will add the vars query, then we will make it query var public and then we need to check for this var query to pass the control to our plugin file. By the time we do this, the normal WordPress initialization will have happened (we break right before the normal post request).

add_action( 'init', 'wpse9870_init_internal' );
function wpse9870_init_internal()
{
    add_rewrite_rule( 'my-api.php$', 'index.php?wpse9870_api=1', 'top' );
}

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

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

      

Codex page add_rewrite_rule

+4


source


1). create an action for the virtual page.

add_action( 'template_redirect', 'apicall' );


function apicall() {

$callapi = $_GET['API'];

if($callapi=='YOUR API Name'){

            header('Content-Type: text/javascript; charset=utf8');
            header("Access-Control-Allow-Origin: *"); 
            header('Access-Control-Max-Age: 3628800');
            header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');

{{YOUR LOGIC}}

        exit;
}   
}

      



Browser entry URL: www.YOURDOMAIN.com/?API=YOUR API NAME

0


source







All Articles