Skip Navigation

[Resolved] Using types_render_field on the backend fails

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

Problem:
Using types_render_field on the backend fails, in this case breaking WPML product translations.

Solution:
types_render_field is intended for use on the front-end only.

If writing code for the backend that requires retrieving custom fields, use get_post_meta.

Relevant Documentation:
https://developer.wordpress.org/reference/functions/get_post_meta/

This support ticket is created 6 years 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by Gason 6 years ago.

Assisted by: Nigel.

Author
Posts
#627587

I am storing the value of a textfield which is in the backend (woocommerce product) into a variable then displaying it on the front end. I had no issues with this until recent updates. Now this code seems to break WPML. The woocommerce multilingual product translation page does not load with this file active. The file is in my child theme in a woocommerce sub-folder.
woocommerce/single-product/stock.php

The full (quite short) file is here https://pastebin.com/GEgSi4jt
The lines that cause the problem are the ones that try to store the value of the textfield in the backend in the variable $baba_stock_message

$baba_stock_message = types_render_field("baba-stock-message", array('show_name' => 'false'));

Later I echo this message to the front end.
Am I doing the storage of that variable the wrong way?

wrapping that line in

if (function_exists('types_render_field')) {

stops WPML breaking but I am not sure if my textfield will still be displayed on the front end.

You can see my WP error log for when this is active and woocommerce multilingual fails to load her if needed. It si short just the relevant parts https://pastebin.com/BxfTkVjX

#627745

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi there

types_render_field is only available on the front end, not in the back end, which is probably why checking the function exists appears to fix the issue.

Rather than use types_render_field you can retrieve the custom field directly from wp_postmeta using get_post_meta: https://developer.wordpress.org/reference/functions/get_post_meta/

Note that Types fields are stored with a prefix of 'wpcf-', so the correct key to use in get_post_meta would be 'wpcf-baba-stock-message"

#629014

This is the code I used instead of the types_render_field code

$baba_stock_message = get_post_meta(get_the_ID(), 'wpcf-baba-stock-message', true);
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.