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(); }
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 accepted a new challenge making a custom Wordpress plugin.
After doing some research I started creating clean urls for the plugin and collected the code below.
When I visit the url https://mysite/estates/list/test/baarle/
The first custom var/rewrite_tag "jebamodus" is displayed with a value "list" in the $wp_query->query_vars array. This one's good.
The second and/or third var/rewrite_tag isn't displayed/registered?
Do I need to init each var/rewrite_tag with rewrite url seperated or are there some faults in my code?
I feel like I almost got it to work, but can't find what's wrong.
Some fresh eyes needed.
function wpse_205120_query_vars( $qvars ) {
$qvars[] = 'jebamodus';
$qvars[] = 'jebatest';
$qvars[] = 'jebaregion';
return $qvars;
}
add_filter( 'query_vars', 'wpse_205120_query_vars' );
add_action( 'init', function() {
add_rewrite_tag( '%jebamodus%', '([^/]+)' );
add_rewrite_tag( '%jebatest%', '([^/]+)' );
add_rewrite_tag( '%jebaregion%', '([^/]+)' );
add_rewrite_rule( '^estates/([^/]+)', 'index.php?jebamodus=$matches[1]', 'top' );
add_rewrite_rule( '^estates/([^/]+)/([^/]+)', 'index.php?jebamodus=$matches[1]&jebatest=$matches[2]', 'top' );
add_rewrite_rule( '^estates/([^/]+)/([^/]+)/([^/]+)', 'index.php?jebamodus=$matches[1]&jebatest=$matches[2]&jebaregion=$matches[3]', 'top' );
flush_rewrite_rules(true);
} );
add_filter('template_include', function($template){
global $wp_query;
if( isset($wp_query->query_vars['jebamodus'])) {
echo "<pre>";
print_r($wp_query->query_vars);
wp_die();
}
return $template;
});
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 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'
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' );