This may sound a little confusing but I'll try my best to explain. I've made a shortcode that grabs the content of a post (when given the id), it looks like this:

$post_id = $id;
$queried_post = get_post($post_id);
$postcontent = $queried_post->post_content;
$title = $queried_post->post_title;

I then have another variable which puts a few things together and gets returned:

$finaloutput = $title . $postcontent;

What I want to do is be able to run shortcodes withing $postcontent, so when a post is fetched any shortcodes within that post will display. I tried running do_shortcode on the $postcontent variable but it caused the page to literally not load at all.

Any ideas?

  • can you post your code attempt to add do_shortcode ? sounds like you had a PHP error.
    – birgire
    Jun 14 '13 at 10:08
  • It was yesterday and I haven't version controlled this but I believe it was $finaloutput = $title . do_shortcode ($postcontent); I also tried putting the do_shortcode into it's own variable so $shortcodeoutput = do_shortcode ($postcontent);
    – user319940
    Jun 14 '13 at 10:14
  • Does $postcontent = apply_filters( 'the_content', $postcontent ); work?
    – shea
    Jun 15 '13 at 7:52
  • Unfortunately I get the same error with that :/ Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.
    – user319940
    Jun 17 '13 at 9:42

You can try this:

add_shortcode( 'content','content_callback' );

function content_callback( $atts, $content = NULL ){
    $atts = shortcode_atts(array(
        'pid' => '',
    ), $atts);      

    if( ! is_int( 1 * $atts['pid'] ) )
        return "<!-- Shortcode Error: pid must be an integer -->"; 

    if(  absint( $atts['pid'] ) === get_the_ID() )
        return "<!-- Shortcode Error: pid can't be the current id -->"; 

    $queried_post = get_post( absint( $atts['pid'] ) );

    if( ! is_object( $queried_post ) )
        return "<!-- Shortcode Error: Post not found! -->";

    $postcontent = do_shortcode( $queried_post->post_content );
    $title = $queried_post->post_title;

    $finaloutput = $title . $postcontent;

    return $finaloutput;

where the shortcode is used like this:

[content pid="2069"]

where pid is some post ID.

  • Unfortunately results in the same error :/
    – user319940
    Jun 17 '13 at 9:41
  • strange, this works on my install. You should check out codex.wordpress.org/Debugging_in_WordPress. Do you get any errors in debug.log ? You could also try to deactivate your plugins and also try it on the default TwentyTwelve theme.
    – birgire
    Jun 17 '13 at 11:14
  • Currently using the default 2012 theme with no plugins - debugging still results in a browser error rather than a PHP error - perhaps I'll try on a server.
    – user319940
    Jun 17 '13 at 13:23
  • Tried on a server - no PHP error at all but won't load the page (cuts off loading at header).
    – user319940
    Jun 17 '13 at 14:29
  • ... and the error only comes up when you add the do_shortcode? What kind of shortcodes are you using in the post?
    – birgire
    Jun 17 '13 at 14:38

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.