Can shortcodes contain conditional statements? Even without them my shortcode renders blank page

The following code is supposed to allow for a shortcode that includes many attributes, but not all of the attributes may always be present in which case I do not want the containing element to show anything. However, with or without the conditional statements, when I upload my functions file I receive only a blank page on the front and back end.

Here is the full shortcode with conditional statements:

function info($atts, $content = null) {
extract(shortcode_atts(array(
    "name" => '',
    "image" => '',
    "address" => '',
    "phone" => '',
    "email" => '',
    "website" => '',
    "description" => '',
    "amenities" => ''
), $atts));
return '<span class="sort">'
if($image) { '<img src="'.$image.'" />'}'
<span class="name">'.$name.'</span>
<span class="details">'
if($phone) { $phone }
if($address) { '&bull; <a href="http://maps.google.com/?q='.$address.'" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" target="_blank">'.$address.' <img src="<?php bloginfo('template_directory'); ?>/_/images/mapmarker.png" width="16" height="16" alt="Map" /></a><br/>'}
if($email) { '<a class="infomail" href="mailto:'.$email.'" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">Send Email</a>' }
if($website) { '<a class="infosite" href="'$website'" rel="nofollow noreferrer noopener">Visit Website</a>' }
if($description) { '<p class="infodetails">'.$description.'</p>' }
if($amenities) { '<p class="amenities">'.$amenities.'</p>' }
'</span>
</span>';
}

add_shortcode("info", "info");

Thanks for your suggestions!

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

your conditional breaks the concatenation of the strings (which is not in your code, anyway)

try to re-write this section:

function info($atts, $content = null) {        
extract(shortcode_atts(array(            
    "name" => '',            
    "image" => '',            
    "address" => '',            
    "phone" => '',            
    "email" => '',            
    "website" => '',            
    "description" => '',            
    "amenities" => ''        
    ), $atts)); 
$output = '<span class="sort">'; 
if($image) { $output .= '<img src="'.$image.'" />';}
$output .= '<span class="name">'.$name.'</span> <span class="details">';
if($phone) { $output .= $phone; } 
if($address) { $output .= ' &bull; <a href="http://maps.google.com/?q='.$address.'" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" target="_blank">'.$address.' <img src="' . get_bloginfo('template_directory') . '/_/images/mapmarker.png" width="16" height="16" alt="Map" /></a><br/>'; } 
if($email) { $output .= ' <a class="infomail" href="mailto:'.$email.'" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">Send Email</a>'; } 
if($website) { $output .= ' <a class="infosite" href="'.$website.'" rel="nofollow noreferrer noopener">Visit Website</a>'; } 
if($description) { $output .= '<p class="infodetails">'.$description.'</p>'; } 
if($amenities) { $output .= '<p class="amenities">'.$amenities.'</p>'; } 
$output .= '</span> </span>'; 
return $output;
}

add_shortcode("info", "info");

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply