1

This question already has an answer here:

I have a plugin with two rewrite rules:

public static function cp_libdatabases_add_rewrites(){
     $options = get_option( 'libdatabases_plugin_options' );
     $staff_url = $options['staff_url'];

     //accept characters A-Z (last name letter)
     add_rewrite_rule('^'.$staff_url.'/libdb/([A-Z]+)/?', 'index.php?libdb=$matches[1]', 'top');
     add_rewrite_rule('^'.$staff_url.'/libsubj/([A-Z]+)/?', 'index.php?libsubj=$matches[1]', 'top');
  }

Depending on the query var, the user is redirected to the appropriate template:

public static function cp_libdatabases_template_redirect(){
  global $wp_query;

  if ( get_query_var( 'libdb' ) ){
    add_filter( 'template_include', function() {
        return LIBDATABASES__PLUGIN_DIR . '/controllers/c.database.php';
    });
  }

  if ( get_query_var( 'libsubj' ) ){
    add_filter( 'template_include', function() {
        return LIBDATABASES__PLUGIN_DIR . '/controllers/c.subject.php';
    });
  }
}

This works when the query var 'libdb' or 'libsubj' has an argument associated with it:

http://example.com/databases/libdb/A/

But I want to be able to show ALL results if the URL is typed in as:

http://example.com/databases/libdb/

I'm unclear on how to tweak my re-direct to achieve this; If I input the URL without the final argument I am sent to the 404 page.

SOLUTION

The regex was missing a leading question mark. Also, replace the + with *:

?([A-Z1-9]*)/?

You must also amend the template_redirect() function to check if the 'libdb' query_var is set:

public static function cp_libdatabases_template_redirect(){
  global $wp_query;
  $query_vars = $wp_query->query_vars;

  if ( isset($query_vars['libdb']) ){
    add_filter( 'template_include', function() {
        return LIBDATABASES__PLUGIN_DIR . '/controllers/c.database.php';
    });
  }

  if ( isset($query_vars['libsubj']) ){
    add_filter( 'template_include', function() {
        return LIBDATABASES__PLUGIN_DIR . '/controllers/c.subject.php';
    });
  }

}

Skipping either of these steps result in the 404. Including these steps allows you to show a list of all records if an argument is not supplied.

marked as duplicate by s_ha_dum Jul 22 '15 at 17:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • If your question is not a duplicate of the one I marked as such, please explain exactly why not. – s_ha_dum Jul 22 '15 at 17:02
  • No I don't think this is necessarily a duplicate for two reasons. 1) When you dump the query vars with an extra argument supplied to (/libdb/A), the result is different than when you have just (/libdb) in the URL - ["libdb"]=> string(1) "A" (when /libdb/A) - ["attachment"]=> string(5) "libdb" (when /libdb) 2. None of the examples on those posts work. – Ravioli87 Jul 22 '15 at 18:58
  • And what are those two reasons? – s_ha_dum Jul 22 '15 at 19:00
  • Basically Wordpress is not reading libdb as a query var at all when there is no extra argument supplied to /libdb. Hence there is always a 404 error. – Ravioli87 Jul 22 '15 at 19:03
  • Which sounds like exactly the problem addressed in what I believe to be a duplicate. Did you try any of those solutions? – s_ha_dum Jul 22 '15 at 19:04