Skip Navigation

[Resolved] How to set CF Conditions programmatically

This support ticket is created 3 years, 8 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by smileBeda 3 years, 7 months ago.

Assisted by: Waqar.

Author
Posts
#2004463
Condition when set purely with the GUI.png
My Custom Set Condtion.png

Tell us what you are trying to do?
==> Setting a Custom Field Condition programmatically

Is there any documentation that you are following?
==> None, since there is neither an API nor a DOC, however I have the requirement.

Is there a similar example that we can see?
==> Yes, see code below

I successfully create some complex Custom Fields upon the activation of a Plugin I created.
This works fine but I want to add a conditional to 2 of those automatically created fields, and of course I'd like to do that automatically as well.

So I set a condition for the fields when registering them (note, this is excerpt from larger code):

...
		$conditional_display = array(
			'relation'		=> 'AND',
			'conditions'	=> array(
				uniqid('condition_')	=> array(//Createa Unique ID. Toolset uses some hash here, but it shouldn't really matter.
					'field' 	=> 'Exclude from SiteMap',//The field that sets the condition
					'operation' => '<>',//Not equal
					'value' 	=> '1',//Value to compare to
					'month' => '03',//Why, I am not sure, but Toolset fields add a date here to the conditional so I just add it as well.
					'date' => '29',
					'year' => '2021',
				),
			),
			'custom' 		=> null,//Toolset default is empty here so I assume I shouldn't fill it either.
            'custom_use' 	=> 0,
		);
...

Then I pass this conditional setting to the data array of WPCF Fields like so (again, excerpt from larger code):

...
$this->plugin_name .'-frequency' => [
    'type' => 'select', 
    'name' => 'Update Frequency', 
    'data' => [
        'options' => $frequency_options, 
        'conditional_display' => $conditional_display, 
        'submit-key' => 'select-frequency-always',
         'disabled_by_type' => 0
    ], 
    'description' => 'Sitemap XML Frequency this Post will be updated with (how often do you plan to update this post)'
],
...

Save the fields with

...
wpcf_admin_fields_save_fields( $fields );
...

Obviously above is part of a much larger code (https://pastebin.com/WF8U4H11) but it should illustrate what I am doing.
The Condition is well set in the GUI but it does not apply (see also screenshots)

Perhaps you can point me to what I am missing.

The main problem seems to be that when we save a condition in the GUI, it triggers something else in the background additional to what is stored in PHP, which makes the magic work, and which is what I miss.

However, I see zero difference when I var_dump the fields settings. Both, wether saved with custom code or types GUI, have the precise same values in the conditionals settings.
So I assume it should be working, but it is not.

Perhaps a Developer can hint towards the right function to use additionally, as it would be really nice to add this feature to a plugin I am making for SEO Toolset Add-on.

If its not possible to hint I also understand. I tried to have a look at the API here as well but I think it is not useful for this kind of things?
https://toolset.com/documentation/programmer-reference/toolset-custom-field-api/
It seems to be a complex way to get fields, but that I can do already with simple get_post_meta and get_posts, whereas setting the fields is more complex 🙂

I would appreciate any hint as of why my Conditional is not "working"

#2004737

Hi Beda,

Thank you for contacting us and I'd be happy to assist.

I'm afraid, I don't have any pointers to add to this, so I've passed on your request to the concerned team, for further review.

I'll keep you updated through this ticket.

regards,
Waqar

#2008167

Hi Beda,

I've received an update from the developers about your code review request.

It turns out some major improvements are in the pipeline to revamp how the Types fields are managed in the backend, which will also make programmatic registration of fields much simpler.

They started looking into the code, but couldn't help but notice that troubleshooting and research into this legacy way is going to be very time-consuming, especially considering that the completed solution will be unstable and bound to become obsolete.

I hope you'll undertand.

regards,
Waqar

#2008327

That is OK Waqar, right now I will simply not add that feature as it seems plain impossible to do with the existing (internal, undocumented) "API", and probably needs some JS action to be fired additionally.

I'll dig perhaps into it in a later moment, and hope that one day we get a PHP API to fully control the Fields not just in out put but also input/creation.
This will be a boost for potential integration/add-on development for Toolset.

Thanks.