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.

I have a shortcode that outputs null ("").

What I'm trying to accomplish is to output a default message if the shortcode outputs null. I don't want to extend my shortcode because there may be multiple instances of the shortcode and I don't want to output the message multiple times.

So my function is as follows:

function av_standalone_doc_404($content) {
    if(has_term('standalone-document', 'formats')){
        $filtered_content = apply_filters('the_content', $content);
        if(empty($content)){
            // Display 404 message
            $output .= '<div id="404_message">Error:
                Please check this page later.</div>';
        }else{
            $output .= '';
        }
        return $output . $content;
    }
}
add_filter('the_content', 'av_standalone_doc_404', 12);

The problem is that apply_filters is causing a fatal error - obviously because you can't apply a filter to the content and call apply_filters within that filter.

The '12' load order doesn't appear to help either.

How can I apply the filter after the subsequent filters which apply the shortcode to the_content?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

if you want to keep the current setup, try to hook in early and use do_shortcode():

function av_standalone_doc_404($content) 
{
    remove_filter( current_filter(), __FUNCTION__ );

    if ( has_term( 'standalone-document', 'formats' ) )
    {
        $filtered_content = do_shortcode( $content );
        // …

If you want to avoid repeating shortcode outputs use a static variable in the shortcode handler:

function shortode_handler()
{
    static $done = FALSE;

    if ( $done )
        return;

    $done = TRUE;

    // create the shortcode output; this will happen only once.

    return $output;
}
share|improve this answer
    
What is the hook here? Also, it's not that I want to limit the output of my shortcodes, it's that I want to assess the_content after the shortcodes are applied. I may have some shortcodes that output content. The 'null' is a conditional, not a static output. –  AlxVallejo Nov 12 '12 at 18:03
    
@AlxVallejo See my update. –  toscho Nov 12 '12 at 18:09
    
Hm, remove_filter/do_shortcode did not work –  AlxVallejo Nov 12 '12 at 19:27
    
Just tried 'remove_shortcode' and it worked! The shortcode is in tact in the content but replaced with my 404 error message! –  AlxVallejo Nov 12 '12 at 20:30
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.