0

I need your recommendation for the best practice here:

  • A custom post type I have created needs a list of attachments at the end of the post content.
  • I have created a series of functions that handle displaying the list of appropriate attachments for a post.
  • I don't want to use shortcode to insert this attachment list, since every post of this post type should have the list
  • I'm looking for a portable, reusable approach

My initial approach was to encapsulate the attachment functions into a standalone plugin. But how to integrate the plugin output into the page if I don't use shortcode?

My solution was to create my own pseudo-template-tag ("the_attachments()") which is defined in the plugin. Then my custom-post-type.php template page uses this pseudo-template-tag right after the_content() and we get our nice list.

But is this really the best approach? Now we have a template that's dependent on an external plugin. If the plugin isn't there, the template "breaks". It's tough to "require" a plugin, from a template perspective because is_plugin_active() is only available at the admin level, and that's probably for a reason (because it's a stupid idea to make a template dependent on a plugin!)

So I'm left with either: A) decouple the plugin output from the template, similar to the way a widget works (if so, how do I pass the $post information to that widget?), or B) embed the "plugin" code within the template functions (but is this then truly portable?)

1
  • I'm still very interested in everyone's recommendations and best practices. The solution that I believe (so far) to be the most flexible and "WordPress-like" is to create a widget in my plugin, then to use a custom sidebar in the theme (on the custom-post-type.php page) that makes a spot under the_content available for widgets. This seems to be the most flexible and idiomatic way to do it, but I'm really hoping to hear other suggestions to improve my knowledge!
    – Tom Auger
    Commented May 6, 2011 at 13:59

2 Answers 2

1
<?php
// A)
// inside your template
if ( function_exists( 'the_attachment_stuff()' ) )
{
    // do stuff
}
// better/faster
if ( class_exists( 'attachment_plugin_class' ) )
{
    // do stuff
}

// B) 
// inside your template
do_action( 'the_attachment_suff');
// means setting this inside your plugin
// this avoids throwing errors or aborting if the hook isn't in your template
add_action( 'the_attachment_stuff', 'your_callback_fn' );

// C)
function add_attachment_stuff()
{
    if ( ! is_admin() )
        return;

    // the following is guess and theory...
    $content  = get_the_content();
    $content .= the_attachment_stuff(); // in case the attachment_stuff returns instead of echos the result/output.
}
// @link http://codex.wordpress.org/Plugin_API/Action_Reference
// only add on publish, on 'post_save' we would add it multiple times
add_action( 'publish_post', 'add_attachment_stuff', 100 );
add_action( 'publish_phone', 'add_attachment_stuff', 100 );
2
  • Hey kaiser, thanks for these interesting suggestions. Let me see if I understand them all. A) in the template file, just check for the existence of the plugin function B) in the template, create an action hook and in the plugin, listen for that action (that one seems kinda 'backward' doesn't it? - very creative!) C) in the plugin, make it so that every time you publish a post, it adds the attachment list to the end. This one is probably not a very good approach - but really creative! Thanks for the suggestions.
    – Tom Auger
    Commented May 6, 2011 at 14:03
  • @Tom Auger - A) is pretty common, but slow (both versions). B) is the best practice advice you'll get from everyone.
    – kaiser
    Commented May 6, 2011 at 14:19
2

You can output your plugins function to the_content by adding with a filter.

function ta_post_attachment() {
    if ( is_singular( 'your_custom_post_type' ) {
        // do stuff
        }
}

add_filter( 'the_content', 'ta_post_attachment' );
2
  • Thanks for the response, Chris. If I am understanding your approach correctly, you're suggesting that I add a filter in my plugin that automatically attaches my attachments output to the end of the_content if the page is my custom post type page? This is brilliant, EXCEPT that it requires a tight coupling between my plugin and that custom post type. If someone else wants to use my plugin with their other custom post type, then they would have to go in and modify my plugin code, no? Or am I missing something?
    – Tom Auger
    Commented May 6, 2011 at 13:56
  • @Tom Auger I thought your plugin created the custom post type. You could also add an if posts_type_exists conditional or build a small options page.
    – Chris_O
    Commented May 6, 2011 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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