11 votes

How can I create custom URL routes?

Another way to do it: add_action('init', function() { add_rewrite_rule( '^properties/([0-9]+)/?', 'index.php?pagename=properties&property_id=$matches[1]', ...
Christian Lescuyer's user avatar
8 votes
Accepted

How do I redirect /search/ to wordpress search template?

You can use template_redirect. Here a simple redirection function. add_action( 'template_redirect', 'se219663_template_redirect' ); function se219663_template_redirect() { global $wp_rewrite; ...
Jevuska's user avatar
  • 1,160
5 votes
Accepted

Redirect the single product page link to the shop page

You can try using template_redirect action hook to check if the current page is product page and after that, you can redirect the user to your shop page. Paste this code into your functions.php ...
Harry's user avatar
  • 151
3 votes
Accepted

How to correctly detect accessing wp-content/uploads?

WordPress does not serve those files, when you request a PDF it's Apache/Nginx that serves the file to the browser, no PHP is executed. This is a good thing as it's much faster than PHP and saves a ...
Tom J Nowell's user avatar
  • 58.2k
3 votes
Accepted

How to override a query and display specific page by ID?

You could do this a few different ways, depending on what's happening in the template and how you are able to identify which page you want to override. 1. Simple method with pre_get_posts: This will ...
Milo's user avatar
  • 78.1k
3 votes

redirect wp-login.php to another page

If you're only concerned about login links displayed on your pages, you should be able to modify the URL by hooking the login_url filter. This won't redirect a user that types http://MYSITE/wp-login....
thirdender's user avatar
3 votes

Change template dynamically

Just in case someone comes here to copy pasta without looking at the codex. THE APPROPRIATE FILTER IS »template_include« See: Codex - Filter Reference As Radek pointed out that method can destroy ...
Jeffrey Baldridge's user avatar
3 votes

single-{$post_type}-{slug}.php for custom post types

The top answer (from 4 years ago) no longer works, but the WordPress codex has the solution here: <?php function add_posttype_slug_template( $single_template ) { $object = get_queried_object();...
skladany's user avatar
2 votes

!is_admin() condition turns true in admin pages

What your function says: On FRONT END do this before template display:      if user is logged in:           check if you ...
jave.web's user avatar
  • 477
2 votes

force download a file in template_redirect hook

The solution is in fact very simple. Just use the action admin_post_(action)... add_action( 'admin_post_export_page', 'export_page' ); function export_page() { // any code you want } than you ...
superfive33's user avatar
2 votes
Accepted

Why does the $_GET parameter ?search forward the front-page to the archive/blog page

search is a reserved term, and should not be used as a query variable. The presence of any reserved query variable tells WordPress that the current query must be for something other than the front ...
Jacob Peattie's user avatar
2 votes

Custom pagination structure

After searching here and there, probably I found solution. (Don't know if I am doing wrong in WP terminology!) Page was redirecting from .../page5 to .../page/5, because of redirect_canonical ...
Parixit's user avatar
  • 133
1 vote
Accepted

Load a specific post if user tries to access 404 page

This is the final code that works. I had to set $wp_query->is_single = true; manually: add_action( 'template_redirect', function(){ global $wp_query; if ( is_404() ){ $id = 1; // ...
Roz's user avatar
  • 21
1 vote

Redirect Users who aren't logged in, aren't post authors and aren't admin. Frontend

get_user_id() is not a function so you'll get an error for that. Additionally, you can get the author id by calling the $post global and accessing the property post_author add_action( '...
Howard E's user avatar
  • 733
1 vote
Accepted

Page get parameter doesn't work with is_single() function

The truth is when send_headers action is processing, the $wp_query isn't ready yet and you can't use is_single() function. But you can use it a bit later. There is a parse_query action, that can help ...
Altkov's user avatar
  • 26
1 vote

How to redirect home page to another page after login for all user?

By adding this to your theme functions.php file: function login_redirect( $redirect_to, $request, $user ){ return home_url('/'); } add_filter( 'login_redirect', 'login_redirect', 10, 3 );
Paul1427's user avatar
1 vote
Accepted

Redirect all child taxonomy to its parent

You can check if your $term have parent and then redirect using wp_redirect() <?php $term = get_queried_object(); $parent = ( isset( $term->parent ) ) ? get_term_by( 'id', $term->...
mrcodefinger's user avatar
1 vote

Redirect specific page in Wordpress for first time visit

I would always hook it to template_redirect. Something like this: add_action( 'template_redirect', 'custom_check_for_redirect' ); function custom_check_for_redirect(){ global $wp_query; # It could ...
Ruvee's user avatar
  • 154
1 vote
Accepted

how to block direct access to multiple thank you pages?

I have solved the issue by myself. I am posting the solution here in case if someone gets the same problem. function wpse15677455_redirect() { $ref = wp_get_referer(); if (is_page(1911) &&...
Ujjawal Poonia's user avatar
1 vote
Accepted

wp_enqueue_style on template_redirect level?

The reason to use wp_enqueue_style is to allow WordPress to manage dependencies between stylesheets. Since you're only outputting a barebones page here, there really is no point to use it. That said, ...
cjbj's user avatar
  • 14.1k
1 vote

Custom URL routes

All internal rewrite rules must point to index.php. This isn't a theme file, it's the main bootstrap file in the root of your WordPress install. So, your rule should look like: add_action( 'init', '...
Milo's user avatar
  • 78.1k
1 vote

Redirect to first child in menu

As @WebElaine said, menu_order sorts by Page Order. So, you have to update your pages and set the order manually.
Rachid Chihabi's user avatar
1 vote
Accepted

stop redirection on /wp-admin call to /wp-login

thanks to Tomasz Struczynski who has explained and answered my question completely You Can See The Answer At This Link First - explanation. Wordpress is kind of tricky, when it comes to admin ...
Poofy's user avatar
  • 69
1 vote

how to show post content with post name in sidebar?

This is a little broad and more than one approach is possible. If you want to show posts in general sidebar area you would need to: Find and edit a template file responsible for that output in your ...
Rarst's user avatar
  • 99.3k
1 vote
Accepted

Template Redirect to /2015 to use page template

If you really want to override the yearly archive for 2015, then you can try the following: add_filter( 'template_include', function( $template ) { //------------------------- // Edit this ...
birgire's user avatar
  • 66.7k
1 vote

Category link redirect to custom template page instead of index.php?

Your question implies that index.php is a page to be redirected to, it isn't, it's purely a template file. It implies that single.php being loaded is what makes a page singular, or that loading the ...
Tom J Nowell's user avatar
  • 58.2k

Only top scored, non community-wiki answers of a minimum length are eligible