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.
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 an existing buddy press user profile link: e.g. https://example.com/members/joseph-bada
I need to make https://example.com/members/joseph-b an exact duplicate of it.
this my code simplified version:
add_filter('wp', 'custom_rewrite_rule');
function custom_rewrite_rule() {
global $wp_query, $wp_rewrite;
$slug = $wp_query->query_vars['name'];
if($slug==='joseph-bada') {
add_rewrite_rule('^members/joseph-b/?', 'members/joseph-bada', 'top');
$wp_rewrite->flush_rules();
}
}
but if i browse https://example.com/members/joseph-b - i get 404 error
UPDATE:
even after adding this in functions.php
add_action('init', 'custom_test');
function custom_test() {
global $wp_rewrite;
add_rewrite_rule('^members/joseph-b', 'members/joseph-bada', 'top');
$wp_rewrite->flush_rules();
}
https://example.com/members/joseph-b is still 404
can someone please point out what im missing?
UPDATE: i learned that https://example.com/index.php?bbp_user=joseph-bada&edit=1
leads to https://example.com/members/joseph-bada
so now i tried this:
add_action('init', 'custom_test');
function custom_test() {
global $wp_rewrite;
add_rewrite_rule('^members/joseph-b', 'https://example.com/index.php?bbp_user=joseph-bada&edit=1', 'top');
$wp_rewrite->flush_rules();
}
still no avail 404 though..
Disclaimer: I'm not a WP developer, but here goes.
It looks as though the $query part of the add_rewrite_rule needs to be explicit in its setting (as shown in the below example with the index.php string).
function custom_rewrite_rule() {
add_rewrite_rule('^nutrition/?([^/]*)/?','index.php?page_id=12&food=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
I also notice that you are doing an add_filter as opposed to add_action.
i gave up using add_rewrite_rule wordpress functionality.. instead, i studied buddypress plugin and how it works.. i discovered the following filters:
function custom_bp_domain_filter($domain, $user_id) {
$change_slug = is_joseph_bada_user_id($user_id);
if ($change_slug) {
$domain = trailingslashit(bp_get_root_domain() . '/' . 'members/joseph-b');
}
return $domain;
}
add_filter('bp_core_get_user_domain', 'custom_bp_domain_filter', 10, 2);
this part above is a filter wherein im gonna check if the user id being displayed belongs to members/joseph-bada. if it is, change it to members/joseph-b
function custom_bp_after_slug_filter($after_member_slug) {
if ($after_member_slug==='joseph-b') {
$after_member_slug = 'joseph-bada';
}
return $after_member_slug;
}
add_filter('bp_core_set_uri_globals_member_slug', 'custom_bp_after_slug_filter');
this above part determines if the URI is now members/joseph-b.. we dont have a user with that, instead it is 'joseph-bada'
the returned value here is the user to be displayed so we need to return 'joseph-bada' if the param value is 'joseph-b'
when members/joseph-b is browsed.. it will display the profile of members/joseph-bada
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've a little question about $wp_rewrite object in Wordpress...
I'm building a plugin which need some routes were not included in Wordpress. So i'm trying to use the non_wp_rules action from $wp_rewrite object.
Here is my code :
add_filter('generate_rewrite_rules', 'add_rewrites');
add_filter('wp_loaded', 'flush_rules');
function flush_rules()
{
$rules = get_option('rewrite_rules');
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function add_rewrites()
{
global $wp_rewrite;
$rules = array(
'documentation/$' => 'fileToCall.php'
);
$wp_rewrite->non_wp_rules += $rules;
}
But I don't understand... My route is added with any problem (just a var_dump on $wp_rewrite is saying to me all is okay), but it seems the file i'm trying to call (fileToCall.php) which just contain an echo for moment (so no errors) returns a 404 error in Firefox dev console.
Is there someone who have this problem someday? Does I am doing something wrong?
Thanks for help.
wp_rewrite test if redirect address is locale or external. In your case it is external, so you have to use a different function. Just replace your second hook by this:
add_action('init', 'add_rewrites');
function add_rewrites()
{
global $wp_rewrite;
$wp_rewrite->add_external_rule('documentation/$', 'fileToCall.php');
}
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' );