because I’m not echoing anything in this code.
But you are! This code:
function smb_function( $atts ) {
?> <div class="social_button_shortcode"><?php
Is the same as:
function smb_function( $atts ) {
echo ' <div class="social_button_shortcode">';
Likewise with the closing div tag at the end. Any form of direct output in a shortcode will break it, wether it was done via echo
, print
, printf
, or other WP functions.
The shortcode also breaks the rule that shortcodes should return their content, because it only returns when if ( count($smb_url) > 0 ) {
is true. But what if it isn’t true? And notice that the function can never reach the closing div tag because of the return
.
As an example, what would the output of this shortcode be:
add_shortcode( 'blank', function() {} );
It’s equivalent to this:
add_shortcode( 'blank', function() { return NULL; } );
And this:
add_shortcode( 'blank', function() { echo "Hello World!"; return NULL; } );
All 3 shortcodes produce a null value.
If you absolutely must output directly, use an output buffer to catch it and assign the result to a variable.
tip: typehint the shortcode function as returning a string, it’ll make it more obvious if you don’t return a string