[Resolved] Complex conditional statement stopping page from loading
This thread is resolved. Here is a description of the problem and solution.
Problem: A complex conditional statement is not working correctly.
Solution: Sometimes long conditionals must be broken up into multiple templates. This is an unfortunate side effect of how WordPress handles regular expressions.
I'm trying to use a conditional statement to add some markup to a template if any one of a group of fields has content. This is my statement:
[wpv-conditional if="( $(wpcf-24-hour-exterior-book-drop) ne '' ) OR ( $(wpcf-early-voting-location) ne '' ) OR ( $(wpcf-election-day-voting) ne '' ) OR ( $(wpcf-lactation-room) ne '' ) OR ( $(wpcf-meeting-rooms) ne '' ) OR ( $(wpcf-multi-stall-single-gender-restrooms) ne '' ) OR ( $(wpcf-single-occupant-all-gender-restroom) ne '' ) OR ( $(wpcf-study-rooms) ne '' )"]
<div class="location-amenities">
<h2>Amenities</h2>
[/wpv-conditional]
When I include this in the template, the test page I'm working on won't load - I get a message from Chrome saying "This site can’t be reached. The connection was reset." If I take out that conditional statement, it loads fine. What can I do differently to make this work better?
I can tell you now that this page works fine on my end even with you conditionals.
So its definitely an issue with you local host. Unfortunately we actually don't debug local host issues, and your logs don't give us a clue as to whats wrong.
However this should work fine when you put it onto a live server.
Thanks for testing and for the pointers - I switched my localhost to run PHP7, and that solved the loading problem. Progress!
Now that the page is loading with those last two statements, I'm seeing them do weird things to other content.
[wpv-conditional if="( '[wpv-post-body]' ne '' )"]
...
[/wpv-conditional]
...is making content near it show up as shortcodes instead of the value. See toolset-conditional-before-1 and toolset-conditional-after-1 for the difference. If I use wpv-post-title instead of wpv-post-body, the issue doesn't come up and it looks fine.
...is making the images within the loop get replaced with shortcodes, as well as a bunch of the other content on the page. See toolset-conditional-before-2 and toolset-conditional-after-2 for the difference on that one. If I just include the content within the conditional, it looks fine - adding the conditional around it breaks it.
Here's the whole current setup for that template, in case there's something else in there that's affecting them:
[wpv-conditional if="( $(location-closed) ne '' )"]
<div class="location-closed">
[/wpv-conditional]
[wpv-post-featured-image size="full" class="location-feature-image"]
[wpv-conditional if="( $(wpcf-location-status) ne '' )"]
<div class="location-status">
[types field='location-status'][/types]
</div>
[/wpv-conditional]
[wpv-conditional if="( $(address) ne '' ) OR ( $(phone-number-main) ne '' ) OR ( $(other-contact-link) ne '' ) OR ( $(facebook-page) ne '' )"]
<div class="location-contact-info">
[/wpv-conditional]
[types field='address'][/types]
[types field='direction-special-notes'][/types]
[types field='phone-number-main'][/types]
[types field='facebook-page' title='Find us on Facebook'][/types]
[types field='other-contact-link' title='Contact us'][/types]
[wpv-conditional if="( $(address) ne '' ) OR ( $(phone-number-main) ne '' ) OR ( $(other-contact-link) ne '' ) OR ( $(facebook-page) ne '' )"]
</div>
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-location-special-info) ne '' )"]
<div class="location-special-info">
[types field='location-special-info'][/types]
</div>
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-getting-here-bike-racks) ne '' ) OR ( $(wpcf-getting-here-bus-routes) ne '' ) OR ( $(wpcf-getting-here-electric-vehicle-charging) ne '' ) OR ( $(wpcf-getting-here-parking) ne '' )"]
<div id="getting-here">
<h2>Getting here</h2>
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-getting-here-bus-routes) ne '' )"]
<h3>Bus routes</h3>
[types field='getting-here-bus-routes'][/types]
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-getting-here-bike-racks) ne '' )"]
<h3>Bike racks</h3>
[types field='getting-here-bike-racks'][/types]
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-getting-here-parking) ne '' )"]
<h3>Parking</h3>
[types field='getting-here-parking'][/types]
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-getting-here-electric-vehicle-charging) ne '' )"]
<h3>Electric vehicle charging</h3>
[types field='getting-here-electric-vehicle-charging'][/types]
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-getting-here-bike-racks) ne '' ) OR ( $(wpcf-getting-here-bus-routes) ne '' ) OR ( $(wpcf-getting-here-electric-vehicle-charging) ne '' ) OR ( $(wpcf-getting-here-parking) ne '' )"]
</div>
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-24-hour-exterior-book-drop) ne '' ) OR ( $(wpcf-early-voting-location) ne '' ) OR ( $(wpcf-election-day-voting) ne '' ) OR ( $(wpcf-lactation-room) ne '' ) OR ( $(wpcf-meeting-rooms) ne '' ) OR ( $(wpcf-multi-stall-single-gender-restrooms) ne '' ) OR ( $(wpcf-single-occupant-all-gender-restroom) ne '' ) OR ( $(wpcf-study-rooms) ne '' )"]
<div class="location-amenities">
<h2>Amenities</h2>
[/wpv-conditional]
[types field='24-hour-exterior-book-drop'][/types]
[types field='multi-stall-single-gender-restrooms'][/types]
[types field='single-occupant-all-gender-restroom'][/types]
[types field='lactation-room'][/types]
[types field='meeting-rooms'][/types]
[types field='study-rooms'][/types]
[types field='early-voting-location'][/types]
[wpv-conditional if="( $(wpcf-election-day-voting) eq 'Election day voting' )"]
<li>[types field='election-day-voting'][/types] - [types field='election-day-precinct'][/types]</li>
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-24-hour-exterior-book-drop) ne '' ) OR ( $(wpcf-early-voting-location) ne '' ) OR ( $(wpcf-election-day-voting) ne '' ) OR ( $(wpcf-lactation-room) ne '' ) OR ( $(wpcf-meeting-rooms) ne '' ) OR ( $(wpcf-multi-stall-single-gender-restrooms) ne '' ) OR ( $(wpcf-single-occupant-all-gender-restroom) ne '' ) OR ( $(wpcf-study-rooms) ne '' )"]
</div>
[/wpv-conditional]
<h2>Common tasks</h2>
*******
[wpv-view name="location-common-tasks"]
<h2>Reserve a room</h2>
*******
[wpv-view name="location-page-feature-areas" area="1"]
<h2>Technology</h2>
*******
[wpv-conditional if="( $(wpcf-did-you-know) ne '' )"]
<div class="location-facts">
<h2>Did you know?</h2>
[types field='did-you-know'][/types]
</div>
[/wpv-conditional]
<!-- feature area 2 -->
[wpv-view name="location-page-feature-areas" area="2"]
<!-- event feed -->
[wpv-conditional if="( $(event-feed) ne '' )"]
<div class="location-events communico-event-feed">
<h2>Today's events</h2>
[types field='event-feed'][/types]
</div>
[/wpv-conditional]
<!-- feature area 3 -->
[wpv-view name="location-page-feature-areas" area="3"]
<!-- special features -->
[wpv-view name="special-location-features" orderby="field-special-feature-order" order="asc"]
<!-- manager -->
[wpv-conditional if="( [wpv-post-id] ne [wpv-post-id item='@manager.parent'] )"]
<div class="location-manager">
<h2>Manager</h2>
[wpv-post-title item="@manager.parent"]
Email: [types field='email-address' item='@manager.parent'][/types]
[wpv-post-body view_template="None" item="@manager.parent"]
[wpv-conditional if="( $(wpcf-personal-pronouns).item(@manager.parent) ne '' )"]
Personal pronouns: [types field='personal-pronouns' item='@manager.parent'][/types]
[/wpv-conditional]
</div>
[/wpv-conditional]
<!--conditional-->
<div class="location-description">
[wpv-conditional if="( $(location-nickname) ne '' )"]
<h2>About [types field='location-nickname'][/types]</h2>
[/wpv-conditional]
[wpv-conditional if="( $(location-nickname) eq '' )"]
<h2>About [wpv-post-title]</h2>
[/wpv-conditional]
[wpv-post-body view_template="None"]
</div
<!--end conditional-->
[wpv-conditional if="( $(wpcf-gallery-image) ne '' )"]
<div class="tb-repeating-field tb-repeating-field--crop toolset-gallery">
<div class="tb-repeating-field--grid">
[wpv-for-each field="wpcf-gallery-image"]
<div>
<a href="[types field='gallery-image' size='full' url='true' separator=''][/types]" >[types field='gallery-image' output='normal' align='none' size='full' proportional='true' resize='proportional' title='%%TITLE%%' alt='%%ALT%%'][/types]</a>
</div>
[/wpv-for-each]
</div>
</div>
[/wpv-conditional]
[wpv-conditional if="( $(location-closed) ne '' )"]
</div>
[/wpv-conditional]
So this is a familiar issue wit the shortcodes breaking and just rendering the shortcode themselves.
Personally on my localhost i'm not seeing this issue but this again has to do with the server resources. Sometimes when queries take longer than the allocated php processing time then some codes won't get processed.
What you can look at is this document below. hidden link
Thanks, Shane - I changed the max input variables, memory limit, and time limit ("PHP":{"MemoryLimit":"1256M","UploadMax":"2M","PostMax":"64M","TimeLimit":"720","MaxInputVars":"5000"}}), but I'm still getting the same thing. Understood that you don't debug localhosts, but would it be possible for you to let me know what your settings are so I can try them? Also, is there anything you see I could be doing differently in this template so that it's not so resource-intensive in the first place?
Hi, Shane is out on public holiday today so I'd like to jump in here. I think your memory and MaxInputVars settings look great. However, complex conditionals are known to cause unexpected rendering issues in certain versions of PHP because of how WordPress regular expressions work. You can see it mentioned in our conditional documentation here: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
When using very long content with the conditional shortcode you might encounter an error. Since Views uses native WordPress parsing for all shortcodes, this limitation does not come from the Views itself. In such cases, there is no alternative but to break up the content into smaller sections, which may mean having to repeat the conditional statements.
Other users have also had luck disabling the pcre.jit regular expression extension, but it's not a universal fix for everyone. You may need help from your site's hosting company to adjust your server extension settings, or you may be able to disable the extension on your own by adding this line to your wp-config.php file:
ini_set('pcre.jit', false);
If this doesn't solve the problem, your best bet is to break up the content into smaller sections. Let me know if you have questions about that.
solved that problem (and the for-each loop handles not printing the rest of the markup if the field is empty).
For the other one, I realized after breaking the same thing on another view that the body needs a view template specified within the conditional - so changing [wpv-conditional if="( '[wpv-post-body]' ne '' )"] to [wpv-conditional if="( '[wpv-post-body view_template="None"]' ne '' )"] fixed the rest of it.