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" >');
$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:
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?
CodePudding user response:
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' );