WordPress.org

Ready to get started?Download WordPress

Forums

Use get_template_part() into a plugin (4 posts)

  1. Andrea Barghigiani
    Member
    Posted 1 year ago #

    Hi there,

    Short Version:
    I am looking to find a solution to use the get_template_part() function inside my plugin, do you know any ways that will let me accomplish that?

    Long Version:
    I am developing a plugin that should show a tab system and on each tab I should show different kinds of WordPress Loops (you now... Most Recent, Most Viewed, Most Commented and so on...). I aim to keep my code as clean as possible and I thought to use some include function but, and here comes the first problem, each time that I try to include a file with a custom loop the code does not run properly, well doesn't run at all leaving me with a blank area. I have tried to put the code for the Loop inside the main file and everything works correctly but each time I try to include a Loop nothing happen.

    So, I tried to run get_template_part() and this works pretty well but I have to relate with the theme files and this is completely wrong from a plugin point of view...

    I start to think that there are no other ways to include a Loop if not to code it straight into the main file, am I right? There are no other way that I could leverage the power of get_template_part()?

    For the records, I have tested my include and require paths and also the structure of my personalized Loop and they works just fine.

    Thanks in advance for the help!

  2. luistinygod
    Member
    Posted 9 months ago #

    I'm in the same boat.

    I'm developing a plugin which injects templates in some user cases, where I would like to re-use template parts in the same way I use get_template_part for theme template files.

    But, after some digging in the WP core files, I found out get_template_part relies on a function called locate_template which only looks on STYLESHEETPATH and TEMPLATEPATH directories (both theme related). So, as I understand it, it isn't possible to use get_template_part on plugins.

    Further reading here.

  3. bcworkz
    Member
    Posted 9 months ago #

    Correct, plugins cannot load templates in their folder using get_template_part(). There is sort of a workaround, though it will not work for all situations.

    If whatever template you wish to use could be used as a page post type template to generate desired content, the template for this page can reside in the plugin folder. The page is created normally, accepting the default template. Then the plugin changes the page's template URL in postmeta under "_wp_page_template" to that of the plugin template.

    Since you can use a page template to get query vars and run loops, there's a fair chance you can make this work for your needs.

    Page templates normally should reside with the theme, but this is just so they show up in the page editor template pulldown. If showing up there is not important, the template can reside in plugin folders.

  4. Surrogate
    Member
    Posted 9 months ago #

    What you want to do ( i think if i understood you correctly, if not sorry ) is execute code from a plugin. folder and input it in template.

    try this :

    I will go full way on how to do it.

    Step one. register a plugin, in your plugin file add this :

    function activate_your_plugin() {
    
             add_option( 'Activated_Plugin', 'Plugin-Slug' );
    
             /* activation code here */
              }
            register_activation_hook( __FILE__, 'activate_your_plugin' );

    Step 2.

    You need code that will create page when your plugin is loaded, and on that page output your wordpress looops.

    So take this function, it will create that page :
    1). Page title // choose title.
    2). Page slug. // choose slug.
    3). post type. // page.
    4). shortcode. // Here you input a shortcode WITH YOUR CUSTON LOOPS ( your code that you need to output. ).
    5) Optional template name, template name you want to use, if not leave it blank.

    function create_custom_loop_page( $title, $slug, $post_type, $shortcode, $template = null ) {
        //Check if the page with this name exists.
         if(!get_page_by_title($title)) {
             // if not :
              $page_id = -1;
            $page_id = wp_insert_post(
                array(
                    'comment_status'    =>   'open',
                    'ping_status'       =>   'open',
                    'post_content' => $shortcode,
                    'post_author'       =>   1,   // Administrator is creating the page
                    'post_title'        =>   $title,
                    'post_name'     =>   strtolower( $slug ),
                    'post_status'       =>   'publish',
                    'post_type'     =>   strtolower( $post_type )
                )
            );
            // If a template is specified in the function arguments, let's apply it
            if( null != $template ) {
                update_post_meta( get_the_ID(), '_wp_page_template', $template );
            } // end if
        return $page_id;
           }
     }

    Step 3 , put your loop code in a shortcode :

    function custom_loop_shortcode(){
          echo 'I put my custom loop stuff here';
          }
          add_shortcode('your_shortcode_name', 'custom_loop_shortcode');

    Step 4, Now we call our function once only to execute this :

    function load_plugin() {
          if ( is_admin() && get_option( 'Activated_Plugin' ) == 'Plugin-Slug' ) {
           delete_option( 'Activated_Plugin' );
            /* do stuff once right after activation */
             create_custom_loop_page(('Custom Loop Page', 'custom-loop-page', 'page', '[your_shortcode_name]' );
    // Also last parameter specific template name, else wp will choose default one. ( recomend trying without it first ).
            }
            }
            add_action( 'admin_init', 'load_plugin' );

    Hope this helps.

    Cheers!

Topic Closed

This topic has been closed to new replies.

About this Topic