Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom url for my web service in wordpress

Tags:

php

wordpress

I am new in wordpress and i am creating an extension for wordpress. I searched a-lot but didn't find any solution. May be my question is duplicate but i didn't find any solution. My issue is :

I want to create a custom url for my extension api. Like some will post data on that url and i will save data in database. My url should be www.example.com/custom_url

There will be no page and no template for my url, it's a kind of web service.

So in my extension i want that when any user install that extension this custom_url will automatically active for them.

I tried add_rewrite_rule , add_rewrite_endpoints etc. But no success.

please suggest me or provide me any link where it is described.

like image 475
ajaykumartak Avatar asked May 19 '15 11:05

ajaykumartak


People also ask

How do I change my WordPress Plugin URL?

Once you install and activate the plugin, go to Tools → Update URLs. Fill in the one field with your old URL, then type in the new (desired) URL in the other field. Check off which URL instances you would like to change. This would most likely include URLs in page content and most of the other checkboxes.


1 Answers

According to the tutorial on WordPress forum:

What are endpoints?

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

Full Tutorial here: https://make.wordpress.org/plugins/2012/06/07/rewrite-endpoints-api/

So it is not helpful for creating virtual pages as you need.

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

How do you create a “virtual” page in WordPress

Here is some excerpt from original article:

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

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

like image 183
Dharmang Avatar answered Oct 07 '22 01:10

Dharmang