How to Pass Extra Variables in Url With Wordpress

How to pass extra variables in URL with WordPress

There are quite few solutions to tackle this issue. First you can go for a plugin if you want:

  • WordPress Quickie: Custom Query String Plugin

Or code manually, check out this post:

  • Passing Query String Parameters in WordPress URL

Also check out:

  • add_query_arg

pass variable through url in wordpress

Well, as suggested by @vl4d1m1r4 , I have used the web server(in my case apache) rewrite rule to rewrite the path. Some-what I have achieved the desired result.
In function.php
I have written the following code to update the rewrite rule in wordpress(.htaccess)

add_filter( 'query_vars', 'add_custom_query_vars' );
function add_custom_query_vars( $vars )
{
array_push($vars, "test_year", "test_postid", "test_title");
return $vars;
}
function custom_rewrite_rule()
{
add_rewrite_rule('^test/([^/]*)/([^/]*)/([^/]*)?','index.php?pagename=test&test_year=$matches[1]&test_postid=$matches[2]&test_title=$matches[3]','top');
}
add_action('init', 'custom_rewrite_rule');

After adding the above code, I have re-saved the permalink settings(setting>>permalink)

To Get the parameter in custom-template-page i.e test.php. I have used the following query.

echo get_query_var('test_title');
echo get_query_var('test_postid');
echo get_query_var('test_year');

How to pass extra variable in WP admin edit.php

You can do this with admin_menu action,
The following code should work :

add_action( 'admin_menu', function(){
global $menu, $submenu;

foreach( $submenu['edit.php'] as $k => $v ){
if( $v['2'] == 'edit.php' ) {
$submenu['edit.php'][$k]['2'] = 'edit.php?orderby=123&order=desc';
break;
}
}
}, 99 );


Related Topics



Leave a reply



Submit