Skip Navigation

[Resolved] How to use custom fields created with Toolset as snippet variables in Yoast

This support ticket is created 3 years, 4 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.

This topic contains 8 replies, has 2 voices.

Last updated by Waqar 3 years, 4 months ago.

Assigned support staff: Waqar.

Author
Posts
#1246827

I am trying to: use custom fields created with Toolset as snippet variables in Yoast (WP SEO). This is possible with WP custom fields as described in the following link:
hidden link

Is this possible with Toolset custom fields?

I don't think debug info is relevant in this case, but I've included it just in case.

#1246899

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Michael,

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

As explained in the link that you've shared ( hidden link ), simple custom fields like a single line type can be used directly in snippet variables, in this format:


%%cf_<custom-field-name>%%

For example, if you have a Toolset custom field with the slug "book-info", it can be used like this:


%%cf_wpcf-book-info%%

Note: By default, Toolset custom fields are saved with a "wpcf-" prefix appended to their slugs.

For other complex type custom fields like radio buttons, checkboxes, etc, you can register a custom template variable ( refer to the section "Registering a custom template variable" from that same link that you shared ).

Note: To get the Toolset custom field values in your custom code, you can use "Types Fields API":
https://toolset.com/documentation/customizing-sites-using-php/functions/

I hope this helps and for personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1246979
yoast snippets.png
custom-field.png

Hi, thanks for your reply.

I have tried your suggestion but can't get it to work. I'm using a simple, single line text field. Here are some screenshots. What am I doing wrong?

#1246996

PS - I'm using the Toolset Types plugin, so I assume I DO NOT need to edit my functions.php file as described in the link you sent (https://toolset.com/documentation/customizing-sites-using-php/functions/). Is that correct?

#1247027

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Michael,

Thanks for writing back.

The field with the same slug works as expected on my test website:

Screenshot - Custom field setting:
hidden link

Screenshot - Custom field value:
hidden link

Screenshot - Custom field in the snippet preview:
hidden link

I'll recommend testing this with all extra plugins (other than Toolset and Yoast SEO) disabled and with a default theme like Twenty Nineteen. This will help in narrowing down to a possible conflict.

Any custom PHP code that you need to execute on your website can be added into either the active theme's "functions.php" file or Toolset's own custom code section, as explained in this guide:
https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

regards,
Waqar

#1247029

Thanks for testing.

I'm still not clear about whether or not I need to add Toolset custom field values into my custom code.

Did you have to modify your php files (as described here) for it to work in your test?

Did you add this code?

<!--?php echo(types_render_field( 'wpcf-job-title', array( 'arg1' => 'val1', 'arg2' => 'val2' ) )); ?-->
#1247051

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Michael,

I apologize if my mentioning of custom code resulted in confusion.

Let me clarify this further. My earlier message can be divided into two parts, as shown in this screenshot:
hidden link

For part 1, where you just need to use a Toolset custom field's value, which is a simple "single line" type field, no custom code changes will be needed and the format %%cf_<custom-field-name>%% works inside the Yoast SEO's snippet preview section.

The screenshots from my last message confirm this as I didn't include any custom code for this testing.

The custom code will be needed for part 2, where you need to use a value from Toolset custom field which stores data in a complex way, for example, the radio buttons, checkbox or checkboxes type fields. If you only need values from "single line" type custom fields, you won't need any custom code.

I hope this clarifies and please let me know if you need any further assistance around this.

regards,
Waqar

#1248269

OK. This seems to be working (sort of). It does not populate the snippet immediately. I have to save the post and then re-open it to see the change. Is this how it worked in your test?

#1248985

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Michael,

Thanks for the update and glad that it works.

On my test website, the snippet preview updates with the value of the custom field immediately/on the fly, without having to save or update the post.
( screencast: hidden link )

Can you please check your browser's console for any script errors or warnings?
( ref: https://wordpress.org/support/article/using-your-browser-to-diagnose-javascript-errors/ )

The dynamic content changes/updates can be affected if there are any script errors or warnings.

regards,
Waqar