55

Add this to your theme's functions.php, or put it in a plugin. add_action( 'init', 'wpse26388_rewrites_init' ); function wpse26388_rewrites_init(){ add_rewrite_rule( 'properties/([0-9]+)/?$', 'index.php?pagename=properties&property_id=$matches[1]', 'top' ); } add_filter( 'query_vars', 'wpse26388_query_vars' ); function ...


19

A) The Base in Core As you can see in the Codex Template Hierarchy explanation, single-{$post_type}.php is already supported. B) Extending the core Hierarchy Now there're gladly some filters and hooks inside /wp-includes/template-loader.php. do_action('template_redirect'); apply_filters( 'template_include', $template ) AND: a specific filter inside ...


13

You should be using the template_include filter for this: add_filter('template_include', 'wpse_44239_template_include', 1, 1); function wpse_44239_template_include($template){ global $wp_query; //Do your processing here and define $template as the full path to your alt template. return $template; } template_redirect is the action called ...


10

So the template_redirect is used for things such as canonicalisation, feeds etc. If you want to alter the template that is served template_include is preferred. Unlike template_redirect, template_include is a filter which filters the path of the template page. This means you don't load/include the template, but just return the template path. WordPress does ...


9

Another way to do it: add_action('init', function() { add_rewrite_rule( '^properties/([0-9]+)/?', 'index.php?pagename=properties&property_id=$matches[1]', 'top' ); }, 10, 0); add_action('init', function() { add_rewrite_tag( '%property_id%', '([^&]+)' ); }, 10, 0); Codex Rewrite API/add rewrite ...


4

redirecting not logged in user is the only thing that works :) in the backend. What you are describing is very peculiar. template_redirect is a front-end hook. Your code will never run on the backend, as far as I can tell. You can demonstrate that with this line: add_action('template_redirect',function() {die;}); You can traverse the back-end all you ...


4

Following the Template Hierarchy image, I don't see such an option. So heres how i'd go about it: Solution 1 (Best in my opinion) Make a template file and associate it to the review <?php /* Template Name: My Great Review */ ?> Adding the template php file in your theme directory, it would appear as a template option in your post's edit page....


4

You can use template_redirect. Here a simple redirection function. add_action( 'template_redirect', 'se219663_template_redirect' ); function se219663_template_redirect() { global $wp_rewrite; if ( is_search() && ! empty ( $_GET['s'] ) ) { $s = sanitize_text_field( $_GET['s'] ); // or get_query_var( 's' ) $location = '/'; ...


4

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 add_action('template_redirect','custom_shop_page_redirect'); function custom_shop_page_redirect(){ if (class_exists('WooCommerce')){ if(is_product()){ ...


3

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 your theme. Additional information: The article »Don’t use template_redirect to load an alternative template file« by Mark Jaquith, where he says, in a nutshell,...


2

Based on your comments, I think I get what you're saying. However, you don't want to redirect on a 404, since it defeats the object of the 'Not Found' header. However, you can filter the 404 template path and pass back your own; function __custom_404( $standard_404 ) { if ( something_is_true() ) return '/absolute/path/to/custom.php'; return ...


2

This is absolutely possible. By default, the category archive page will be at domain.com/category/the_category. The file that is typically used for this rendering is category.php, which is located in your theme directory. To remove the /category/ part, so that your category archives are at domain.com/the_category, you can use the SEO Ultimate plugin. After ...


2

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(); $single_postType_postName_template = locate_template("single-{$object->post_type}-{$object->post_name}.php"); if( file_exists( $...


2

I couldnt find a better method other than: /* * Restrict non logged users to certain pages */ add_action('template_redirect','my_non_logged_redirect'); function my_non_logged_redirect() { if ((is_page('mi-perfil') || is_page('agregar-empresa')) && !is_user_logged_in() ) { wp_redirect( home_url() ); die(); } }


2

As @s_ha_dum said, shortcodes are views, a late result of a query, not its precondition. The only way to do what you are asking for would be parsing the shortcode long before the query is set: on save_post for example. Then you would have to evaluate the arguments for the query filter, store them in a separate option and use that option value in ...


2

By the time your shortcode runs on the the_content hook (usually) the main query has executed, the pre_get_posts hook has fired for the main query, and the template_redirect hook has fired. You won't be able to hook to template_redirect or alter the main query (if that is what you need) without somehow getting your shortcode to run much earlier than ...


2

template_redirect runs before template_include. You can demonstrate that with this: add_filter( 'template_include', 'var_template_include', 1000 ); function var_template_include( $t ){ echo __FUNCTION__; } // use template redirect to include file add_action('template_redirect', 'ra_template_block'); function ra_template_block() { echo __FUNCTION__; } ...


2

First, don't hack core files (unless you intend to submit a patch :) ). Second, what you are doing isn't really checking to see if the title exactly matches the search string. What you are doing it checking to see if there is only one result. That doesn't mean the title matches. Undo the core hack, then do this: function redir_title_match($a) { if (...


2

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.php directly into their browser, but it should affect the login links displayed throughout your site. This is the example code from the login_url Codex page: ...


1

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 page.php template will give you a page, when it's actually the other way around. I strongly recommend reading about the template hierarchy, it should answer your ...


1

I solved my problem by adding in my function.php this function: add_filter('single_template', create_function( '$the_template', 'foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") ) { return TEMPLATEPATH . "/single-{$cat->slug}.php"; } ...


1

Try using a custom post type (named "products") and rename your single.php file to single-products.php. It should be much easier like this. The plugin below (free) should help you create a custom post type without any coding. http://wordpress.org/plugins/custom-post-type-maker/


1

What your function says: On FRONT END do this before template display:      if user is logged in:           check if you are NOT on admin page (back end), if you are not on admin page:               , go ...


1

You are essentially trying to create a "fake" page without having to create a physical page in the WordPress database and for that, you will need custom re-write rules. For more details see my answer here: Setting a custom sub-path for blog without using pages? Quick overview: step 1: setup your custom rewrite rules add_action('init', 'fake_page_rewrite'...


1

Make sure you have wp_footer() added to your footer.php file


1

If CSS is not outputting inside wp_head, it may imply that they're not being enqueued correctly. See here for more info. Maybe they're hardcoded in the theme file? In header.php? As for scripts, my guess is that they are being enqueued and set to trigger during footer actions, so the fact that you're not calling wp_footer() means that those actions never ...


1

I just want to say thanks, I was able to use this and do exactly what I hoped changing the line to cover the category and sending people to my login page instead of the home url, /* * Restrict non logged users to certain pages */ add_action('template_redirect','my_non_logged_redirect'); function my_non_logged_redirect() { if ((in_category(1) &&...


1

The posts/pages which you want to hide from non logged in users can be published as "Private" and it will not be available to any public visitor of the site. This way you don't need to implement any sort of redirection or custom code. You can find more information here.


1

This is what I ended up using (for all those interested): function override_tax_template($template){ // is a specific custom taxonomy being shown? $taxonomy_array = array('tax1','tax2'); foreach ($taxonomy_array as $taxonomy_single) { if ( is_tax($taxonomy_single) ) { if(file_exists(trailingslashit(...


1

Use Page Templates Another approach for scalability would be to duplicate the page template drop-down functionality on the page post type for your custom post type. Reusable Code Duplication in code is not a good practice. Overtime it can cause severe bloat to a codebase when then make it very difficult for a developer to manage. Instead of creating a ...


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