Hi,
I am trying to get the raw (database content) from a custom types field using types_render_field() with 'output' => 'raw'. I need the shortcode to extract the image ids. This worked fine until I updated Types from 2.1 to 3.0.1. So manually changing the type or structure is not an option.
I checked the database if the content had been changed while updating Types and the database still has the raw shortcode.
So I debugged the code and saw that in the file /wp-content/plugins/types/application/models/field/service.php the function public function render_frontend() at line 137 (
$output = $view->render();
) converts/renders the gallery shortcode into html source code.
Could you help me figure out if I am using types_render_field() wrong or if maybe there's a bug in Types?
I attached some screenshot so you can check the values.
Thanks a lot for your help!
Hello,
How do you setup the custom field "image-gallery"? is it a custom text field? can you take a screenshot for it?
How do you setup your PHP codes? can you copy/paste it here?
I need to test and debug it in my localhost, thanks
Hi,
I am using a WYSIWYG which allows the user to create a gallery using WordPress native behaviour. I attached some screenshot as you asked.
Here's the code I am using
...
$sliderItems = localizedContent($exhibition->ID, 'image-gallery', $exhibition->post_type, 'metadata', true, 'fr', array('output' => 'raw', 'suppress_filters' => 'true'));
...
function localizedContent($id, $field, $type, $returnType = 'id', $raw = false, $lang = ICL_LANGUAGE_CODE, $args = array()) {
$original = get_wpml_object_id($id, $type, true, $lang);
$result = false;
if ($returnType === 'id') {
$result = $original;
}
if ($returnType === 'metadata') {
$result = getMetadata($field, $original, $raw, $args);
}
if ($returnType === 'text') {
$result = get_post($original)->$field;
}
if (!$result) {
$original = get_wpml_object_id($id, $type, true, 'fr');
$result = false;
if ($returnType === 'id') {
$result = $original;
}
if ($returnType === 'metadata') {
$result = getMetadata($field, $original, $raw);
}
if ($returnType === 'text') {
$result = get_post($original)->$field;
}
}
return $result;
}
...
function getMetadata($fieldName, $id, $raw = false, $args = array()) {
if ($raw) {
return types_render_field($fieldName, array_merge(array('post_id' => $id, 'output' => 'raw'), $args));
}
else {
return types_render_field($fieldName, array_merge(array('post_id' => $id), $args));
}
}
...
Thanks for your help!
You can use wordpress built-in function get_post_meta to get custom field value, for example, modify this line from:
return types_render_field($fieldName, array_merge(array('post_id' => $id, 'output' => 'raw'), $args));
To:
return get_post_meta($id, 'wpcf-' . $fieldName, true);
Yes, I also thought of that solution. But I am unsure if it is a good one since types_render_field() also allows you to pass other attribute arguments. Are they being ignored when 'output' => 'raw' is also being passed as argument?
In this special case your solution would work but I have a lot of code in many places and other projects which could break.
Yes, you are right, the attribute 'output' => 'raw' does not work, it is a known bug, I have added this thread into our to-do list, And our developers are working on it, I will update this thread if there is anything news.
Since you are going to get the raw value of the custom field, I don't think you need other parameters, so in this case, you can use wordpress function "get_post_meta" instead of Types function "types_render_field"
Ok, thanks for the confirmation.
I will wait for the bugfix and in the meantime use the workaround.
Thanks for the patience, our developers are working on it, I will update this thread if there is anything news.
Here is the feedback from our developers:
This will be fixed with the upcoming release Types plugin version 3.0.2, it should be release soon.
Hi,
I noticed that the update has been recently released and tested the fixed. Unfortunately the update didn't solve the issue.
I attached a screenshot which shows the watch of the function when debugging (with the arguments).
Thanks for your help!
Thanks for the feedback, I can duplicate the same problem with custom WYSIWYG field, and put it int our to-do list, our developers will take care of it.
Here is the feedback from our developers:
The issue will be fixed with the next release of Types plugin. In about 2-3 weeks.
Hi,
thanks for the feedback and great to hear!
Types plugin version 3.1 is released, you can download it here:
https://toolset.com/account/downloads/
Please test and feedback if the problem is fixed.
Hi,
thanks for the update and yes it works now! 🙂