Community Forums › Forums › Archived Forums › General Discussion › template_redirect and switching templates dynamically
Tagged: CPT, hooks, template_redirect
- This topic has 11 replies, 3 voices, and was last updated 9 years, 7 months ago by marcus.
-
AuthorPosts
-
September 10, 2014 at 10:27 am #123757marcusMember
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!
MarcusSeptember 10, 2014 at 10:53 am #123764Brad DaltonParticipantCouldn't you add a check for Genesis in your plugins code?
Or check for genesis and then include a special template for genesis.
September 10, 2014 at 11:17 am #123769marcusMemberThanks 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 #123798Brad DaltonParticipantIf 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.
September 11, 2014 at 1:23 pm #124004marcusMemberIf 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 #124016Brad DaltonParticipantOr 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', ), ) ) ); }
September 12, 2014 at 1:41 am #124075September 12, 2014 at 1:54 am #124078marcusMemberThanks 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 #124096Brad DaltonParticipantSeptember 14, 2014 at 4:33 am #124305marcusMemberOK, 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 #124316Brad DaltonParticipantSomeone might do that at some stage.
Or you could include them in your plugin.
September 14, 2014 at 5:54 am #124317marcusMemberWe'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!
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.