php - Return different code if variable is a string or not -
i want return small block of code if variable string, if empty return text "no image".
function link_img_preview( $atts, $content = null ) { if ( is_string( $content ) ) { $content = do_shortcode( str_replace( '###space###', '', $content ) ); } if ( is_string( $content ) == false ) { $content = 'no image'; } require_once('opengraph.php'); $graph = opengraph::fetch($content); $return = '<img class="link-image" src='; $return .= $graph->image; $return .= '>'; return $return; }
no need overcomplicate things. default value $content = null evaluate false, if don't provide second argument 'no image'. if pass empty string evaluate false, 'no image'. if pass not string function appears expecting string, throw error, seems reasonable response.
if ($content) { $content = do_shortcode( str_replace( '###space###', '', $content ) ); } else { $content = 'no image'; }
Comments
Post a Comment