Wordpress - URL wild card id loads root permalink - wordpress

I have a Wordpress page called "Design" with the permalink "/design"
It loads as expected. What I am trying to do is to tell wordpress or my htaccess file that if I want to go to a url like:
domain.com/design/123abc
It will still load the /design permalink but keep the "123abc" appened to the url. I am not looking for a redirect. I just want wordpress to load the page and ignore everything after "/design/"
Is this possible?

Found my answer. Added this script in my child theme functions.php
24565 is the id of the /design page
function custom_rewrite_basic() {
add_rewrite_rule('^design/*', '/index.php?page_id=24565', 'top');
}
add_action('init', 'custom_rewrite_basic');

Related

How to create custom route to a page in WordPress

I want to set a new Route in WordPress that will be redirected to the desired path by entering a specific pattern.
for example:
When I enter any url like below:
http://mywebsite.com/audio/Everything
http://mywebsite.com/audio/**********
http://mywebsite.com/audio/0123456789
These routes and similar ones redirects to sound.php
http://mywebsite.com/wp-content/sounds/sound.php
You can use add_rewrite_rule function, Here is official WP reference - https://developer.wordpress.org/reference/functions/add_rewrite_rule/
As per your example above, You can do like below,
add_action('init', 'custom_rewrite_rule', 10, 0);
function custom_rewrite_rule() {
add_rewrite_rule('^audio/([^/]*)/?','wp-content/sounds/sound.php','top');
}
Try above code as per your requirement and add into your theme's
function.php OR plugin file.
After code added, You must Go to Settings > Permalinks page on your site admin panel and do click on Save Changes button.

Wordpress add action 'init' not working frontend

I have installed Wordpress without making any changes and using the standard 2019 theme. I want to create virtual pages when ever specific url patterns are called. To do this I need to run a URL rewrite as early as possible, so I have added this line to the top of the themes functions.php file:
function url_rewrite() {
die('here');
}
add_action('init', 'url_rewrite');
I get the message 'here' as expected on pages, but if its a URL which does not exist the site is not picking this up at all, it just goes to a 404 page. I'm hoping someone knows why.
I have also tried:
add_action('init', 'url_rewrite', 1, 0);
its working for me perfect, can you please check as you have added code with theme selected and once update your permalink in backend

Wordpress pagination stops working after page 10

For some reason after getting to page 10 in my blog(ex: /page/10) -- the page goes to a 404.
When go to example.com/page/11 it redirects me here:
https://example.wpengine.com/blog/pagehttps:/example.wpengine.com/
Cannot figure out what is going on here.
Put this code on function.php file:
/**
* Fix pagination on archive pages
* After adding a rewrite rule, go to Settings > Permalinks and click Save to flush the rules cache
*/
function my_pagination_rewrite() {
add_rewrite_rule('blog/page/?([0-9]{1,})/?$', 'index.php?category_name=blog&paged=$matches[1]', 'top');
}
add_action('init', 'my_pagination_rewrite');
Replace blog with your category name in the code above.
After adding this code, You can go to Settings > Permalinks
After, click Save to flush the rules cache, or else the rule will not be applied.
Get the more details about the solution, you can follow the article - Fixing a WordPress pagination 404 error

Want to redirect a Wordpress URL to a specific page template and pass name of post

I've got a Wordpress site set up with a custom post type and custom taxonomy and custom URLs. However when I go to:
/custom-post-type/custom-taxonomy/custom-taxonomy-sub/postname
It 404s. I've tried everything and just want to create a redirect that grabs the postname and passes it to single-custom-taxonomy.php without changing the URL.
Some sort of redirect with a regex like /custom-post-type/(.*)/(.*)/(.*) and pass the third match to single-custom-taxonomy.php as a variable so it can get the post page.
How do I do this?
Figured it out:
function custom_rewrite_basic() {
add_rewrite_rule('^knowledge-center/(.*)/(.*)/(.*)', 'index.php?pagename=post_type=questions&questions=$matches[3]', 'top');
}
add_action('init', 'custom_rewrite_basic');

Rewrite category wordpress

Is it possible to rewrite only one category ?
I have a category "Photos" and just for this category.
I just want to rewrite it from /category/photos to /photos
You can do it with WP Rewrite API.
Add this to functions.php
add_action('init', 'register_rewrites');
function register_rewrites() {
add_rewrite_rule('^photos$', 'index.php?category_name=photos','top');
}
Remember to reload your rewrite settings: go to Settings -> Permalinks and click on Save button - no changes needed.
The Yoast SEO plugin has this function build in, I recommend this plugin in general.
no-category-base-wpml only does this
If you don't want to install a plugin for this. Around the web there are many tutorials which do this:
http://thisismyurl.com/6704/remove-category-url-wordpress/
http://www.webdevtuts.net/php/how-to-remove-category-from-wordpress-url-structure/
http://www.wprecipes.com/how-to-remove-category-from-your-wordpress-url
Too enable this for just one category I would advise the following:
create a page called photos.
create a theme template page for this photo page
In is set up a new query_posts* getting the category 'photos'
Include the category.php file.
assign that template to the photo page.
Not tested but should work. Because the page has the correct URL and should include the category. Questions, aks.
*query_posts is bad for performance if you want to do it totally correct use the pre_get_posts filter. It's requires more knowledge.

Resources