Adding Multiple Custom urls in Wordpress

Currently the way I am adding custom urls in wordpress is

functions.php
function custom_rewrite_rule() {

    add_rewrite_rule('^custompage/([^/]*)/([^/]*)/?','index.php?page_id=1933&param1=$matches[1]&param2=$matches[2]','top');

}
add_action('init', 'custom_rewrite_rule', 10, 0);
add_filter('query_vars', function($vars) {
    $vars[] = "param1";
    $vars[] = "param2";
    return $vars;
});

Open in new window


If I want to have multiple custom urls, I find I have to do the same thing twice like
functions.php
function custom_rewrite_rule() {

    add_rewrite_rule('^custompage/([^/]*)/([^/]*)/?','index.php?page_id=1933&param1=$matches[1]&param2=$matches[2]','top');

}
add_action('init', 'custom_rewrite_rule', 10, 0);
add_filter('query_vars', function($vars) {
    $vars[] = "param1";
    $vars[] = "param2";
    return $vars;
});
// short code to generate data for mysite.com/custompage/


function custom_rewrite_rule_two() {

    add_rewrite_rule('^new_custompage/([^/]*)/?','index.php?page_id=2034&param1=$matches[1]','top');

}
add_action('init', 'custom_rewrite_rule_two', 10, 0);
add_filter('query_vars', function($vars) {
    $vars[] = "param1";
    return $vars;
});

// short code to generate data for mysite.com/new_custompage/

Open in new window


That works, how can I do this in just one function for adding custom urls

function custom_rewrite_rule() {

    add_rewrite_rule('^custompage/([^/]*)/([^/]*)/?','index.php?page_id=1933&param1=$matches[1]&param2=$matches[2]','top');
    add_rewrite_rule('^new_custompage/([^/]*)/?','index.php?page_id=2034&param3=$matches[3]','top');

}
add_action('init', 'custom_rewrite_rule', 10, 0);
add_filter('query_vars', function($vars) {
    $vars[] = "param1";
    $vars[] = "param2";
    $vars[] = "param3";
    return $vars;
});

Open in new window


I have tried different combinations and can't seem to make this fully functional.
LVL 56
Scott Fell, EE MVEDeveloper & EE ModeratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Question:
1. Why not add this directly to .htaccess?
2. What is the purpose of the query_vars hook?
0
Scott Fell, EE MVEDeveloper & EE ModeratorAuthor Commented:
Julian,

I have to integrate dynamic data/html into an already established theme where the dynamic data is coming from a data source not associated with the WP DB.  

What I am doing is creating a page in the current theme and throwing in a shortcode in the body. In WP, I have a page called custompage that contains the shortcode.  Using mysite.com/custompage/param1/param2 I am using the two parameters to filter data.

The shortcode function is
add_shortcode( 'short_code', 'short_code_func' );
short_code_func{

   $param1= get_query_var( 'param1' );
   $param2= get_query_var( 'param2' );

   return $param1.' '.$param2;

}

add_shortcode( 'other_short_code', 'other_short_code_func' );
other_short_code_func{

   $param3= get_query_var( 'param3' );
  
  return $param3;
}

Open in new window

0
Julian HansenCommented:
Ok, but I am confused as to how that is tying into the add_rewrite_rule()

I had no problem with this

function custom_rewrite_rule() {
	add_rewrite_rule('^abc/([0-9]+)/?', 'index.php?page_id=$matches[1]', 'top');
	add_rewrite_rule('^def/([0-9]+)/?', 'index.php?page_id=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);

Open in new window

However, I did have to go to settings => permalinks [SAVE] before the above would work.
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Scott Fell, EE MVEDeveloper & EE ModeratorAuthor Commented:
Yes, every time you make an update to the rewrite rule you need to save the permalinks which runs flush_rewrite_rules();.

The issue I am having that I did not make clear is getting the parameters.

If I have
function custom_rewrite_rule() {

    add_rewrite_rule('^custompage/([^/]*)/([^/]*)/?','index.php?page_id=1933&param1=$matches[1]&param2=$matches[2]','top');
    add_rewrite_rule('^new_custompage/([^/]*)/?','index.php?page_id=2034&param3=$matches[3]','top');

}
add_action('init', 'custom_rewrite_rule', 10, 0);
add_filter('query_vars', function($vars) {
    $vars[] = "param1";
    $vars[] = "param2";
    $vars[] = "param3";
    return $vars;
});

Open in new window


Then on the shortcode function for each of custompage and new_custompage, accessing the parameters is where I am having trouble. If I run as separate functions it works, but it seems all of these can be in one function.  Actually the first rule for custompage I can access param1 and param2. But it does not work for new_custompage. The parameter is null.

I have also tried
add_rewrite_rule('^custompage/([^/]*)/([^/]*)/?','index.php?page_id=1933&param1=$matches[1]&param2=$matches[2]','top');
    add_rewrite_rule('^new_custompage/([^/]*)/?','index.php?page_id=2034&param3=$matches[1]','top');

Open in new window


Note that I am using index.php?page_id=1933 as the filter so each is specific to a page.
0
Scott Fell, EE MVEDeveloper & EE ModeratorAuthor Commented:
The URL will look like mysite.com/custompage/abc/123xyz And I need to extract the two parameters abc and 123xyz
0
gr8gonzoConsultantCommented:
Try using a custom class to group the callbacks together and make a clean, one-line setup:

<?php
class CustomPage
{
  private $numParameters = 0;
  private $rewriteFromURL;
  private $rewriteToURL;
  
  public function __construct($page, $toPageID, $numParameters = 0)
  {
    // Keep track of the # of parameters fro the filter_query_vars callback
    $this->numParameters = $numParameters;
    
    // Build "from" and "to" portions of the rewrite rule
    $this->rewriteFromURL = "{$page}/";
    $this->rewriteToURL = "index.php?page_id={$toPageID}";
    for($i = 0; $i < $this->numParameters; $i++)
    {
      $this->rewriteFromURL .= "([^/]*)/";
      $this->rewriteToURL .= "&param".($i+1)."=\$matches[".($i+1)."]";
    }
    $this->rewriteFromURL .= "?";
    
    // Hook into the action and filter
    add_action('init', array( $this, 'action_init' ));
    add_filter('query_vars', array( $this, 'filter_query_vars' ));
  }
  
  public function action_init()
  {
    add_rewrite_rule($this->rewriteFromURL, $this->rewriteToURL, 'top');
  }
  
  public function filter_query_vars($vars)
  {
    for($i = 0; $i < $this->numParameters; $i++)
    {
      $vars[] = "param" . ($i + 1);
    }
    return $vars;
  }
}

Open in new window


With that code, you can set up your callbacks with one line of code each:
// new CustomPage("shortcode", desired page ID, # of parameters to pass);
new CustomPage("custompage", 1933, 2);
new CustomPage("new_custompage", 2034, 1);

Open in new window

2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott Fell, EE MVEDeveloper & EE ModeratorAuthor Commented:
Perfect solution!
0
Scott Fell, EE MVEDeveloper & EE ModeratorAuthor Commented:
Thank you!  This is what I used.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.