The situation that has come up is that upon developing the hours shortcode (seen in the footer), there is a strange error that it stops the very next shortcode from running and makes it return nothing. If I put 2 of the same one after the other, the second one renders properly.
Example:
[business-hours]
[business-phone]
— This properly displays the hours, but doesn’t render the phone
[business-hours]
[business-phone]
[business-phone]
— This displays the hours, doesn’t display the first phone, but then displays the second phone properly.
Any help narrowing down the cause would be appreciated.
]]>As for the back to back, it is only being affected by the [business-hours] shortcode. Using a [business-phone] or [business-address] back to back, the issue is not there.
Also, I have tried adding static text between as well. That didn’t stop it from not rendering the shortcode which comes right after the [business-hours] one. They could be in different columns, or have any text between. In fac ton the current page the text “Phone” down there is inbetween the 2 shortcodes.
Here’s the supplied code:
function j3afb_ga_click($gacategory = 'None Set', $gaaction = 'None Set', $galabel = 'None Set', $gainteraction = false) {
if($gainteraction) {
$interaction = ", 'nonInteraction: 1'";
}
return "ga('itpcga.send', 'event', '".$gacategory."', '".$gaaction."', '".$galabel."'".$interaction.");";
}
function j3afb_build_phone($phonenum, $phonestyle, $class, $gacode, $pwrap = false, $nolink = false) {
switch(strtolower($phonestyle)) {
case 'dot':
case 'dots':
case 'period':
case 'periods':
$phoneareastart = '';
$phoneareaend = '.';
$phonedivider = '.';
break;
case 'hyphen':
case 'hyphens':
case 'dash':
case 'dashes':
$phoneareastart = '';
$phoneareaend = '-';
$phonedivider = '-';
break;
case 'normal':
default:
$phoneareastart = '(';
$phoneareaend = ') ';
$phonedivider = '-';
break;
}
if(strlen(preg_replace('/[^0-9]/','',$phonenum)) == 7)
$phoneformat = preg_replace("/([0-9]{3})([0-9]{4})/", "$1".$phonedivider."$2", preg_replace('/[^0-9]/','',$phonenum));
else if(strlen(preg_replace('/[^0-9]/','',$phonenum)) == 10)
$phoneformat = preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", $phoneareastart."$1".$phoneareaend."$2".$phonedivider."$3", preg_replace('/[^0-9]/','',$phonenum));
else
$phoneformat = $phonenum;
$phonelink = '<a href="tel:'.preg_replace('/[^0-9]/','',$phonenum).'" class="phonelink" onclick="'.$gacode.'">';
return ($pwrap ? '<p class="phonewrap '.$class.'">' : '<span class="phonewrap '.$class.'">').($nolink ? '<span class="phonelink">' : $phonelink).$phoneformat.($nolink ? '</span>' : '</a>').($pwrap ? '</p>' : '</span>');
}
function j3afb_build_address($businessname = '', $addressstreet, $addresssuite = '', $addresscity, $addressstate, $addresscode, $class, $nosuite = false, $nocity = false, $nostate = false, $nozip = false, $pwrap = false, $nolink = false, $breaks = '', $streetbreak = '', $suitebreak = '' ) {
if(isset($businessname) && $businessname != '') {
$labelset = true;
}
if(isset($addresssuite) && $addresssuite != '') {
$suiteset = true;
}
if (isset($breaks) && $breaks != '') {
$stdiv = $breaks;
$sudiv = $breaks;
} else {
$stdiv = '<br />';
$sudiv = '<br />';
}
if(isset($streetbreak) && $streetbreak != '') {
$stdiv = $streetbreak;
}
if(isset($suitebreak) && $suitebreak != '') {
$sudiv = $suitebreak;
}
$streetdivider = '<span class="addressstreetbreak">'.$stdiv.'</span>';
$suitedivider = '<span class="addresssuitebreak">'.$sudiv.'</span>';
$fulladdress = '<span class="streetwrap">'.$addressstreet.'</span>'.do_shortcode($streetdivider).($suiteset ? ($nosuite ? '' : '<span class="suitewrap">'.$addresssuite.'</span>'.do_shortcode($suitedivider)):'').($nocity ? '' : '<span class="citywrap">'.$addresscity.',</span> ').($nostate ? '' : '<span class="statewrap">'.$addressstate.'</span>').($nozip ? '' : ' '.'<span class="postalcodewrap">'.$addresscode.'</span>');
$linkcode = '<a href="https://maps.google.com/?q='.($labelset ? $businessname.' ' : '').$addressstreet.', '.$addresscity.', '.$addressstate.', '.$addresscode.'" class="addresslink" onclick="'.$gacode.'" target="_blank">';
return ($pwrap ? '<p class="addresswrap '.$class.'">' : '<span class="addresswrap '.$class.'">').($nolink ? '<span class="addresslink">' : $linkcode).$fulladdress.($nolink ? '</span>' : '</a>').($pwrap ? '</p>' : '</span>');
}
function j3afb_business_phone($atts) {
extract(shortcode_atts(
array( 'name' => '', 'phone' => '', 'style' => '', 'class' => '', 'gacategory' => 'call', 'gaaction' => 'call', 'galabel' => 'Site Default', 'ganoninteraction' => false, 'pwrap' => false, 'nolink' => false,), $atts )
);
$content = '';
$phonenum = '';
$businessname = '';
$businessphonelabel = '';
$gacode = '';
if( !function_exists('acf_add_options_page') ) {
return;
}
if(isset($style) && $style <> '') {
$phonestyle = $style;
} else {
$phonestyle = get_field('business-phone-style', 'option');
}
$gacode = j3afb_ga_click($gacategory, $gaaction, $galabel, $ganoninteraction);
if(have_rows('business', 'option')) {
while(have_rows('business', 'option')) : the_row();
$businessname = get_sub_field('business-name', 'option');
if((strtolower($businessname) == strtolower($name)) || (isset($name) && $name == '')){
if(have_rows('business-phones', 'option')) {
while(have_rows('business-phones', 'option')) : the_row();
$businessphonelabel = get_sub_field('business-phone-label', 'option');
$phonenum = get_sub_field('business-phone-number', 'option');
if((strtolower($businessphonelabel) == strtolower($phone)) || (isset($phone) && $phone == '')) {
$content = j3afb_build_phone($phonenum, $phonestyle, $class, $gacode, $pwrap, $nolink);
break;
}
endwhile;
}
}
endwhile;
}
return $content;
}
function j3afb_business_address($atts) {
extract(shortcode_atts(
array( 'name' => '', 'address' => '', 'class' => '', 'gacategory' => 'directions', 'gaaction' => 'directions', 'galabel' => 'Site Default', 'ganoninteraction' => false, 'nosuite' => false, 'nocity' => false, 'nostate' => false, 'nozip' => false, 'pwrap' => false, 'nolink' => false, 'breaks' => '', 'streetbreak' => '', 'suitebreak' => ''), $atts )
);
$content = '';
$businessname = '';
$addressstreet = '';
$addresssuite = '';
$addresscity = '';
$addressstate = '';
$addresscode = '';
if( !function_exists('acf_add_options_page') ) {
return;
}
$gacode = j3afb_ga_click($gacategory, $gaaction, $galabel, $ganoninteraction);
if(have_rows('business', 'option')) {
while(have_rows('business', 'option')) : the_row();
$businessname = get_sub_field('business-name', 'option');
if((strtolower($businessname) == strtolower($name)) || (isset($name) && $name == '')){
if(have_rows('business-address', 'option')) {
while(have_rows('business-address', 'option')) : the_row();
$businessaddresslabel = get_sub_field('business-address-label', 'option');
$addressstreet = get_sub_field('business-address-street', 'option');
$addresssuite = get_sub_field('business-address-street2', 'option');
$addresscity = get_sub_field('business-address-city', 'option');
if(get_sub_field('business-address-country', 'option') == 'Canada') {
$addressstate = get_sub_field('business-address-province', 'option');
} else {
$addressstate = get_sub_field('business-address-state', 'option');
}
$addresscode = get_sub_field('business-address-postal-code', 'option');
if((strtolower($businessaddresslabel) == strtolower($address)) || (isset($address) && $address == '')) {
$content = j3afb_build_address($businessname, $addressstreet, $addresssuite, $addresscity, $addressstate, $addresscode, $class, $nosuite, $nocity, $nostate, $nozip, $pwrap, $nolink, $breaks, $streetbreak, $suitebreak);
break;
}
endwhile;
}
}
endwhile;
}
return $content;
}
function j3afb_build_day_hours($skipday = false, $dayopen = false, $day, $opentime, $closetime, $specialnote = '', $dayseperator = ':', $hourseperator = '-') {
$info = '';
$note = '';
if($skipday && !$dayopen) {
return;
}
if(isset($specialnote) && $specialnote != '') {
$note = '<span class="noteseperator"><br /></span><span class="specialnote">'.$specialnote.'</span>';
}
$info = '<p class="businesshours">';
$info .= '<span class="daylabel">'.$day.'</span><span class="dayseperator">'.$dayseperator.'</span> ';
if(!$dayopen) {
$info .= '<span class="hourentry"><span class="dayclosed">Closed</span></span>';
} else {
$info .= '<span class="hourentry"><span class="dayopen">';
$info .= '<span class="houropen">'.$opentime.'</span> ';
$info .= '<span class="hourseperator">'.$hourseperator.'</span> ';
$info .= '<span class="hourclose">'.$closetime.'</span>';
$info .= '</span></span>';
$info .= $note;
}
$info .= '</p>';
return $info;
}
function j3afb_business_hours($atts) {
$attr = shortcode_atts(array(
'name' => '',
'style' => '',
'grouping' => '',
'dayseperator' => ':',
'hourseperator' => '-',
'skipclosed' => false
), $atts );
if( !function_exists('acf_add_options_page') ) {
return;
}
$content = '';
$daylabels = array(
'monday' => array('full' => 'Monday', 'short' => 'Mon', 'abbr' => 'M'),
'tuesday' => array('full' => 'Tuesday', 'short' => 'Tues', 'abbr' => 'T'),
'wednesday' => array('full' => 'Wednesday', 'short' => 'Wed', 'abbr' => 'W'),
'thursday' => array('full' => 'Thursday', 'short' => 'Thurs', 'abbr' => 'Th'),
'friday' => array('full' => 'Friday', 'short' => 'Fri', 'abbr' => 'F'),
'saturday' => array('full' => 'Saturday', 'short' => 'Sat', 'abbr' => 'Sa'),
'sunday' => array('full' => 'Sunday', 'short' => 'Sun', 'abbr' => 'Su')
);
if(have_rows('business', 'option')) {
while(have_rows('business', 'option')) : the_row();
$businessname = get_sub_field('business-name', 'option');
if((strtolower($businessname) == strtolower($attr['name'])) || (isset($attr['name']) && $attr['name'] == '')) {
if(isset($attr['style']) && $attr['style'] == ''){
$attr['style'] = get_sub_field('day_name_style', 'option');
}
if(isset($attr['grouping']) && $attr['grouping'] == ''){
$attr['grouping'] = get_sub_field('day_group_style', 'option');
}
if(isset($attr['skipclosed']) && $attr['skipclosed'] == ''){
$attr['skipclosed'] = get_sub_field('skip_closed_days', 'option');
}
$content = '';
$daysinfo = array(
'monday' => get_sub_field('hours_mon'),
'tuesday' => get_sub_field('hours_tues'),
'wednesday' => get_sub_field('hours_wed'),
'thursday' => get_sub_field('hours_thurs'),
'friday' => get_sub_field('hours_fri'),
'saturday' => get_sub_field('hours_sat'),
'sunday' => get_sub_field('hours_sun')
);
$dayset = array(
'monday' => false,
'tuesday' => false,
'wednesday' => false,
'thursday' => false,
'friday' => false,
'saturday' => false,
'sunday' => false
);
$daynames = array_keys($daysinfo);
if($attr['grouping'] == 'span') {
for($i=0;$i<count($daysinfo);$i++) {
$currentday = '';
if(!$dayset[$daynames[$i]]) {
$daylabel = $daylabels[$daynames[$i]][strtolower($attr['style'])];
for($y=$i+1;$y<count($daysinfo);$y++){
if($daysinfo[$daynames[$y]]['hours_open'] && $daysinfo[$daynames[$i]]['hours_open_time'] === $daysinfo[$daynames[$y]]['hours_open_time'] && $daysinfo[$daynames[$i]]['hours_close_time'] === $daysinfo[$daynames[$y]]['hours_close_time']) {
$currentday = $daylabels[$daynames[$y]][strtolower($attr['style'])];
$dayset[$daynames[$y]] = true;
} else {
break;
}
}
if(isset($currentday) && $currentday != '') {
$daylabel .= ' - '.$currentday;
}
$dayset[$daynames[$i]] = true;
$content .= j3afb_build_day_hours($attr['skipclosed'],$daysinfo[$daynames[$i]]['hours_open'],$daylabel,$daysinfo[$daynames[$i]]['hours_open_time'],$daysinfo[$daynames[$i]]['hours_close_time'],$daysinfo[$daynames[$i]]['hours_note'],$attr['dayseperator'],$attr['hourseperator']);
}
}
} else if($attr['grouping'] == 'group') {
for($i=0;$i<count($daysinfo);$i++) {
if(!$dayset[$daynames[$i]]) {
$daylabel = $daylabels[$daynames[$i]][$attr['style']];
for($y=$i+1;$y<count($daysinfo);$y++){
if($daysinfo[$daynames[$y]]['hours_open'] && $daysinfo[$daynames[$i]]['hours_open_time'] === $daysinfo[$daynames[$y]]['hours_open_time'] && $daysinfo[$daynames[$i]]['hours_close_time'] === $daysinfo[$daynames[$y]]['hours_close_time']) {
$dayset[$daynames[$y]] = true;
$daylabel .= ', '.$daylabels[$daynames[$y]][$attr['style']];
}
}
$dayset[$daynames[$i]] = true;
$content .= j3afb_build_day_hours($attr['skipclosed'],$daysinfo[$daynames[$i]]['hours_open'],$daylabel,$daysinfo[$daynames[$i]]['hours_open_time'],$daysinfo[$daynames[$i]]['hours_close_time'],$daysinfo[$daynames[$i]]['hours_note'],$attr['dayseperator'],$attr['hourseperator']);
}
}
} else {
foreach($daysinfo as $dayname => $values) {
$content .= j3afb_build_day_hours($attr['skipclosed'],$values['hours_open'],$daylabels[$dayname][strtolower($attr['style'])],$values['hours_open_time'],$values['hours_close_time'],$values['hours_note'],$attr['dayseperator'],$attr['hourseperator']);
}
}
break;
}
endwhile;
}
return $content;
}
add_shortcode('clinic-phone', 'j3afb_business_phone');
add_shortcode('business-phone', 'j3afb_business_phone');
add_shortcode('clinic-address', 'j3afb_business_address');
add_shortcode('business-address', 'j3afb_business_address');
add_shortcode('clinic-hours', 'j3afb_business_hours');
add_shortcode('business-hours', 'j3afb_business_hours');
]]>
It’s difficult to digest all of that, but it appears to be in order. Because it relies on custom fields, it’s not possible for us to fully test it.
If you were to put the same shortcodes directly in a post’s content, do you observe the same issue? Also check the page’s HTML source to be sure the phone number is really missing and not just hidden by CSS rules. If the phone number is truly missing, I’m afraid that some detailed debugging on your site will be the only way to track down the problem. That level of assistance is beyond the scope of these forums. You’ll need to manage it yourself. The basic process is to return intermediate values from j3afb_business_phone() to verify every assignment works as expected. Start near the beginning and return any arbitrary string just to ensure the function is being called. Then at strategic points in the function, return intermediate values to be sure all is well.
For example, return $phonestyle and verify it has the correct value. Then return $gacode, $businessname, $businessphonelabel, etc. in turn. If you find an anomaly, dig deeper, perhaps doing the same for a called function like j3afb_build_phone(). By a process of elimination, you’ll be able to zero in on the problem.
It will be a tedious process, but you will eventually find the problem.
]]>I will test in the post content itself – I have failed to do that before hand because was focused in on making it work in the footer area that the problem was showing.
Update: The same functionality of it not running the shortcode directly after the [business-hours] happens even on page, not just in the footer. And it does render as a blank <p></p> — It is not hidden by any CSS.
]]>Turns out the break; in the j3afb_business_hours was causing an issue with the way ACF Pro works. I took that out and added a check for the first iteration of the while loop, and took care of it that way.
Thanks for the help up to here to getting it narrowed down to this problem spot.
]]>