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 have in my plugin dir a endpoint.php and need to rewrite like this:

http://domain.com/API/endpoint.php

This is the code I've used:

add_rewrite_rule( 'API/endpoint.php', 'myplugin-path/endpoint.php', 'top' );

When I print the $wp_rewrite global I got this:

[non_wp_rules] => Array
    (
        [API/endpoint.php] => http://mydomain/myplugin-path/endpoint.php
    )

But still not work, any Ideas?

share|improve this question
    
updated your question with the code you are using –  Bainternet May 22 '12 at 6:13

1 Answer 1

up vote 1 down vote accepted

Wordpress rewrite mechanism works in a different way, not like .htaccess rewrite rules. More information about it you can find in codex (Rewrite API, WP_Rewrite, etc.). But fast work around could be the following:

First of all we need to register our query var and rewrite rule:

function myplugin_add_query_var( $vars ) {
    $vars[] = 'myplugin_api';
    return $vars;
}
add_filter( 'query_vars', 'myplugin_add_query_var' );

function myplugin_update_rewrite_rules( $rules ) {
    $newrules = array();
    $newrules['api\/endpoint\.php$'] = 'index.php?pagename=api&myplugin_api=1';
    return $newrules + $rules;
}
add_filter( 'rewrite_rules_array', 'myplugin_update_rewrite_rules' );

To complete registration of rewrite rule, we need to flush rules on activation/deactivation of you plugin:

function myplugin_activate() {
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'myplugin_activate' );

function myplugin_deactivate() {
    flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'myplugin_deactivate' );

A piece of magic to make rewrite rules working correctly :D

function myplugin_a_piece_of_magic_on_init() {
    global $wp_rewrite;
    $wp_rewrite->use_verbose_page_rules = !preg_match( "/api\/endpoint\.php$/i", $_SERVER['REQUEST_URI'] );
}
add_action( 'init', 'myplugin_a_piece_of_magic_on_init' );

And finally check if our query var presents on wp action:

function myplugin_check_query_var( $wp ) {
    if ( array_key_exists( 'myplugin_api', $wp->query_vars ) ) {
        // do api stuff here ...
        exit;
    }
}
add_action( 'wp', 'myplugin_check_query_var' );
share|improve this answer

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.