Making plugin output customizable

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.