1

I would like to create a custom Wordpress page from a plugin displaying content that is not from Wordpress with 2 get parameters and apply rewriting rules on it.

Let's say I have a file called test.php. I would like with 2 get parameters to display content using the URL:

example.com/param1/param2/

I'm good on the plugin side (Admin, etc...) but have no clue about custom page. At the moment I have this code in my test.php file but I'm really not sure if that's the right way to do:

include('../../../wp-load.php');
include('myplugin.php');

global $myclass, $wpdb;

echo $myclass->get_setting('test');

Or would I need to check query vars parameters passed in URL and modify content accordingly?

I tried some stackoverflow answers without success so if you could hint me up on this. Thanks a lot in advance.

1 Answer 1

1
  1. Create a page for this purpose, which you can filter its content later UNLESS you are working other way.

  2. Add the page ID to the rewrite rule init function:

    add_action('init', function() {
    
        $page = get_post( 2 ); // replace 2 with page ID
    
        add_rewrite_rule(
            '([^/]+)/([^/]+)/?$',
            'index.php?pagename=' . $page->post_name . '&my_param_1=$matches[1]&my_param_2=$matches[2]',
            'top'
        );
    
    });
    
  3. Append the query variables to the main query:

    add_filter('query_vars', function($vars) {
        $vars[] = "my_param_1";
        $vars[] = "my_param_2";
        return $vars;
    });
    

You should be done now and you need to flush the rewrite rules with either flush_rewrite_rules() hooked to init or wp, or go to Settings > Permalinks and hit the save button in the bottom.

Now a test > http://example.com/john/smith/ :

// first param
echo var_dump( get_query_var( 'my_param_1' ) ); // john

// second param
echo var_dump( get_query_var( 'my_param_2' ) ); // smith

I hope that works for you as it did in my local installation.

Edit: It would be much better if you make the parameters go after the page name like $page->post_name . '/([^/]+)/([^/]+)/?$' so you get /sample-page/john/smith/ This way you won't make any conflicts to existing contents and WordPress will serve the existing content initially before passing to your plugin OR add a unique prefix before the first param slug. Just a thought so.

15
  • Create a page for this purpose? You mean in admin pages? If yes do you think it can be done without? Thanks for your precious help!! May 17, 2016 at 16:20
  • No, a front-end page. Pages > Add New or with wp_insert_post() in your plugin developer.wordpress.org/reference/functions/wp_insert_post
    – Ismail
    May 17, 2016 at 16:26
  • It would be better to work with a page so you benefit from the page template. After that you use add_filter('the_content' ...) to set the content for that page. Or even better, through a custom shortcode, I recommend this one.
    – Ismail
    May 17, 2016 at 16:27
  • Yes but what if I wanna display a page without any content? Just the 2 get variables? Thanks a lot for your help!! May 17, 2016 at 16:31
  • 1
    No all good. You the one! May 17, 2016 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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