0

I am writing a plugin that will display a timeline of historic events, and I am looking for ways to make the output customizable. I was considering including a template/example within the plugin folder that can be overridden inside the theme folder. My idea was to make sure WP looked in the theme folder first for a customized template before resorting to the one I create.

If this is not good practice, let me know!

What functions can I use to do the above functionality?

0

Shortcode with a custom output template:

You could try the following shortcode demo:

/**
 * The shortcode [history_timeline] with a custom output template.
 */
add_shortcode( 'history_timeline', function( $atts = array(), $content = '' )
{
    ob_start();
    if ( '' == locate_template( 'templates/timeline.php', TRUE ) )
            include( plugin_dir_path( __FILE__ ) . 'templates/timeline.php' );

    $html = ob_get_clean();
    return $html;
});

where the shortcode is [history_timeline] and

if ( '' == locate_template( 'templates/timeline.php', TRUE ) )
     include( plugin_dir_path( __FILE__ ) . 'templates/timeline.php' );

means:

  • If /wp-content/themes/MYTHEME/templates/timeline.php is located, then it will be automatically loaded.

  • Else: /wp-content/plugins/history-timeline/templates/timeline.php is loaded.

where we assume that __FILE__ is in the history-timeline root directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.