0

I've created a shortcode using my limited knowledge, google, and stackexchange to do so.

But when I load the page, it loads above the page contents and appears as html text.

(shortocode text)
-------
page contents appearing normally

As I've said, I have limited (Zero) knowledge of this type of coding and I'm unable to find out which part/what isn't working.

I've attached a compacted version of the code I've used, with less content being displayed in the shortcode function.

// Add Shortcode
function p_formatage( $atts , $content = null ) {

    // Attributes
    extract( shortcode_atts(
        array(
            'postid' => '',
            'ptitle' => '',
            'pimage' => '',
        ), $atts )
    );

    $postid = $atts['postid'];
    $ptitle = $atts['ptitle'];
    $pimage = $atts['pimage'];

    // Post title
    $p-ptitle = if ( empty( $ptitle ) ) {
    echo "Whoops! Looks like went wrong here!";
    }
    else { 
     if ( $postid == true ) {
     echo "<span class="ptitle"><a href="http://www.website.com/' . $postid . '">' . $ptitle . '</a></span>";
     }
     else { 
     echo "<span class="ptitle">' . $ptitle . '</span>";
     }
    }

    $p-beta = if ( empty( $pimage) ) {
    echo "$ptitle";
    }
    else { 
    echo "<span class="pimage"><img href="http://www.website.com/xx.jpg";/></span>";
    }

   $pclass = if ( $postid ) {
    echo "$p-ptitle";
    }
    else { 
    echo "$p-beta";
    }

    // Output
    $output = {
        echo "$pclass";
    }

    return $output;
}

add_shortcode( 'p-fmt', 'p_formatage' );
  • Please format your code properly and paste those here. – Subharanjan Jun 17 '14 at 7:50
1

You're using your shortcode function to directly output something (using echo). Shortcodes are supposed to return the desired output rather than echo it directly. You could use output buffering to "catch" anything that's outputted (for example, via echo) in an output buffer instead of directly outputting it, and retrieve the content of the buffer afterwards.

This would yield an implementation like this:

function wpse149825_my_shortcode( $atts, $content = NULL ) {
    // Turn on output buffering, which sends output from "echo" to a "buffer" instead of directly outputting it
    ob_start();

    // Output shortcode content
    echo 'Shortcode content';

    // Get buffer contents
    $contents = ob_get_clean();

    // Return shortcode content
    return $contents; 
}

add_shortcode( 'my_shortcode', 'wpse149825_my_shortcode' );
  • I understand the logic, but how do you use the buffer on multiple echo $value? – user51030 Jun 18 '14 at 1:34
  • The buffer basically catches anything that's outputted. So, using multiple echos inbetween ob_start and ob_get_clean are all "caught" in the buffer. – engelen Jun 18 '14 at 6:45
0

You need fix the echo statements which are breaking in between. Also, as rightly said by @engelen above, don't directly echo the output. Make it buffer and "return" it.

Here is the code which will help.

<?php
// Add Shortcode
function p_formatage( $atts, $content = null ) {

    // Attributes
    extract( shortcode_atts(
            array(
                'postid' => '',
                'ptitle' => '',
                'pimage' => '',
            ),
            $atts,
            'p-fmt'
        )
    );

    $postid = $atts['postid'];
    $ptitle = $atts['ptitle'];
    $pimage = $atts['pimage'];

    // Start buffering the output
    ob_start();

    /* ------ DO WHATEVER OUTPUT YOU WANT TO DISPLAY BELOW THIS -------- */

    // Post title
    if ( empty( $ptitle ) ) {
        echo 'Whoops! Looks like went wrong here!';
    } else {
        if ( $postid == true ) {
            echo '<span class="ptitle"><a href="http://www.website.com/' . $postid . '">' . $ptitle . '</a></span>';
        } else {
            echo '<span class="ptitle">' . $ptitle . '</span>';
        }
    }

    /* ------ STOP SPITING OUT THE OUTPUT NOW  ------------------------ */

    // Get buffer contents
    $contents = ob_get_clean();

    // Return shortcode content
    return $contents;
}
add_shortcode( 'p-fmt', 'p_formatage' );
  • In your code above, a single $value has been turned into $contents output. But what if you have multiple $value echoing code, like $p-ptitle = if was, how would you go about it? – Would it be recommended to turn them into functions? – user51030 Jun 19 '14 at 2:05
  • Yes, Keep using echo multiple times like I have shown above, provided all the echo, print etc. must be used before this line $contents = ob_get_clean();. This way, all the values will be inside $contentsand will be displayed on frontend too. :) – Subharanjan Jun 19 '14 at 4:15
  • If the echo codes start with a value like $p_ptitle = if ( whatever..), do they need to be formatted differently? Would they now need to begin by if ( whatever..) and end with $p_ptitle = ob_get_contents(); – user51030 Jun 19 '14 at 4:53
  • I don't clearly understand why do you want to assign the value returned from an if( whatever... ) condition check, to a variable ? Simply use this conditions etc. to check and echo what needs to be displayed in place of the [short-code]. – Subharanjan Jun 19 '14 at 10:56
  • Hey @Subharanjan I've created some code in the original question to help you see. The shortcode is made of multiple echoes, that create different $values, which are combined at the end to create the final output of the shortcode. – user51030 Jun 19 '14 at 16:54

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.