• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

template_redirect and switching templates dynamically

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › template_redirect and switching templates dynamically

This topic is: not resolved

Tagged: CPT, hooks, template_redirect

  • This topic has 11 replies, 3 voices, and was last updated 10 years, 10 months ago by marcus.
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • September 10, 2014 at 10:27 am #123757
    marcus
    Member

    I'm the author of the Events Manager WordPress plugin. In our plugin we have a handy little feature which allows users to choose which template to show our events/locations as, which are custom post types and WP doesn't let you choose templates in CPTs

    What we do to achieve this is by hooking into single_template (same lines as template_redirect, either will work) and switch the chosen template file that'll be loaded by WordPress with the one chosen in our settings.

    This works in most cases, but it seems that Genesis makes that decision within the framework and modifies which file is loaded later in the process. Our settings page does correctly identify the right template/filename it just doesn't take when making the switch.

    I'm wondering if anyone experienced with Genesis has any idea on how to swap templates during the loading process, which will avoid users from having to add snippets removing actions, like this - http://pastebin.com/ynK9YkJb

    Cheers!
    Marcus

    September 10, 2014 at 10:53 am #123764
    Brad Dalton
    Participant

    Couldn't you add a check for Genesis in your plugins code?

    Or check for genesis and then include a special template for genesis.


    Tutorials for StudioPress Themes.

    September 10, 2014 at 11:17 am #123769
    marcus
    Member

    Thanks for the reply. I could do that, I see there's some constants I could check for like PARENT_THEME_NAME == 'Genesis' (is that a good way?)

    The problem still remains that I don't know how to switch template files the genesis way even if I know Genesis is being used.

    September 10, 2014 at 1:04 pm #123798
    Brad Dalton
    Participant

    If you look at the way Genesis checks for SEO plugins in genesis > lib > functions > seo.php you'll see how its done.

    You might also like to check the Genesis WooCommerce Connect plugin to see how they detect if WooCommerce is active and how they load the 3 templates which are included in the plugin to make WooCommerce work with genesis.


    Tutorials for StudioPress Themes.

    September 11, 2014 at 1:23 pm #124004
    marcus
    Member

    If you look at the way Genesis checks for SEO plugins in genesis > lib > functions > seo.php you’ll see how its done.

    For this I assmue you're referring to checking if Genesis is active? I guess checking for a core function may be even better...

    Thanks for the Connect plugin reference, that's very handy. I had a go at copying what they do but it doesn't take (the hooks are fired and the right templates are passed on). I'll set up WC and this plugin to see it in action and will investigate further asap, if anyone has any suggestions meantime, much appreciated!

    
    add_action( 'after_setup_theme', 'em_genesis_single_template' );
    function em_genesis_single_template(){
    	if( get_option('dbem_cp_events_template') ){ //override default template
    		add_filter('template_include',array('EM_Event_Post','single_template'), 20); //function to do the overriding
    	}
    }
    
    September 11, 2014 at 2:28 pm #124016
    Brad Dalton
    Participant

    Or checking for a PHP class as this is what Genesis does when it checks if popular SEO plugins are installed.

    function genesis_detect_seo_plugins() {
    
    	return genesis_detect_plugin(
    		// Use this filter to adjust plugin tests.
    		apply_filters(
    			'genesis_detect_seo_plugins',
    			//* Add to this array to add new plugin checks.
    			array(
    
    				// Classes to detect.
    				'classes' => array(
    					'All_in_One_SEO_Pack',
    					'All_in_One_SEO_Pack_p',
    					'HeadSpace_Plugin',
    					'Platinum_SEO_Pack',
    					'wpSEO',
    					'SEO_Ultimate',
    				),
    
    				// Functions to detect.
    				'functions' => array(),
    
    				// Constants to detect.
    				'constants' => array( 'WPSEO_VERSION', ),
    			)
    		)
    	);
    
    }
    

    Tutorials for StudioPress Themes.

    September 12, 2014 at 1:41 am #124075
    hongtrinh_it
    Member

    http://tribenhgan.com/gan-nhiem-mo-va-chan-doan-gan-nhiem-mo-khi-mang-thai.html

    September 12, 2014 at 1:54 am #124078
    marcus
    Member

    Thanks for the tip, but need to point out that this is somewhat off topic (esp for anyone else wanting to contribute here)... the checking of Genesis is fairly straightforward, my main concern is the switching of templates which is the part I'm having difficulties with.

    The Connect plugin was really helpful though, assuming it works I'll test it out and figure out why the same sort of snippet doesn't work for me.

    September 12, 2014 at 7:16 am #124096
    Brad Dalton
    Participant

    Would like to see what you come up with.


    Tutorials for StudioPress Themes.

    September 14, 2014 at 4:33 am #124305
    marcus
    Member

    OK, an update.... this isn't possible the way I'd like to do it.

    What we're doing for normal themes is feeding the theme with the template file (even page.php) that has to be loaded and that file usually contains the HTML that is output.

    Genesis does it differently, calling the Genesis functions which then build the page according to WP_Query functions like is_single() which CPTs will always trigger as true.

    The only way to do this (and this is how Genesis WooCommere Connect does it) is load custom templates that fire/unload the right Genesis hooks to produce the right structure of page.

    So.... we'll need to warn users using Genesis that this function won't work as expected and that templates are needed. Maybe another plugin like GWC will be made in the future for Events Manager

    September 14, 2014 at 5:53 am #124316
    Brad Dalton
    Participant

    Someone might do that at some stage.

    Or you could include them in your plugin.


    Tutorials for StudioPress Themes.

    September 14, 2014 at 5:54 am #124317
    marcus
    Member

    We'd be happy to do it when time permits, not entirely yet sure whether to include it in the plugin, there are pros/cons but might make sense to do so.

    Thanks for the pointers!

  • Author
    Posts
Viewing 12 posts - 1 through 12 (of 12 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2025 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble