• Good Afternoon,

    I’m having an issue where the filter for the CPT template callback is not getting called but if I put any old post type in there like ‘single_template’ it’s getting called just fine.

    Any idea why?

    Thanks!

    
    register_post_type('stoneblue-thinkific', $rpt_arg);
    
    
    $this->loader->add_filter('archive-stoneblue-thinkific_template', $plugin_public, 'archive_stoneblue_thinkific');
    $this->loader->add_filter('single-stoneblue-thinkific_template', $plugin_public, 'single_stoneblue_thinkific');
    
    $this->loader->add_filter('single_template', $plugin_public, 'single_stoneblue_thinkific');
    $this->loader->add_filter('archive_template', $plugin_public, 'archive_stoneblue_thinkific');
    
    

    So for a single post and an archive it calls the cb and loads the template located in the plugin dir. But for CPT nada. The CB doesn’t get called.

    The CPT is there, I can create new CPTs and see the list in the admin area wp-admin/edit.php?post_type=stoneblue-thinkific

    That all works great just not able to load the CPT templates. Well I can load the CPT templates with single post and archive post types but not the CPTs.

    • This topic was modified 6 years, 7 months ago by Ken Stone.
    • This topic was modified 6 years, 7 months ago by Ken Stone.
Viewing 4 replies - 16 through 19 (of 19 total)
  • Moderator bcworkz

    (@bcworkz)

    If your plugin generates links from the onset using online-courses as base, there would never be any reason for links with stoneblue-thinkific to exist. Be sure the back end links like the “view” action link also are using online-courses. If no stoneblue-thinkific links exist, there is no reason to have it in the rewrite rules. You can specify online-courses as the rewrite slug when registering the post type. Upon rewrite flush, WP will add it to the persistent rewrite rules.

    Directly manipulating the rewrite rule array instead of going through add_rewrite_rule() is rather hacky, but the end result is the same. If this were custom work for a single site, I don’t see any problem with such an approach. If your plugin is eventually intended to be in the WP repository, you probably should be doing it the “right” way. If add_rewrite_rule() isn’t working for you, it may be worth trying to find out why. Easier said than done. It could involve tracing execution through core code.

    It’s recommended to flush rewrite rules only on activation and deactivation because it’s a computationally expensive process. Doing so on every request greatly adds to the server load. The results of a flush persist afterwards because the rules are saved in either the DB or .htaccess. I’m not sure why we call add_rewrite_rule() on init TBH. It seems like on activation would be enough. I’ve not investigated this aspect enough to really understand all of what goes on “under the hood”.

    Congratulations on taking the plunge towards understanding regexp. It’s empowering to have some confidence in composing good regexps. They can be very powerful. One could almost claim it to be a compact symbolic programming language in itself. Don’t get carried away though. Don’t use preg_replace() if str_replace() will do the job. The former is a good deal more “expensive”.

    Thread Starter Ken Stone

    (@wpstoneblue)

    Thanks bcworkz and lannister,

    I could have made it easy on myself and just named the CPT online-courses. This is a single site. I turned what should have been a few hour project into a few days but the learning experience is priceless.

    Yes all the back end links seem to be covered by post_type_link hook but more testing will be needed to determine this. And at this point I believe there aren’t any stoneblue-thinkific links remaining. Putting the names I’m sure that will be used directly in the CPT surely would have been the way to go and could still be. I will investigate the rewrite slug further.

    I guess the reason I decided to give the direct manipulation of the rules array a try is because I saw just how many rules are created for the CPT and thought wow might as well just copy these.

    I’ve used add_rewrite_rule successfully in the past for things other than CPTs so I’m sure that especially equipped with my new mastery of regexs I’d be able to get this working in no time at this point. TBH I wasn’t quite sure where exactly to redirect to but now seeing everything clearly in the rules array it’s a bit of a no brainer.

    Yes I’m flushing only on act and deact. I believe it’s recommended to rewrite the rules on init just because of you never know what other plugins are doing but I could be wrong on that.

    This is a great book by jfriedl on regex, highly recommend it to everyone no doubt. I’m sure I will get carried away initially…

    I was having difficulty with single_template I think it was because I was failing to visit the perma links page or flush the rules. The code for it is still in but the add_filter is commented out currently.

    Thread Starter Ken Stone

    (@wpstoneblue)

    OK I just revisited this one tried using the slug by adding
    'rewrite' => array('slug' => $thinkific_rewrite),
    to the CPT. Then deleted all the code for creating rewrite rules.

    Also using

    $this->loader->add_filter('single_template', $plugin_public, 'stoneblue_thinkific_single_template');                
    $this->loader->add_filter('archive_template', $plugin_public, 'stoneblue_thinkific_archive_template');

    for the cpt templates.

    Mission accomplished.

    Thanks!

    Moderator bcworkz

    (@bcworkz)

    Great news! It seems the final solution is often SO much simpler than we could have imagined when in the thick of it!

    BTW, you very well could have seen advice regarding flushing on init. But it’s not credible advice. “Official” word on the topic: https://codex.www.ads-software.com/Function_Reference/flush_rewrite_rules#Usage

    Flushing is computationally very “expensive”, it’s wasteful doing so on every request and can overload one’s resources on a busy site. But if you find doing so is useful, there’s nothing stopping you, it’s your site ??

Viewing 4 replies - 16 through 19 (of 19 total)
  • The topic ‘add_filter(single-{CPT}_template’ is closed to new replies.