What ACF version do you use, 4 or 5?
Doing shortcodes via filters for table fields.
// ACF Version 5
function my_acf_format_value_v5 ( $value, $post_id, $field ) {
$value = do_shortcode( $value );
return $value;
}
add_filter( 'acf/format_value/type=table', 'my_acf_format_value_v5', 10, 3 );
// ACF Version 4
function my_acf_format_value_v4 ( $value, $post_id, $field ) {
/* The value of a table field is given as an array in ACF version 4
and has to be converted into a string (JSON) for doing shortcodes. */
if ( $field['type'] == 'table' ) {
$value = wp_json_encode( $value );
}
$value = do_shortcode( $value );
/* Just convert JSON string back into an array. Be sure, that the second
parameter of json_decode() is true for returning an array, not object. */
if ( $field['type'] == 'table' ) {
$value = json_decode( $value, true );
}
return $value;
}
add_filter( 'acf/format_value_for_api', 'my_acf_format_value_v4', 10, 3 );
Thread Starter
decay
(@decay)
I have ACF 5 Pro. I will be testing this when I get in the office and will report back. Thanks for getting back with me!
Thread Starter
decay
(@decay)
So I’m realizing that the function I was using previously is the same as what you have shown here. It still isn’t working, but it should be. I am going to test this on other tables I have built and see if I can get it to work there. It may have something to do with the table itself.
I’ll report back. I’m sure this will be helpful to someone down the road, as your support forum has been for me 🙂
Thread Starter
decay
(@decay)
So I have tested this in a few different ways and I cannot get the table to show after I enable shortcodes in functions.php and add the [shortcode] to the table cell. All of my other fields show the expected shortcode content, it is only the table fields that break. To be more specific: The entire table disappears after enabling shortcodes.
I thought that this may be caused by the structure of my custom fields. I have a flexible content field that is containing my table as well as all my other elements. I tried creating a table that is completely independent of all other elements and I have the same issue.
I am back at square 1 on this and cannot figure out why the table breaks with shortcodes. Any one else have this problem?
edit: I updated my ACF Pro to version 5.3.7 and my table fields plugin is current. WP version is only 4.3 Everything is working fine except for this shortcode/table issue that I am up against. I will not be updating my WP version tonight as it requires some help from my guys on the back end (this WP install is messy and was inherited from previous developers) but I will report back tomorrow if this fixes it.
Do you think this may be the issue Johann? Would you suggest I go to WP version 4.4 or the new 4.5 just released?
An issue could be, that the returning content of the shortcode contains special characters, which breaks the JSON format of the table data. A solution is to encode the content into JSON format and removing the surrounding quotes of the JSON string.
function my_shortcode( $atts ) {
$return = 'Something with JSON special characters like "quotes" and \backslashes.';
// encoding to JSON escapes JSON special characters
$return = json_encode( $return );
// removing surrounding quotes of the JSON string
$return = trim( $return, '"' );
return $return;
}
add_shortcode( 'foobar', 'my_shortcode', 10, 3 );
Thread Starter
decay
(@decay)
Johann, and anyone that may be watching this…
I have been distracted by some other needs with my page, so I have not been able to get back to this issue. I will look at this as again, as soon as I can. Thanks for the additional info.
Thread Starter
decay
(@decay)
I’m back! Sorry for my delay.
In the interest of full disclosure you should know that we are using “Shortcode exec php”
(I discovered this and it cleared up my problem quite a bit)
When I created a shortcode with:
return "Here is the new shortcode content";
I got the expected result.
When I used :
‘return “<i class=\”fa fa-times\”></i>”;`
I had this error:
Warning: Invalid argument supplied for foreach() in /srv/bindings/bacb3e3fda39463f9ad5632741cf2459/code/wp-content/themes/sahifa-child/template-article-1.php on line 345
I know that this is because it is receiving the wrong information, ie:
An issue could be, that the returning content of the shortcode contains special characters, which breaks the JSON format of the table data. A solution is to encode the content into JSON format and removing the surrounding quotes of the JSON string.
Which you had pointed out. So I have removed the “\” out of my return and everything works great.
Thank you for your help in this. I know it was a pretty simple issue, but you provided me everything I needed to get it figured out in a short time frame. This is such a great addition to ACF and it is used on every page that I create. (We use tons of tables…)
Thanks again and I hope this helps someone in the future.
Cheers!