Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In a plugin which brings its own post type to the table, I wish to handle post_meta by filtering the_content - unless the user has provided their own single-{post-type}.php template.

Should I run this within the_content filter? Or elsewhere?

if ( file_exists( get_stylesheet_directory() . '/single-event.php' ) ) {
    include( get_stylesheet_directory() . '/single-event.php' );
} elseif ( file_exists( get_template_directory() . '/single-event.php' ) ) {
    include( get_template_directory() . '/single-event.php' );
} else {
    // filter time
}

If it's not in my add_filter('the_content', 'event_filter_function', 10); then I guess I'm wondering how else I will know at run-time whether this file exists.

So is computationally expensive to run this filter and simply return $content unaltered if there is a template provided? Or is there a better way?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

Two code snippets here that are pretty self explanatory:

Add a template from the plugin if none is present from the theme

add_filter( 'page_template', 'wpse110317_append_meta' );
function wpse110317_append_meta( $template )
{
    is_singular()
    AND ! file_exists( get_template_directory()."/{$template}" )
        AND $template = plugin_dir_path( __FILE__ )."/{$template}";

    return $template;
}

Append something to the content until there's a template in the theme

add_filter( 'page_template', 'wpse110317_maybe_add_filter' );
function wpse110317_maybe_add_filter( $template )
{
    is_singular()
    AND ! file_exists( get_template_directory()."/{$template}" )
        AND add_action( 'the_content', 'wpse110317_append_meta' );

    return $template;
}
function wpse110317_append_meta( $content )
{
    // do stuff
    return $content;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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