Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to create a custom API endpoint in WordPress, and I need to redirect requests to a virtual page in the root of WordPress to an actual page that ships with my plug-in. So basically, all requests to the one page are actually routed to the other.

Example:
http://mysite.com/my-api.php => http://mysite.com/wp-content/plugins/my-plugin/my-api.php

The point of this is to make the url for the API endpoint as short as possible (similar to http://mysite.com/xmlrpc.php but to ship the actual API endpoint file with the plug-in rather than requiring the user to move files around in their installation and/or hack core.

My first stab was to add a custom rewrite rule. However, this had two problems.

  1. The endpoint always had a trailing slash. It became http://mysite.com/my-api.php/
  2. My rewrite rule was only ever partially applied. It wouldn't redirect to wp-content/plugins..., it would redirect to index.php&wp-content/plugins.... This lead to WordPress displaying either a page not found error or just defaulting to the homepage.

Ideas? Suggestions?

share|improve this question
add comment

8 Answers

up vote 19 down vote accepted

There are two types of rewrite rules in WordPress: internal rules (stored in the database and parsed by WP::parse_request()), and external rules (stored in .htaccess and parsed by Apache). You can choose either way, depending on how much of WordPress you need in your called file.

External Rules:

The external rule is the easiest to set up and to follow. It will execute my-api.php in your plugin directory, without loading anything from WordPress.

add_action( 'init', 'wpse9870_init_external' );
function wpse9870_init_external()
{
    global $wp_rewrite;
    $plugin_url = plugins_url( 'my-api.php', __FILE__ );
    $plugin_url = substr( $plugin_url, strlen( home_url() ) + 1 );
    // The pattern is prefixed with '^'
    // The substitution is prefixed with the "home root", at least a '/'
    // This is equivalent to appending it to `non_wp_rules`
    $wp_rewrite->add_external_rule( 'my-api.php$', $plugin_url );
}

Internal Rules:

The internal rule requires some more work: 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;
}
share|improve this answer
1  
I just want to add that its important to go to the Permalinks page and click "Save Changes" in the WP-Admin. I was playing with this for an hour before thinking that I needed to refresh the permalinks... Unless someone knows a function that can do that? –  ethanpil May 28 '13 at 18:21
    
For the external rule: Because the path to my web root had a whitespace character, this caused apache to fall over. Whitespaces need to be escaped if they exist in the path to your wordpress installation. –  Willster Jun 4 '13 at 9:41
1  
Works, but I can't seem to access any passed query variables with get_query_vars() in my-api.php. I checked what variables are loaded. And the only var that is set is a a WP object called $wp. How do I access or tranform this into a WP_Query object so I can access the passed variables with get_query_vars()? –  Jules Aug 13 '13 at 10:24
1  
@Jules: When you include a file, it gets executed in the current scope. In this case, it is the wpse9870_parse_request function, which only has the $wp parameter. It's possible that the global $wp_query object hasn't been set at this time, so get_query_var() will not work. However, you are lucky: $wp is the class that contains the query_vars member that you need - I use it myself in the above code. –  Jan Fabry Aug 13 '13 at 15:49
    
@JanFabry Thanks. Figured that out after a while. I was just wondering if there is a way to convert a WP object to a WP_Query object. But I'm not to sure if there would ever even be a need for something like that, so never mind. Thanks for this and all your other pieces of code. Found a lot of helpful information on this site already thanks to you. –  Jules Aug 14 '13 at 16:59
show 1 more comment

Any reason not to do something like this instead?

http://mysite.com/?my-api=1

Then just hook your plugin into 'init' and check for that get variable. If it exists, do what your plugin needs to do and die()

share|improve this answer
2  
That would work, but I'm trying to provide a very clear distinction between the query variables and the actual endpoint. There might be other query args in the future, and I don't want users to mix things up. –  EAMann Feb 20 '11 at 4:53
    
What if you kept the rewrite, but rewrote it to the GET var? You might also look at how the rewrite for robots.txt works. It might help you figure out how to avoid the redirect to my-api.php/ –  Will Anderson Feb 20 '11 at 5:34
add comment

I haven't dealt with rewrite that much, yet, so this is probably a little rough, but it seems to work:

function api_rewrite($wp_rewrite) {
    $wp_rewrite->non_wp_rules['my-api\.php'] = 'wp-content/plugins/my-plugin/my-api.php';
    file_put_contents(ABSPATH.'.htaccess', $wp_rewrite->mod_rewrite_rules() );
}

It works if you hook this into 'generate_rewrite_rules', but there must be a better way, as you don't want to rewrite .htaccess on each page load.
Seems like i can't stop editing my own posts...it should probably rather go into you activate callback and reference global $wp_rewrite instead. And then remove the entry from non_wp_rules and output to .htaccess again in you deactivate callback.

And finally, the writing to .htaccess should be a bit more sophisticated, you want to only replace the wordpress section in there.

share|improve this answer
add comment

This worked for me. I never ever touch the rewrite API, but am always up to push myself in new directions. The following worked on my test server for 3.0 located in a sub folder of localhost. I don't for see any issue if WordPress is installed in web root.

Just drop this code in a plugin and upload the file named "taco-kittens.php" directly in the plugin folder. You will need write a hard flush for your permalinks. I think they say the best time to do this is on plugin activation.

function taco_kitten_rewrite() {
    $url = str_replace( trailingslashit( site_url() ), '', plugins_url( '/taco-kittens.php', __FILE__ ) );
    add_rewrite_rule( 'taco-kittens\\.php$', $url, 'top' );
}
add_action( 'wp_loaded', 'taco_kitten_rewrite' );

Best wishes, -Mike

share|improve this answer
    
I got an access denied error while trying this code. I suspect my server or WP didn't like the absolute URL. This, on the other hand, worked fine: add_rewrite_rule( 'taco-kittens', 'wp-content/plugins/taco-kittens.php', 'top' ); –  Jules Aug 13 '13 at 9:46
add comment

I may not be understanding you questions fully, but would a simple shortcode solve your issue?

Steps:

  1. Have the client create a page i.e. http://mysite.com/my-api
  2. Have the client add a shortcode in that page i.e. [my-api-shortcode]

The new page acts as an API end point and your shortcode sends requests to your plugin code in http://mysite.com/wp-content/plugins/my-plugin/my-api.php

( of course this means that my-api.php would have the shortcode defined )

You can probably automate steps 1 and 2 via the plugin.

share|improve this answer
add comment

I had a similar requirement and wanted to create several end-points based on unique slugs that pointed to content generated by the plugin.

Have a look at the source for my plugin: https://wordpress.org/extend/plugins/picasa-album-uploader/

The technique I used starts by adding a filter for the_posts to examine the incoming request. If the plugin should handle it, a dummy post is generated and an action is added for template_redirect.

When the template_redirect action is called, it must result in outputting the entire contents of the page to be displayed and exit or it should return with no output generated. See the code in wp_include/template-loader.php and you'll see why.

share|improve this answer
add comment

I'm using another approach which consists in forcing the home page to load a custom title, content and page template.

The solution is very neat since it can be implemented when a user follows a friendly link such as http://example.com/?plugin_page=myfakepage

It is very easy to implement and should allow for unlimited pages.

Code and instructions here: Generate a custom/fake/virtual Wordpress page on the fly

share|improve this answer
add comment

I'm using an approach similar to Xavi Esteve's above, which stopped working due to a WordPress upgrade as far as I could tell in the second half of 2013.

It's documented in great detail here: http://stackoverflow.com/questions/17960649/wordpress-plugin-generating-virtual-pages-and-using-theme-template

The key part of my approach is using the existing template so the resulting page looks like it's part of the site; I wanted it to be as compatible as possible with all themes, hopefully across WordPress releases. Time will tell if I was right!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.