0
  1. Should a plugin be able to add a widget area above the_content? (Without requiring template/theme changes.)
  2. What would be the best way to do this? Doubts: widgets typically echo output to the screen; the_content filter examples always return $content.

The code below does something but seems flawed: testing indicated that shortcodes were not being parsed so I added do_shortcode.

function insert_above_content_widget($content) {

if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Above Content') ) : 
endif;

echo do_shortcode($content);

}

add_filter( 'the_content', 'insert_above_content_widget' );

Seems like it would be a useful thing to be able to do but this approach doesn't feel right somehow. Anyone see any problems?

1 Answer 1

1

Anyone see any problems?

What you have done doesn't create a sidebar panel in the backend at wp-admin->Widgets (I tested it) so you can't really use your sidebar like a widgetized sidebar anyway. You'd have to programmatically add widgets to your sidebar, but if you are going to be doing that why bother with the sidebar at all? Why not just add the markup you want at the top of your post content and be done with it? I don't understand the need for this overhead.

Yes, shortcodes wouldn't work because you have short circuited the filter process. By echoing $content instead of returning it you are interfering with the rest of the filters that should be running before the content is finally echoed. do_shortcode is not the only one that you'd need to replace, but don't do that, just return $content.

Echoing inside the filter seems to work but I wouldn't rest too easily. There are ways that could go wrong. I'd worry about someone doing something like this:

$content = get_the_content();
$content = 'abc'.$content.'def';
$content = apply_filters('the_content',$content);

Imagine somebody does that early in the page load, maybe even before the headers are sent? (Possibly to use the content as a meta description or something?) Your content is echoing at a very wrong place. It could break attempts to get content over AJAX as well. JSON is very picky.

Other things that come to mind...

If you echo content rather than return it you are are essentially breaking any other hook to the_content.

It is also possible that some other plugin could hook before yours and echo something even earlier, probably violating your intentions and making a further mess of design.

I won't get into whether you should insert a sidebar before the content but I am fairly sure you should not do it this way.

3
  • Thanks @s_ha_dum. It felt wrong and it was wrong! But the whole point of plugins is to avoid webmasters having to make changes to their themes template files so the should part of the question still feels valid?
    – Jon
    Commented Dec 22, 2012 at 10:59
  • The plugin can register the sidebar widget - register_sidebar - and create the widget; the user can add the widget to the sidebar. BUT the widget won't display until the widget area has been included in the theme: the plugin can't / shouldn't (?) do this.
    – Jon
    Commented Dec 22, 2012 at 11:06
  • Trying to alter a theme, which is basically what this is, would be over stepping the bounds of a plugin, in my opinion. Let the theme control layout/design.
    – s_ha_dum
    Commented Dec 22, 2012 at 15:04

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.