Home › Toolset Professional Support › [Resolved] Use php to check if a types field is empty – not the conditional shortcode way
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | - |
- | 13:00 – 18:00 | 13:00 – 18:00 | 13:00 – 18:00 | 14:00 – 18:00 | 13:00 – 18:00 | - |
Supporter timezone: America/Jamaica (GMT-05:00)
Tagged: Types plugin
This topic contains 13 replies, has 2 voices.
Last updated by Shane 5 years, 4 months ago.
Assisted by: Shane.
Tell us what you are trying to do?
Use php to check if a types field is empty - not the conditional shortcode way
Is there any documentation that you are following?
No
Is there a similar example that we can see?
Not that I've found
What is the link to your site?
hidden link
Here's the code I have so far. I'm just missing the php to check the Toolset Types field (isset or empty).
add_action( 'wp_enqueue_scripts', function() {
// this is where you check the toolset field and if you want a div hidden set to true in the array
$args = array(
'#testimonials' => true,
'#full-bio-section' => true,
'#case-studies' => true,
'#video-gallery' => true,
);
wp_localize_script( 'jquery', 'rm_hide', $args );
});
Hi Ray,
Thank you for getting in touch.
You can only check if the field is empty when you are getting it from the database. To do this you will need to use the get_post_meta() to get the field from the database.
So an example would be.
$field = get_post_meta(get_the_ID(),'wpcf-my-field'); //Then you run and if statement on the field like this if (!empty($field)){ //do something }
So this section of code will only run if the field is not empty.
Please let me know if this helps.
Thanks,
Shane
Could you write how the code would look based on the example I gave in my initial post? I DO NOT want to involve JS or jQuery to do this if possible. Can you show me how to accomplish this with straight PHP and no JS/jQuery? Is that even possible?
Please help!
Hi Ray,
I'm not sure of the context of your code however it will look like this.
add_action( 'wp_enqueue_scripts', function() { $field = get_post_meta(get_the_ID(),'wpcf-my-field'); //Then you run and if statement on the field like this if (!empty($field)){ $args = array( '#testimonials' => true, '#full-bio-section' => true, '#case-studies' => true, '#video-gallery' => true, ); wp_localize_script( 'jquery', 'rm_hide', $args ); } }
I suspect you want to pass those arguments if the field is not empty. You need to replace "wpcf-my-field" with the slug of the field you want to check.
Thanks,
Shane
Hi Shane!
Actually, what I want to do is hide the div id's in the array (#testimonials, #full-bio-section, #case-studies, #video-gallery) if the field with the named guide-bio is empty. Also, I don't have anything going on with 'rm_hide' in the wp_localize_script( 'jquery', 'rm_hide', $args ); so, now that I've said this, what would the code look like?
Also, do I need to format the first part like this?
$field = get_post_meta( get_the_ID(), 'wpcf-my-field', true );
The 'true' part returns a string, if that isnt there you will get an array.
Could you please write it again with but, the information I've shared in mind? Or, does what I've said here make any difference?
Hi Ray,
To get this clear you don't have a working JS code to be loaded by the wp_localize_script function ?
So even if we add the code to check for an empty field there isn't a working code to hide the div ids?
Are these ids being hardcoded into the theme's template file?
Please clarify for me because im still a little unsure of the context.
Thanks,
Shane
Hi Shane!
So, here's what I'm doing in this site.
I am using a template I've made in Elementor to display a single custom post type created in Toolset. In this template, there are several modules being used to display various fields from my custom post type. I have given these modules and columns div ID's so that they can be targeted to be hidden if a person does not have a full bio for their profile page.
I can edit the div ID's any way you think is best. For example, I could have two ID's or classes like this:
#hide-no-bio and #show-alt-view
Then I could add CSS to say, the #show-alt-view with {display:none;} and then use your code to make it {display:block;} of the field with the slug guide-bio is empty.
I'm quite flexible, I just want to do it the best way possible. I'm very open to not even using JS or jQuery if that's even possible.
Thoughts?
Hi Ray,
So this is a bit more complex than how it sounds. This is definitely in the area of some custom coding. Usually we do not provide such support as it is out of the scope of our support forum.
I would suggest that you contact the elementor team in order to find the best way on how to hide the divs given your scenario.
If these divs were hardcoded into the template file for the site then I couldn't provided some code to dynamically hide them in the template but since these are being generated by when you add them then it would be best to contact them for a suitable solution.
Thanks,
Shane
Hi Shane!
The div ID's are added to the modules and columns the same as if I were hard-coding them into a template, there's zero difference. So, you can still provide me with the code because it will work the same. The div ID's do not fluctuate and are not generated dynamically so, div id still equals div id.
You are already close with the code you provided. I was merely trying to give as many details as I could so you would know how it should be written.
So please, provide me with the code based on the div id's as if it were a hard-coded template.
I really need your help and code example.
Hi Ray,
Though i'm not sure of the purpose of this code here
$args = array( '#testimonials' => true, '#full-bio-section' => true, '#case-studies' => true, '#video-gallery' => true, ); wp_localize_script( 'jquery', 'rm_hide', $args ); }
But i've already given you the code to check if a field is empty. The only other modification that I can do is if the field is a select field or checkbox field.
If not then this code below is the correct code.
add_action( 'wp_enqueue_scripts', function() { $field = get_post_meta(get_the_ID(),'wpcf-my-field'); //Then you run and if statement on the field like this if (!empty($field)){ $args = array( '#testimonials' => true, '#full-bio-section' => true, '#case-studies' => true, '#video-gallery' => true, ); wp_localize_script( 'jquery', 'rm_hide', $args ); } }
There isn't another way to check if the field is empty. Not sure how you will be hiding or displaying the Div's but I wrote the code to execute your code if the field is not empty.
Thanks,
Shane
Hi Shane!
All I want to do is hide the divs listed in the array if the Toolset field is empty.
Hi Ray,
The divs are generated dynamically on the page when elementor loads up their page layouts.
As mentioned you wanted me to provide the php way on how to do this. Unless there is some static div tag in the php template file then a php solution won't work.
This is why I mentioned that you would need to contact the elementor team for a solution on how to hide these dynamically loaded ID's
Since these ID's will only show up after the page has loaded and they are not there physically in the template.php file for the post.
I thought of a JS solution but this will require that something be added to the page itself to check and if the JS code finds this page element then we add some css to that element to hide it.
This is not quite an efficient solution but we can use it given that the elementor team has no solution of their own.
Thanks,
Shane
Understood, then I'd like to see your JS solution. Sorry for all the trouble, just trying to get this working today for the client.
Hi Ray,
The solution would go like this.
[wpv-conditional if="( empty($(wpcf-test-field)) )"] <span class="hide-div"></span>
This above can be placed in any content area on this template.
Then you just need to add this JS.
jQuery(document).ready(function () { if ( jQuery( ".hide-div" ).length ) { jQuery( "#testimonials" ).hide(); jQuery( "#full-bio-section" ).hide(); jQuery( "#case-studies" ).hide(); jQuery( "#video-gallery" ).hide(); }});
Please let me know if this helps.
Thanks,
Shane