Shortcode Stopping Next Shortcode on Page
-
I have some custom shortcodes that work with ACF Pro to make updating business information more streamlined.
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.
The page I need help with: [log in to see the link]
-
you say “custom shortcodes” i take it you wrote some custom functions? possibly in your functions.php file? can you share the code for your “custom shortcodes”?
Not only do I concur with Mr Case, shortcodes do not normally work in footers at all. How/where did you add the shortcodes so that they appear in the footer? It seems the do_shortcode() parser is having trouble with back to back shortcodes. Have you tried adding intervening text between the two? It shouldn’t matter, but it would be useful to know if it makes a difference.
Yes, these were written by myself.
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');
- This reply was modified 6 years, 8 months ago by itpcjames.
Thanks for posting the code. FYI, posting that much code here in the forums is frowned upon. In this case we were asking for it, not realizing how much was involved. In the future, please use Pastebin or a Gist for large code excerpts, they make code much easier to read.
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.
Thanks for the insight. I was worried about that – and was just curious if anyone had any idea why it would be causing an issue. It doesn’t matter which of the shortcodes I use after the hours one. I use the address, and the address doesn’t show even. So I had suspected something inside the hours code was running away or something, not clearing correctly… But I haven’t been able to narrow down where any of those problems could be because that text information does get output correctly.
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.
- This reply was modified 6 years, 8 months ago by itpcjames.
Problem is Resolved.
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.
I’m glad you solved it! That’s a strange one all right.
- The topic ‘Shortcode Stopping Next Shortcode on Page’ is closed to new replies.