I need to pass a URL variable to my category.php file.
Currently my category page is at http://example.com/category-slug/
I am using the SEO plugin to rewrite http://example.com/category/category-slug and remove the /category/ part.
Also, the settings formy permalinks are set to this option in the settings menu: http://example.com/sample-post/
Now I need to be able to pass a variable in the URL like:
http://example.com/category-slug/?type=VALUE
or
http://example.com/category-slug/VALUE
where "type" is the name of the variable and VALUE is its value
I have tried using this piece of code in my functions.php file:
<?php
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = 'type';
return $qvars;
}
global $wp_query;
if (isset($wp_query->query_vars['type']))
{
print $wp_query->query_vars['type'];
}
?>
However, when I try to open http://example.com/category-slug/?type=something or http://example.com/category-slug/something I get "nothing found" and "Page not found" pages.
While I see this has been discussed over and over, none of the solutions seem to work for my case.
How do I properly pass a variable to a category page?
First of all, you code will never reach the if statement, as you return from the function before.
I also don't know which SEO tool you are using, but there is one function that goes with the "query_vars" filter: add_rewrite_rule()
I would recommend to write a little plugin which does the rewriting of the category permalink. Something like this (untested, but similar to a plugin I use):
// Flush added rewrite rules on activation
function category_permalink_rewrite_activate() {
category_permalink_rewrite_set_rewrite_rules();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'category_permalink_rewrite_activate' );
// Remove rewrite rule for event archives
function category_permalink_rewrite_deactivate() {
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'category_permalink_rewrite_deactivate' );
// Add rewrite rule for category permalink on init
add_rewrite_rule( '^category-(.*)/(.*)', 'index.php?category_name=$matches[1]&type=$matches[2]', 'top' );
kaufunction category_permalink_rewrite_set_rewrite_rules() {
}
add_filter( 'init', 'category_permalink_rewrite_set_rewrite_rules' );
// Register the custom query var so WP recognizes it
function category_permalink_rewrite_add_query_vars( $vars ) {
$vars[] = 'type';
return $vars;
}
add_filter( 'query_vars', 'category_permalink_rewrite_add_query_vars' );
Related
I'm adding rewrite rules to my PHP script which is included in a WordPress page with the permalink kb
So I can visit domain.com/kb and the page is displayed.
function wdm_add_rewrite_rules() {
add_rewrite_rule( '^kb/([^/]+)/?$', 'kb?kb_cat=$matches[1]&kb_seq=1', 'top');
}
add_action('init', 'wdm_add_rewrite_rules');
But when I visit the page with additional strings in the url, I get a 404.
So when I visit domain.com/kb is shows the correct page, and then visiting domain.com/kb/84/92, it shows a 404
I just need to be able to read the additional url params in my PHP script, such as $_GET["kb_cat"]
function wdm_add_rewrite_rules() {
add_rewrite_rule( '^kb$', 'index.php?kb_cat=$matches[1]&kb_seq=1', 'top');
}
add_action('init', 'wdm_add_rewrite_rules');
to take it a step further and use the parameters:
function add_query_vars_filter( $vars ){
$vars[] = "kb_cat";
$vars[] = "kb_seq";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
and then load a custom template file:
function include_custom_template($template){
if(get_query_var('kb_cat') && get_query_var('kb_seq')){
$template = get_template_directory() ."/my-custom-template.php";
}
return $template;
}
add_filter('template_include', 'include_custom_template');
Once adding to your functions.php go to Settings > Permalinks and hit 'save changes' to reset the flush rules
try this:
function wdm_add_rewrite_rules() {
add_rewrite_rule( '^kb\/([^\/]+)\/?([^\/]+)$', 'kb?kb_cat=$matches[1]&kb_seq=1', 'top');
}
add_action('init', 'wdm_add_rewrite_rules');
You can check the regular expression on https://regex101.com/ or any other websites like this online for the matches
I have a wordpress theme template page accessible through "www.myDomain.com/documents".
The slug of the page is therefor "documents".
But i can't seem to find a way to add a ressource to the URL.
How can i add a ressource like "123" at the end and still have it go to my "documents" page?
Like so "www.myDomain.com/documents/123" with the last part being the id to get in my wordpress theme page code... right now it just ends up as a 404.
add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
add_rewrite_rule(
'documents/([A-Za-z0-9_-]+)/?$',
'index.php?pagename=documents&documents_id=$matches[1]',
'top' );
}
add_filter( 'query_vars', 'wpse26388_query_vars' );
function wpse26388_query_vars( $query_vars ){
$query_vars[] = 'documents_id';
return $query_vars;
}
add_filter('init','flushRules');
function flushRules(){ global $wp_rewrite; $wp_rewrite->flush_rules(); }
I'm trying to give a pretty url to my custom CPT (Orders). This is the way im trying:
add_filter('post_type_link', 'custom_post_type_link', 1, 3);
function custom_post_type_link( $link, $post = 0 ){
if ( $post->post_type == 'orders' ){
$order_code = 1121000+$post->ID;
return home_url( 'orders/' . $order_code);
} else {
return $link;
}
}
Assume that post ID is 32, This code creates the following url:
http://www.example.com/orders/1121032
Now i wanna write a code to display the post with post ID=32 in above url.
I heard somewhere that i should use custom query vars. But i don't know how to use it.
This is the rest of code i've written
add_filter('query_vars', 'custom_add_query_vars');
function custom_add_query_vars($qVars){
$qVars[] = "code";
return $qVars;
}
add_action( 'init', 'custom_rewrites_init' );
function custom_rewrites_init(){
add_rewrite_rule(
'orders/([0-9]+)?$',
'index.php?post_type=orders&code=$matches[1]',
'top' );
}
After adding custom rewrite rules, wordpress needs to be told to activate the new rule.
Use this method after the add_rewrite_rule function call.
$wp_rewrite->flush_rules();
Warning: flush_rules is expensive, you definitely don't want to call it on every request. Typically you would put the custom_rewrites_init and flush_rules in a plugin register_activation_hook function.
If you're lazy, you can just add it to your code once, make a request to the website (which will rewrite the .htaccess rewrite rules), then comment the flush_rules method out.
I'm trying to send a variable in the url. As I have the permalinks activated, I'm trying to add a rewrite rule, as said on a post I've read here...
Well nothing is working. This is the code I have on functions.php
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('init','flushRules');
// Remember to flush_rules() when adding rules
function flushRules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
$newrules = array();
$newrules['view-article/(.+)'] = 'index.php?pagename=view-article&aid=$matches[1]';
$finalrules = $newrules + $rules;
return $finalrules;
}
// Adding the var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
array_push($vars, 'aid');
return $vars;
}
//Stop wordpress from redirecting
remove_filter('template_redirect', 'redirect_canonical');
and this is the call to get the variable.
$aid = urldecode($wp_query->query_vars['aid']);
What am I doing wrong? :(
My wordpress version is 4.0 (The latest)
Try the following code:
add_action('init', 'flush_urls');
function flush_urls()
{
// Execute flush_rules() on every request can
// slow down your entire website
if('clean' !== get_option('flush_urls', ''))
{
flush_rewrite_rules();
add_option('flush_urls', 'clean');
}
}
add_action('init', 'register_rewrite_rules', 0);
function register_rewrite_rules()
{
add_rewrite_tag('%aid%', '([^/]+)');
add_rewrite_rule(
"view-article/([^/]+)/?",
'index.php?pagename=view-article&aid=$matches[1]',
"top"
);
}
Finally in your template-redirect or an any later action/hook you should do the following:
global $wp_query;
echo $wp_query->query_vars['pagename'];
echo $wp_query->query_vars['aid'];
In case you cannot see these variables inside the $wp_query, then try to save again the permalinks in your WordPress Dashboard. This will clean out the urls cache.
I'm creating a custom url rewrite structure for my WordPress site.
First I had the following code to have url: /objekt/<GID> which loads objekt page and have query var GID.
add_action( 'init', 'objekt_url_rewrites_init' );
function objekt_url_rewrites_init(){
add_rewrite_rule(
'objekt/([A-Za-z_0-9]+)/?$',
'index.php?pagename=objekt&GID=$matches[0]',
'top' );
}
add_filter( 'query_vars', 'objekt_url_query_vars' );
function objekt_url_query_vars( $query_vars ){
$query_vars[] = 'GID';
return $query_vars;
}
Now, I need to change URL structure to /en/objekt/<GID>. So, I change
objekt/([A-Za-z_0-9]+)/?$
to
en/objekt/([A-Za-z_0-9]+)/?$
But its not working!
When I visit /en/objekt/<GID> it just redirects to /en/objekt/ and not getting any query var named 'GID'