1

I just wrote a custom post (suppliers) as a plugin (with meta content) that works great. I've also created a template page (single-suppliers.php) within the same plugin to display the data. I wrote it in such a way that, if someone so desires, they can create their own single-suppliers.php page in their template and get their own look and feel. (I'm using template_include filter to do this.)

Example of the template_include filter.

// Register a my Membership Template.
function my_membership_include_template_function( $template_path ) {
    if ( get_post_type() == 'my_members' ) {
        if ( is_single() && is_main_query() ) {
            // Check if the file exists in the theme first, otherwise
            // serve the file from the plugin.
            if ( $theme_file = locate_template( array ( 'single-my_members.php' ) ) ) {
                $template_path = $theme_file;
            } else {
                $template_path = plugin_dir_path( __FILE__ ) . '/single-my_members.php';
            }
        }
    }
    return $template_path;
}
add_filter( 'template_include', 'my_membership_include_template_function', 1 );

An example of single-my_members.php.

<?php
get_header();
?>
<h1>Test Header - Does not appear within the theme's "content" area.</h1>
<?php
get_sidebar();
get_footer();

However, the issue that I am having is that, while the content looks great within its own context, it does not sit in the correct location of my client's current theme. It sits in its own area above the start of the theme's content. See the screenshot:

Actual Location + Desired Location

After searching through WordPress documentation, I ran across the filter, the_content, which resolves the above problem by putting the content in the correct location, but then it does not seem to allow for customization of a page should my client want to do that ( which they have indicated that they do. There's also the issue that the_content filter appears to append. )

I'm not sure how to best resolve this or if there is a better practice that I should be using. If it helps, I am using Genesis with a child theme, but I specifically don't want to code in Genesis hooks as that would defeat the purpose of the generic plugin.

  • Can you add some code blocks to show what is going on? – Cedon Dec 3 '17 at 23:23
  • @Cedon Added per request. – JasCav Dec 4 '17 at 0:47
0

You can not replace half of a template by using filters and hooks if the template itself doesn't support so.

The template_include will override an entire template, it means header, body, content, sidebar, footer, and everything.

Now you have these options:

Style Your Template

Create a full single-my_member.php file and then style it by CSS and probably JS. You need to think of it as a complete template, not just a page to render a few lines of content.

Use preg_replace()

Not a good idea at all, but you could do a preg_replace() combined with an output buffering and add content to a page's common places, such as <body> or <header>. This might slow down your site a lot, and is not always accurate.

Provide Shortcodes

If you are trying to add limited content ( not a full HTML structure ) to templates, then you might be able to provide some shortcodes for the users to use. The risk of messing with templates here is very low.

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.