Support

Account

Home Forums ACF PRO acf/save_post + post_meta array + shortcodes

Solved

acf/save_post + post_meta array + shortcodes

  • Howdy,

    I’m using acf/save_post to save multiple ACF fields as array values in one post_meta field. I’m then fetching this one post_meta field in my theme.

    When I have a WYSIWYG field, I’ve noticed that shortcodes get processed differently if they are in quotes.

    For example, my WYSIWYG field value (I’m using htmlentities():

    
    [my_url]
    <a href="[my_url]">Link</a>
    

    My post_meta field then looks like:

    
    a:73:{s:43:"
    [my_url]
    <a href=&quot;http://www.mysite.com/my-url/&quot;>Lin</a> // The URL Shortcode is running, the < and > get replaced wtih & l t ; ...but in this forum it's auto-replacing them
    "}
    

    Is there any way that I can prevent shortcodes from running when saving such value?

    Thanks in advance!

  • That’s interesting. Is that the way it is saved by ACF or is WP’s automatic serialization of the array converting the quotes to &quote? What does the array/value look like before it’s saved in your new meta field?

  • Hey John!

    Thanks for the reply!

    Sure, here’s my acf/save_post code:

    
    function my_post_field_array_save($post_id) {
    	if( have_rows('my_repeater',$post_id) ): 
    		while ( have_rows('my_repeater',$post_id) ) : the_row();
    			remove_filter('acf_the_content', 'wpautop');
    			// remove_filter('acf_the_content', 'do_shortcode'); Could I do something like this?
    			$my_repeater_arr['my_repeater_wysiwyg'] = htmlentities(get_sub_field('my_repeater_wysiwyg',$post_id));
    			add_filter('acf_the_content', 'wpautop');
    		endwhile;
    		update_post_meta($post_id,'my_repeater_meta',$my_repeater_arr);
    	endif;
    };
    add_action('acf/save_post', 'my_post_field_array_save', 20);
    

    Here’s the actual (raw) WYSIWYG content:

    
    [site_url]
    <a href="[site_url]">URL</a>
    

    Here’s the array (no post/save):

    
    array(1) {
      ["my_repeater_wysiwyg"]=>
      string(62) "[site_url]
    <a href="[site_url]">URL</a>"
    }
    

    Here’s the array after (i.e. serialized in the post_meta field…using the pastebin below because this ACF forum converts my HTML entities): http://pastebin.com/iZqffdq1

    Thanks in advance!

    Ryan

  • You’re running htmlentities on the value of the WYSIWYG field

    
    $my_repeater_arr['my_repeater_wysiwyg'] = htmlentities(get_sub_field('my_repeater_wysiwyg',$post_id));
    

    This is going to convert the quotes and <> brackets in the value you store.

  • Hi John,

    Thanks for the reply. Yes, using htmlentities was one way that I tried to get around processing shortcodes, but unfortunately, this still happens anyways. (and the output of the shorcodes have all html entities replaced.

    In other words, I want the shortcode to be stored as [my_shortcode] and only output it when I call that field on the front end…now the field is actually storing the shortcode output itself. Hopefully that makes sense.

    Thanks in advance

  • If you want to not process the shortcodes, or for that matter run any of ‘acf_the_content’ filters when getting the value from ACF

    
    get_sub_field('my_repeater_wysiwyg', false);
    // second parameter tells ACF not to format the value
    

    I just noticed that you are using the $post_id parameter in get_sub_field(). This function does not have a $post_id parameter, it uses the same ID given in the have_rows() call. Setting it to the post ID you are basically setting format to true.

    Anyway, if it’s not formatted you can store it that way and then do

    
    $content = apply_filters('acf_the_content', $value_from_your_stored_array);
    

    when you actually want to display the content that value holds

  • By the way, I just did something similar in this plugin https://github.com/Hube2/acf-options-page-adder. I added 2 wysiwyg fields to it. All of the settings in the the plugin are actually store in post_content of my post type, this speeds up loading because I don’t need to get the values of each field.

    You can see where the stored values are displayed in the function customize_end() on line 677 of the main file. I actually use get_post_meta() to retrieve the values so that part probably won’t help you, but if you want to look at that the function that gets the values and stores them in an array is on line 578 build_options_page_settings()

  • Thanks John, that did the trick!

    Also thank for mentioning your plugin, looks like cool stuff and I’ll give it a go.

    Thanks again!

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘acf/save_post + post_meta array + shortcodes’ is closed to new replies.