Articles of template redirect

template_redirect to accompany with a shortcode

In one of my plugin’s shortcode I’m handing user login / registration on the fly with a post submission. So the whole thing is happening within the shortcode like: <?php function this_shortcode() { ob_start(); //if isset(login)… process login, get $user_id //else if isset(registration)… process registration, get $user_id //else if already registered, get $current_user->ID as $user_id […]

Template Redirect to /2015 to use page template

We work with non-profits and come across this issue a lot. We need to use a page template for the url structure domain.com/2015 where we’ll be adding an “end of year” report. We’re not using the typical date archives to list blog posts. I’m not sure how I’d redirect the template based on this. My […]

how to show post content with post name in sidebar?

I started working on a wordpress website, I need to show 20 words from every post with post name in Sidebar. I am new to wordpress, I am finding difficult to do it.

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

I need to build a offline page and first idea was to simply get desired page ID and do wp_redirect but that leaves a URL of the page to redirect to. function _action_offline_mode( $template ){ if( is_admin()){ return; } $offline = get_option(‘offline’,’inactive’); $off_page = get_option(‘off_page’,null); if(‘active’ == $offline && !empty($off_page)){ $user = wp_get_current_user(); $allowed_roles = […]

How should I approach changing the template & $query as part of a shortcode's execution?

I am working on a project where I need to be able to change the template being used if a given shortcode is used. To complicate things more, the plugin must also be able to rewrite $query if this same shortcode is called. I believe that I am running into issues in when various parts […]

!is_admin() condition turns true in admin pages

I need users to redirect to login if not logged and once they are, redirect them to backend if they try to see front end pages. This is my code: add_action( ‘template_redirect’, function () { if(is_user_logged_in() ): bk1_debug(‘logged in’); if (!is_admin()): bk1_debug(‘not in admin, redirecting…’); wp_redirect(admin_url()); endif; else: bk1_debug(‘not logged in, redirecting…’); wp_redirect(wp_login_url( admin_url() )); […]

Common single page template options

I am developing a common theme for 25 websites, the default requirements are done. Each site has got different single post template. I need to group it as a single theme so i don’t need to hard-code every time and maintain a common platform. The various single posts template are normal-blog / tab structure / […]

Different Template based on HTTP Referer

He Guys, i’ve been looking for a way to show a different Page-Template based on HTTP Referer or maybe the User Agent or what ever. I’m planning an Image-Gallery which should be shown in a Fancybox. My Idea is to use a template which contains only the loop and all the stuff i need to […]

Pre-Populate $wp_query settings with custom rewrite rules and custom template_redirect

I’m currently working on a file browser plugin which requires frontend editing capabilities for a registered user. For this scenario, I have registered some custom rewrite rules for my custom post type. Rewrite Analyzer As you can see in the screenshot above, the plugin has a basic CRUD interface for managing the downloads. The routing […]

Force HTTPS 301 redirect with hook

I am trying to force a 301 https redirect with hooking into the ‘template redirect‘ function. I want not doing it a htaccess because when I update my permalinks, htaccess with https redirect disappears. So with this function I want to do a redirect if the url is loaded with ‘http’, but nothing happens. Is […]