Skip to content
Advertisement

Plugin content always on top of post WordPress

I’m trying to make a WordPress plugin. Well, actually it’s done and fully working, except one thing.

I have added a shortcode for the plugin. But no matter where in the content I call this shortcode, the contents it gets are always on top of the post, instead of where I placed the tag.

The code that outputs something:

public static function showIncomingSearches(){

global $id;
$arSearches = self::getArObj(array('wp_post_id' => $id));

ob_start();
if(!empty($arSearches)){
$str = '<ul>' . PHP_EOL;        

    foreach($arSearches as $oSearch){
        $str .= '<li>'.htmlspecialchars($oSearch->searchterm).'</li>' . PHP_EOL;
    }

$str .= '</ul>';

if(!empty($arSearches))
    echo $str;
} else {
    echo ' ';
}

return ob_get_clean();

}

And the shortcode functionality:

add_shortcode('show_incoming_searches', 'checkReferrer');
function checkReferrer(){
    incomingSearches::checkReferrer();  
    echo incomingSearches::showIncomingSearches();
}

What I want to know though, is why it is always on top of the content?

Advertisement

Answer

Your shortcode code needs to return the content, not echo it.

function checkReferrer(){
    incomingSearches::checkReferrer();  
    return incomingSearches::showIncomingSearches();
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement