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.
Related
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.
I am trying to make a custom URL like "mysite.com/artist/bruno-mars" with a custom template "artist.php", which is in the theme folder.
Here is the "artist.php" template code for example
<?php
get_header();
//my custom code goes here //
get_footer();
?>
And now in the head of the theme functions.php, I put the code
/**
* Custom rewrite
*/
add_action( 'init', 'ic_rewrite_rule' );
function ic_rewrite_rule() {
add_rewrite_rule( 'artist/([^&]+)', 'index.php?artist=$matches[1]', 'top' );
}
/**
* Custom query vars
*/
add_filter( 'query_vars', 'ic_register_query_var' );
function ic_register_query_var( $vars ) {
$vars[] = 'artist';
return $vars;
}
/**
* Custom page template
*/
add_action( 'template_redirect', 'ic_url_rewrite_templates' );
function ic_url_rewrite_templates() {
if ( get_query_var( 'artist' ) )
add_filter( 'template_include', function() { return get_template_directory() . '/artist.php'; });
}
But this is not working. It is showing 404 error. Can anybody help me out.
Here is an example - Custom rewrite rules in Wordpress but not sure, If that might work.
UPDATE
I figured it out. Here is code for functions.php in theme folder.
add_filter('query_vars', 'add_artistname_var', 0, 1);
function add_artistname_var($vars){
$vars[] = 'artistname';
return $vars;
}
add_rewrite_rule('^artist/([^/]+)/?$','index.php?pagename=artist&artistname=$matches[1]','top');
I'm trying to implement a custom JSON API upon Wordpress, like /api3/get_posts, I refer to the implementation of plugin JSON-API, but it just doesn't work. Below is my code:
$myPluginBase = 'api3';
function my_plugin_init() {
global $wp_rewrite;
add_filter('rewrite_rules_array', 'my_plugin_rewrites');
add_action('template_redirect', 'my_plugin_template_rewrite');
$wp_rewrite->flush_rules();
}
function my_plugin_template_rewrite(){
global $myPluginBase;
if( isset( $_REQUEST[ $myPluginBase ] ) ){
echo $_REQUEST[ $myPluginBase ];
exit;
}
}
function my_plugin_activation() {
// Add the rewrite rule on activation
global $wp_rewrite;
add_filter('rewrite_rules_array', 'my_plugin_rewrites');
$wp_rewrite->flush_rules();
}
function my_plugin_deactivation() {
// Remove the rewrite rule on deactivation
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function my_plugin_rewrites($wp_rules) {
global $myPluginBase;
if (empty($base)) {
return $wp_rules;
}
$my_plugin_rules = array(
"$myPluginBase\$" => "index.php?{$myPluginBase}=info",
"$myPluginBase/(.+)\$" => "index.php?{$myPluginBase}=\$matches[1]"
);
return array_merge($my_plugin_rules, $wp_rules);
}
// Add initialization and activation hooks
add_action('init', 'my_plugin_init');
register_activation_hook( __FILE__, 'my_plugin_activation');
register_deactivation_hook( __FILE__, 'my_plugin_deactivation');
It seems like the template_redirect action works, as I open /api3=hello, the server will send hello back. But if I try to open /api3/hello, it keeps showing me the home page.
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' );
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');