1

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?

1

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;
}

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.