How to Rewrite WordPress URL for a Plugin

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

Read More

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'));

Related posts

Leave a Reply

1 comment