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
Related
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.
So here's the problem: We don't like the fact that WordPress doesn't allow duplicate slugs, even for sub categories meaning we cannot have urls like:
product-1/guides
product-1/articles
product-2/guides
product-2/articles
That's very annoying! One solution we are considering is setting up our slugs like this:
product-1/product-1-guides
product-1/product-1-articles
product-2/product-2-guides
product-2/product-2-articles
But in our htaccess - can we use it to pick up such urls and rewrite them as prettier urls which have the product name removed from the sub folder? We don't mind hard coding these as we'll only ever have 5-10 products on the site.
This would keep the WordPress install happy with unique slugs, but the SEO tick in the box with better looking urls.
I just need a hand with the syntax please?
EDIT 1:
After looking at the WordPress Rewrite API, I'm failing to get anywhere with what I think is a really simple test. I have the following code in my functions.php which is running as I tested an echo, but no rewriting is taking place?
add_action( 'init', 'productRewrites' );
function productRewrites() {
add_rewrite_rule('^wordpress/james?','index.php?author_name=jwilson','top');
}
Nothing happens when I hit:
mysite.com/wordpress/james
Edit 2:
Cool I realise I now have to click save each time. The problem I now have is the following does not work not when I use $matches[1] - it only works if I hard code the author_name value (to jwilson for example):
function productRewrites() {
add_rewrite_rule(
"writer/([^/]+)/?",
"index.php?author_name=$matches[1]",
"top");
}
When I use $matches[1] it just returns everything! So clearly isn't using ([^/]+) in the url?!
you have to reset permalink structure
in order to do that, move to Settings -> Permalinks and press Save changes button
I'm trying to get browser-sync to work with my multipress wordpress install, for simpler mobile / responsive development.
Currently I'm having problems in that, my normal development takes place at local.example.com, and browser-sync is proxying this (via 123.456.78.9:3202, as per browser sync).
So far browser-sync loads the site, but none of my scripts or CSS are loading (although images load fine). They jsut fail with no response in the network panel.
I'm using NGINX for hosting the site, as opposed to apache.
Does anyone have any wordpress browser-sync experience? Am I missing something with the browser-sync set up? And tips for this would be super welcome. I'd love to get this as a solid part of my work flow.
The problem is to do with how wordpress handles URLs, in that it normally uses full URLs for including content and links etc.
The proxy is trying to access these on another domain and that's why it's failing.
Update
A much simple, cleaner and maintainable strategy, that also helps with development environments is to use the Root Relative URLs plugin. Adds hooks and configs similar to below, but also updates your content and editors to apply the same structure, so it's a bit more robust
Original Answer
You can add a simple hook (source: wordpress relative urls) to filter wordpress generated urls and remove the base domain so you get relative links to styles and posts etc:
$relative_url_filters = array(
'script_loader_src', //js
'style_loader_src', //css
'post_link', // Normal post link
'post_type_link', // Custom post type link
'page_link', // Page link
'attachment_link', // Attachment link
'get_shortlink', // Shortlink
'post_type_archive_link', // Post type archive link
'get_pagenum_link', // Paginated link
'get_comments_pagenum_link', // Paginated comment link
'term_link', // Term link, including category, tag
'search_link', // Search link
'day_link', // Date archive link
'month_link',
'year_link'
);
foreach ( $relative_url_filters as $relative_url_filters ) {
add_filter( $relative_url_filters, 'wp_make_link_relative' );
}
Which should clean up most of your issues and get browser-sync working nicely.
I'm still having some issues where I have more complex inclusions for images, but more or less it's working and we're already seeing how cool it is!
Here's my issue. My company needs a vendor database added to our wordpress website. None of the existing plugins will even come close to what we need, and we already have a mysql database with all of our information, so we need to create a plugin or something to do what we need.
These urls need to be direct-accessible and have SEF urls. So, for example:
mysite.com/vendors/
mysite.com/vendors/pipe-manufacturers/
mysite.com/vendor/bobs-pipes/
And, the custom content needs to appear inside the wordpress template.
There are really 2 options:
1) Find a way to write our application outside of wordpress, but find a way to bootstrap wordpress to show the header, footer, and sidebar.
2) Run the app from inside wordpress.
So I went for option #2. I created a new template file named "vendor.php", and began working. I added this code to my functions.php of my theme:
add_filter( 'template_include', 'xyz_template_check' );
function xyz_template_check() {
global $template;
$rqst = $_SERVER['REQUEST_URI'];
$ra = split("/", $rqst);
if ($ra[1] == "vendors") {
$template_file = get_stylesheet_directory() . '/vendors.php';
return $template_file;
}
return $template;
}
So what the above code does, if it sees the word "vendors" as the first part of the url after the site name, it sends you to vendor.php. This works PERFECTLY....
except...
Wordpress believes that the page is not found. It returns a 404 header, and NOT FOUND into the page title and breadcrumb.
Adding a PAGE called "Vendor Database" with the permalink "/vendors/" fixes the main page. But there will be literally hundreds of vendors and different categories. I cant be creating a custom page for each one. This needs to be dynamic.
So, how do I make wordpress give a 200, and supply an acceptable page title, breadcrumb, etc.
Don't even get me started on the danged wp_title filter. This did NOT work as documented. Although, it just occurred to me that this might be an issue with Wordpress SEO (the wp_title filter issue).
Anyone got an idea on this?
Ok got this. The solution was to use the rewrite api, as mentioned above, to look for the pattern /vendors/, letting it know that it was a valid URL. Coupled with my existing template override, this is what I needed.
I have used this code in my themes function.php file, to pass custom argument to a page in wordpress but it is not working. I am getting 404 page not found. What am I doing wrong?
function photo_details_vars()
{
add_rewrite_rule('^photo-details/([^/]*)/?','index.php?p=20&id=$matches[1]','top');
}
add_action('init', 'photo_details_vars');
I am new to wordpress. I found this idea from here - http://codex.wordpress.org/Rewrite_API/add_rewrite_rule please be nice and direct me to a solve.
Additionally how can I catch this url variables value from the template file of page no. 20
How should I use add_rewrite_tag() for this rewrite rule?
Have you registered the query variable using add_rewrite_tag()? otherwise WordPress will not recognize the variable.
Additionally how can I catch this url variables value from the template file of page no. 20??
if I understand it correctly, are you asking about this: $_GET['p']?