1

I am in a little trouble on wordpress, hope you can help me :)

I have a sub-page that i can call with different paramater :

I want to make clean urls and dismiss the sub-page name :

To do this, i tried this add_rewrite_rule :

function add_directory_rewrite() {
add_rewrite_tag("%symbol%", '([a-zA-Z0-9_]+)');
add_rewrite_rule('^crypto-monnaies/([a-zA-Z0-9_]+).html', '/crypto-monnaies/showcoin/?symbol=$matches[1]', 'top');
}

add_action( 'init', 'add_directory_rewrite' );

The problem is that when i call the rewrited url, it redirect me to https://www.cryptolia.fr/crypto-monnaies/showcoin/ without the parameter

so first it should not be a 301 redirect, and second : it should carry the parameter so i can grab it in PHP.

thanks for your help guys :)

  • Possible duplicate of How can I Rewrite a 'page' URL based on query string parameters? – Anson W Han Jan 26 '18 at 14:52
  • Check out this similar question and answer on stack overflow which walks you through the custom function to write. Be sure to also flush rewrite rules/ permalinks when you've finished updating your code: stackoverflow.com/questions/29076665/… – Anson W Han Jan 26 '18 at 14:54
  • for the 2nd argument of add_rewrite_rule, try with the direct URL of the custom post like index.php?crypto-monnaies=ADA&symbol=$matches[1] – mmm Jan 26 '18 at 19:35
  • You have a mixture of internal and external rule syntax. WordPress assumes any rule that doesn't point to index.php is external, and puts it in the htaccess file, but you are using $matches[1], which is PHP and invalid in an htaccess file. – Milo Jan 26 '18 at 21:32
1

thanks for you reply,

So after much digging, it's working :

function myplugin_rewrite_tag_rule() {
   add_rewrite_tag( '%symbol%', '([a-zA-Z0-9_]+)' );
   add_rewrite_rule( '^crypto-monnaies/([a-zA-Z0-9_]+)/?$', 'index.php?
pagename=showcoin&symbol=$matches[1]','top' );
}
add_action('init', 'myplugin_rewrite_tag_rule');

function add_query_vars( $query_vars ) {
   $query_vars[] = 'symbol';
   return $query_vars;
}
add_filter( 'query_vars', 'add_query_vars' ); 

Please note that if you dynamic page is a second level page, you have to call the ?pagename=parent-page/child-page (with the slash).

To resolve my problem i put the "showcoin" page to first level.

| improve this answer | |

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.