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 am trying to build a Wordpress plugin here with a custom user admin area. What I would like to do is when the user adds /edit at the end of the page, it should open a page from my plugin. So for example

example.com/page-1/edit should load
example.com/wp-content/plugins/custom-user-admin-area/index.php?u=page-1

I am trying to make use of WP_Rewrite but in vain.

class MyPlugin {
    function create_rewrite_rules($rules) {
        global $wp_rewrite;
        $newRule = array('([A-Za-z0-9-].+)/edit/' => 'wp-content/plugins/custom-user-admin-area/index.php?u=' . $wp_rewrite->preg_index(1));
        $newRules = $newRule + $rules;
        return $newRules;
    }

    function flush_rewrite_rules() {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }

}

$MyPluginCode = new MyPlugin();
add_filter('rewrite_rules_array', array($MyPluginCode, 'create_rewrite_rules'));
add_filter('init', array($MyPluginCode, 'flush_rewrite_rules'));
share|improve this question
    
Do not use your plugin URL like that. Plugins can be on a separate domain where the Cookies do not work. Use a custom URL under the main web site instead. –  toscho Oct 18 '12 at 22:01
    
I might be needing wordpress functions too. So, do you mean create a new php file and load wp-load.php instead? My edit page updates a posts and its custom fields basically. –  Atif Mohammed Ameenuddin Oct 18 '12 at 22:06
add comment

1 Answer

up vote 2 down vote accepted

Create an endpoint. In the callback function for the endpoint call your plugin functions internally, so the whole WordPress environment is available and you are still on the same domain, no matter where the plugin URL is.

Plus, make sure not to flush the rewrite rules on every page load, use the (de)activation hook for that.

share|improve this answer
    
exactly what I wanted. Thanks ! –  Atif Mohammed Ameenuddin Oct 18 '12 at 22:19
    
care to explain this line I saw that coding style for the first time isset($vars['epex']) and $vars['epex'] = true; –  Atif Mohammed Ameenuddin Oct 18 '12 at 22:21
1  
@atif089 That is the same like if ( isset($vars['epex']) ) {$vars['epex'] = true;} – just shorter. The part after the and will be ignored if the part before that is false. –  toscho Oct 19 '12 at 18:44
    
Thanks, another thing is how shall I handle POST requests on the same URL, as of now it does not seem to handle POST requests on the same page. –  Atif Mohammed Ameenuddin Oct 27 '12 at 21:54
    
@atif089 Ask a new question for that please. –  toscho Oct 27 '12 at 22:57
show 1 more 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.