Skip Navigation

[Resolved] PHP Custom Code for Toolset Custom Fields

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

Problem

I want to write a PHP Code that does some things with the Toolset Custom Fields.

Mainly, it should display the things conditionally, for example, if the field is not empty, it should display something, otherwise not.

Can you help me?

Solution

The Toolset is crafted with this one purpose in mind:
Avoid code.

Everything in the described goal can be achieved using only the Toolset GUI and no PHP.

However, for PHP solutions there is a good „mini-tutorial“ linked below.

This support ticket is created 6 years, 9 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by simranjeetS-2 6 years, 9 months ago.

Assisted by: Beda.

Author
Posts
#621544

Tell us what you are trying to do?
I am trying to show fields conditionally by editing single.php file. This is my shortcode which I got from views. But how to use it in php after I deactivate views?
[types field='enrollment-number'][/types][wpv-conditional if="( $(wpcf-legal-type) eq 'Lawyer' )"]

In other words, if field legal type has value lawyer, only then, I want to show value of enrollment number.
Also, the value is to be shown like this:
Enrollment Number: "the number"

Is there any documentation that you are following?
https://toolset.com/forums/topic/conditional-display-of-fields-using-php-code-if-empty-dont-display/
https://toolset.com/documentation/beyond-the-basics/conditionally-show-content/
https://toolset.com/documentation/user-guides/types-custom-fields-conditional-display/

Is there a similar example that we can see?
No.

What is the link to your site?
hidden link

#621659

ShortCodes, in WordPress, first must be registered.

That is exactly what the Views Plugin does, and hence, if you disable Toolset Views, all its ShortCodes will stop working.

You must keep Toolset Views active if you want to use its ShortCodes or API somewhere.

Now, to conditionally show a Field in PHP I do not suggest to use the ShortCodes HTML conditional, as that was created for the purpose to avoid the need of PHP.

In PHP, conditions are set with if/else/elseif.
hidden link

We have a large Documentation on how to display fields with PHP:
https://toolset.com/documentation/customizing-sites-using-php/

To display a Field only if its value is "xy", in PHP, you need to first get_post_meta() the field's value, put it in a $variable, and then compare that to your value of choice.
If the result is true, you show the Field either with echo get_post_meta() or echo types_render_field()
https://developer.wordpress.org/reference/functions/get_post_meta/
https://toolset.com/documentation/customizing-sites-using-php/functions/

This falls under Custom Code, I can help with the types related fields display if you have issues with them.

#621668

Ok. Views is activated. How to add [types field='enrollment-number'][/types][wpv-conditional if="( $(wpcf-legal-type) eq 'Lawyer' )"] in single.php?

#621671

As I said, you should not use the HTML Conditional in PHP, the HTML conditional was exactly created to be used when you do not want to code.

The entire Toolset is crafted with this one purpose in mind:
Avoid code.

Anyway, I elaborated here how to display the custom field conditionally in PHP:
https://toolset.com/forums/topic/php-code-wanted-for-conditional-display-of-fields/#post-621659

This below is a sample code using Toolset API:

//Get the Field and assign it's value to a variable
//Note that I use types_render_field() and hence, the Field slug does NOT need the wpcf- prefix
//https://toolset.com/documentation/customizing-sites-using-php/functions/
$field = types_render_field( 'toolset-line', array() );

//Now, check if the field is not empty, or of any value of your choice
if (!empty($field)) {

  //If it's not empty, echo it
  echo $field;
}

This code, in turn, does the same with the WordPress API:

//define the global $post
global $post;

//Get the Field and assign it's value to a variable
//Note that I use get_post_meta() and hence, the Field slug does NEED the wpcf- prefix
//https://developer.wordpress.org/reference/functions/get_post_meta/
$field = get_post_meta( 'wpcf-toolset-line', $post->ID, true );

//Now, check if the field is not empty, or of any value of your choice
if (!empty($field)) {

  //If it's not empty, echo it
  echo $field;
}

These are good examples to use as a start to learning the usage of Toolset and WordPress API.
Together with PHP, you will be able to compare this field's value to any other value you need and display it only if the conditions are true.

However, I cannot craft a full custom code for you - if you require this I suggest you contact a Contractor:
https://toolset.com/contractors/

#622232

Thanks a lot.