Wordpress - wp_rewrite rules - php

I'm attempting to write a plugin for WordPress but I'm having some issues with regards to the wp_rewrite functionality.
I want to make one page appear as its many by passing variables via the URL (such as: www.mysite.com/WordPress?variable=helloall)
However I want to keep the permalink structure intact, so i want the URL to appear as such:
www.mysite.com/WordPress/helloall
I then want to be able to take the slug and use that the search my database. (like you would using $_GET if I was using the general method i mentioned first)
I have found a few tutorials online and as of yet able to get this working. I believe my problem is due to a lack of understand with HOW you write the rules correctly.
I have used this tutorial:
http://www.prodeveloper.org/create-your-own-rewrite-rules-in-wordpress.html
and I have attempted to use the same code for the most part. I am able to set the rules, but they just dont seem to want to work for me
can anyone tell me the correct format to beable to do this?
Edit
this is my current function
function add_rewrite_rules( $wp_rewrite )
{
$new_rules = array
(
'(.?.+?)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
$wp_rewrite->preg_index(1).'&varname='.
$wp_rewrite->preg_index(2).'&page='.
$wp_rewrite->preg_index(3),
'(.?.+?)/(.*?)/?$' => 'index.php?pagename='.
$wp_rewrite->preg_index(1).'&varname='.
$wp_rewrite->preg_index(2)
);
// Always add your rules to the top, to make sure your rules have priority
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'add_rewrite_rules');
Solution
I have figured it out, I was going to post this as an answer but it seems I am unable to answer my own questions at this moment in time, so instead I'm editing the original post:
First off, the code I post above is correct, however the reason it was not working is because I was not flushing the rules, I do this with the following code:
function ebi_flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action( 'init', 'flush_rewrite_rules');
My new problem, was that my code worked a little to well, redirecting ALL pages instead of just the one I wanted, this meant that no child pages would display which is a bit of an issue, I have however solved the problem with one small edit:
function add_rewrite_rules( $wp_rewrite )
{
$new_rules = array
(
'(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
$wp_rewrite->preg_index(1).'&varname='.
$wp_rewrite->preg_index(2).'&page='.
$wp_rewrite->preg_index(3),
'(testpage)/(.*?)/?$' => 'index.php?pagename='.
$wp_rewrite->preg_index(1).'&varname='.
$wp_rewrite->preg_index(2)
);
// Always add your rules to the top, to make sure your rules have priority
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
So my final code regarding the wp_rewrite functionality is as follows:
function add_rewrite_rules( $wp_rewrite )
{
$new_rules = array
(
'(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
$wp_rewrite->preg_index(1).'&varname='.
$wp_rewrite->preg_index(2).'&page='.
$wp_rewrite->preg_index(3),
'(testpage)/(.*?)/?$' => 'index.php?pagename='.
$wp_rewrite->preg_index(1).'&varname='.
$wp_rewrite->preg_index(2)
);
// Always add your rules to the top, to make sure your rules have priority
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
function query_vars($public_query_vars)
{
$public_query_vars[] = "varname";
return $public_query_vars;
}
function ebi_flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action( 'init', 'flush_rewrite_rules');
add_action('generate_rewrite_rules', 'add_rewrite_rules');
add_filter('query_vars', 'query_vars');
I hope this saves someone else some time in the future.

I figured out how to get it working on e a specific page name, this is for anyone who is having issues in the future:
function add_rewrite_rules( $wp_rewrite )
{
$new_rules = array
(
'(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
$wp_rewrite->preg_index(1).'&varname='.
$wp_rewrite->preg_index(2).'&page='.
$wp_rewrite->preg_index(3),
'(testpage)/(.*?)/?$' => 'index.php?pagename='.
$wp_rewrite->preg_index(1).'&varname='.
$wp_rewrite->preg_index(2)
);
// Always add your rules to the top, to make sure your rules have priority
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
function query_vars($public_query_vars)
{
$public_query_vars[] = "varname";
return $public_query_vars;
}
function ebi_flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action( 'init', 'flush_rewrite_rules');
add_action('generate_rewrite_rules', 'add_rewrite_rules');
add_filter('query_vars', 'query_vars');

Related

Wordpress plugin to create virtual page using rewrite rules

I'm creating a plugin that will need virtual pages to output content on the front end.
Here is my code:
add_filter( 'generate_rewrite_rules', function ( $wp_rewrite ) {
$wp_rewrite->rules = array_merge(
['my-custom-url/?$' => 'index.php?custom=1'],
$wp_rewrite->rules
);
} );
add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = 'custom';
return $query_vars;
} );
add_action( 'template_redirect', function() {
$custom = intval( get_query_var( 'custom' ) );
if ( $custom ) {
include plugin_dir_path( __FILE__ ) . 'templates/states.php';
exit();
}
} );
In the plugin I have templates/states.php and in that file I have:
<?php
$state = get_query_var( 'custom' );
echo $state;
?>
When I visit localhost/my-custom-url/somevariable I get a page not found from Wordpress. I've tried flushing my permalinks.
As I said in the comments
I have never used generate_rewrite_rule
The documentation on it is pretty weak too. But this key looks just like I would expect a Regular expresion to be. And, that makes sense as that is how real Mod Rewrite and Htaccess work.
add_filter( 'generate_rewrite_rules', function ( $wp_rewrite ) {
$wp_rewrite->rules = array_merge(
['my-custom-url/?$' => 'index.php?custom=1'],
$wp_rewrite->rules
);
} );
So this $ here matches the end of the string, this means your URL must end with my-custom-url with an optional / because of the ?.
When I visit localhost/my-custom-url/somevariable I get a page not found
That's not surprising as your URL does not end in a way that will match that pattern.
Test it yourself!
So you can just remove the $ you may or may not want to keep the / optional.
add_filter( 'generate_rewrite_rules', function ( $wp_rewrite ) {
$wp_rewrite->rules = array_merge(
['my-custom-url/?' => 'index.php?custom=1'],
$wp_rewrite->rules
);
} );
One note is this will make that match anywhere in the URL
Test this one
Hope it works.

Rewrite customly generated urls in wordpress

I'm using Wordpress, where I have used my custom designed template, so I'm using custom generated URL's in most cases.
I want my URL to look like: website_url/details/191/
I want to rewrite this to more SEO friendly. I don't know how to write rewrite rules in htaccess and could use your help.
add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );
// flush_rules() if our rules are not yet included
function my_flush_rules(){
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['(details)/(\d*)$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
$newrules = array();
$newrules['(details)/(\d*)$'] = 'index.php? pagename=$matches[1]&id=$matches[2]';
return $newrules + $rules;
}
// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars )
{
array_push($vars, 'id');
return $vars;
}
This is what I have added in my function.php but no change.

Custom query var in wordpress

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.

Wordpress $wp_rewrite

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');
}

Passing a URL variable to category.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' );

Categories