Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Shortcode always showing at the top of the wordpress (before post)

I’m new to WP plugin development and found out that my shortcode is showing at the top of the page, but I want to include it anywhere in the post (not at the TOP).

My code:

function getLotoSecond() {
    $html = file_get_contents_curl('URL__ADDRESS');

    // List
    $start = stripos($html, '<ul id="results-2" class="results-items">');
    $end = stripos($html, '</ul>', $offset = $start);
    $length = $end - $start;

    $list = substr($html, $start, $length);
    //--

    // Regex
    preg_match_all('(<label for="LotoPart_C2_([1-6]|Additional)">([0-9]|[1-9][0-9])<\/label>)', $list, $matches);
    $items = $matches[2];
    //--

    // Output
    echo '<ul>';
    for($i = 0; $i < 7; $i++) {
        if($i == 6) {
            echo '<li style="width:30px;height:30px;border-radius:5px;background-color:#F4A024;color:white;font-size:20px;text-align:center;line-height:30px;display:inline-block;margin:0px 5px 5px 0px;">' . $items[$i] . '</li>';
        } else {
            echo '<li style="width:30px;height:30px;border-radius:5px;background-color:#294A70;color:white;font-size:20px;text-align:center;line-height:30px;display:inline-block;margin:0px 5px 5px 0px;">' . $items[$i] . '</li>';
        }
    }
    echo '</ul>';
    //--
}

And then:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function main($atts) {
    if($atts["type"] == "loto-prvy") {
        getLotoFirst();
    } else if($atts["type"] == "loto-druhy") {
        getLotoSecond();
    }
}
    
add_shortcode('tipo', 'main');

What should be the problem here?

>Solution :

Shortcodes should only return the content, they must not print or echo anything. if you have a function that echo/print some data, then you need to use ob_start and ob_get_clean

Here is how you can use ob functions within your shortcode

function main( $atts ) {
    ob_start();
    
    if ( $atts['type'] == 'loto-prvy' ) {
        getLotoFirst();
    } elseif ( $atts['type'] == 'loto-druhy' ) {
        getLotoSecond();
    }

    $content = ob_get_clean(); // store buffered output content.

    return $content; // Return the content.
}
add_shortcode( 'tipo', 'main' );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading