Skip Navigation

[Resolved] change wpv-conditions to css class

This thread is resolved. Here is a description of the problem and solution.

Problem:

Check if a field is empty using custom coding.

Solution:

This can be done by adding the following custom shortcode to your functions.php file.

// Add Shortcode
function wp_check_field( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'id' => '',
            'field' => '',
        ),
        $atts
    );
 
    $field = get_post_meta($atts['id'],$atts['field']);
    $class = '';
   if(!empty($field[0])){
$class = '';
return;
}
    return $class;
 
}
add_shortcode( 'wp_check_field', 'wp_check_field' );

You can get the specific field information by doing this [wp_get_field id='[wpv-post-id]' field='wpcf-my-field']

This support ticket is created 5 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

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)

This topic contains 16 replies, has 3 voices.

Last updated by Jim 5 years, 5 months ago.

Assisted by: Shane.

Author
Posts
#1125224

Jim

Another try to get this solved...
I am still strugling with this problem: https://toolset.com/forums/topic/condition-causes-error/

The issue is not resolved yet and I am looking for other ways to find a solution.

It is all about checking if the field has a value, if it does print it to the page, if not don't.

First I was thinking to leave out the condition altogether and that indeed solves the problem because if the field is empty and is printed to the layout it doesn't leave an error, but...

in most cases within those conditions I start with a heading for that field:
<h3>References</h4>
--- here the references field content ---

So now if the field is empty there still is this heading... (thats why I use the condition)

Now I am thinking to turn the condition into a class like this:

$references = "showme";

if ($(wpcf-references) eq '' ){$references="hideme";}

then for the html I could wrap the section in a div and add the class to it like this:

<div id="references" class="<?php print $references; ?>">
<h3>References</h4>
[types field='references'][/types]
</div>

This way a simple css rule:
.hideme{display:none;}

would then hide the complete div if the field is empty.

But now I need some help how to add php to this layout - visual editor.

I know it is possible to turn php into a shortcode, maybe that is the way? But I do not know how to get the values of the fields into a shortcode.

I hope you understand what I am trying to do here and would appreciate any help getting this to work.

Thanks again, Jim

#1125375

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jim,

Thank you for contacting our support forum.

To get a better understanding of this, its that you want to perform your conditionals within a shortcode and then print out that value using the custom shortcode ?

Please let me know.
Thanks,
Shane

#1125536

Jim

Yes, I think if I can avoid the conditions then the page will load normaly.
At the moment all shortcodes within conditions are not executed on pages with a lot of images. I need a workarround.

So I want to turn the conditions of all those fields into a css class that I can add to the appropiate divs as explained above.

I do not neccesairily want to use short codes, but I need a way to check those fields.
If you have a better solution let me know.

#1125543

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jim,

No problem, we actually have a little document that should be able to guide you on how to write this into a custom shortcode.
https://toolset.com/documentation/adding-custom-code/how-to-create-a-custom-shortcode/

Please let me know if this helps.

Thanks,
Shane

#1125599

Jim

Well it's not that I don't know how to create a shortcode Shane.

I need help how to get those values that are now conditions into my layout.
Maybe it can be done with shortcodes but if so I need help how to get those field values.

But then again, maybe there is a better way. Did you read and understand my first post?

#1125601

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jim,

So essentially what you want to do is to check in the code if the fields are empty and if they are not return a class that you can use to hide the entire div right ?

#1125605

Jim

Yes, the code should check if (not) empty and then set the classname accordingly

#1126422

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jim,

Take a look at this example below.

// Add Shortcode
function wp_check_field( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'id' => '',
			'field' => '',
		),
		$atts
	);

	$field = get_post_meta($atts['id'],$atts['field']);
	$class = '';
	if(!empty($field)){
	    $class = 'someclass';
	}
	return $class;

}
add_shortcode( 'wp_check_field', 'wp_check_field' );

You can get the specific field information by doing this [wp_get_field id='[wpv-post-id]' field='wpcf-my-field']

This should return the classname if the field is not empty.

Please let me know if this helps.

Thanks,
Shane

#1126449

Jim

Thanks Shane, I will try that.
But then, how can I apply the class to the div? I cannot use php or anything can I?

Can you give a short example how this could work inside a visual editor?

#1126493

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jim,

All you need to do is add it like this.

<div class="[wp_get_field id='[wpv-post-id]' field='wpcf-my-field']"></div>

This should give the desired results.

Thanks,
Shane

#1127360

Jim

Thanks Shane.
It's working but not on all fields.

The figures&tables field (field type image), and the video field (field type embedded media) are not recognized as empty when empty.
Do you know a way to check those aswell?

#1127580

Hi, Shane is on public holiday and will return tomorrow to continue assisting you. Thanks for your patience.

#1128313

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jim,

Some fields are stored a little differently than others.

Could you allow me to login and check this for you ?

Could you send me a link to the page where you have this setup as well ?

Thanks,
Shane

#1128470

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jim,

I tried it and it still works.

I noticed that you are not using the wpcf- prefix on the field attribute. Since this is a custom shortcode, when retrieving or checking types fields you need to use the wpcf- prefix on the field names.
Example "wpcf-ddid"

Please let me know if this helps.

Thanks,
Shane

#1128493

Jim

Hi Shane,
I saw that the ddid field was the only one where this prefix was missing. Did you see that?

On this example page two field should be hidden.
Did you see the page not correctly returning when empty on the "wpcf-tables-figures" field and "wpcf-video-media"?

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.