Support

Account

Home Forums Backend Issues (wp-admin) acf/save_post Shortcodes

Solved

acf/save_post Shortcodes

  • Is there any way to use something like this? (just posting the relevan part of code, i know how to use acf/save_post with add_action and so on):

    ob_start();
    get_template_part(‘my-layout’);
    $builder_out .= ob_get_clean();

    $post[‘post_content’] = $builder_out;

    remove_filter(‘acf/save_post’, ‘builder_save_post’, 20);
    wp_update_post( $post );
    add_action(‘acf/save_post’, ‘builder_save_post’, 20);

    Where “my-layout.php” could have many things, custom fields of course, but what i´m interested is when using something like this inside the template:

    echo do_shortcode(‘[shortcode]’);

    When a do_shortcode is saved into the post_content, what is saved is the result of that, not the [shortcode] string only.

    Could be very long to explain why i need this, but i need it 🙂

    How can i tell acf/save_post not to filter the do_shortcode? (may be that´s what i´m asking in fact)

    Should i do something on pre_save maybe?

    Thanks so much.

  • Hi @rgdesign

    I’m afraid I don’t understand the situation. Could you please share some examples of the current result and how you want it to be?

    Thanks 🙂

  • Ok, this is the entire function:

    function builder_save_post($post_id){
    	
    	global $post;
    	global $saving_build;
    	$saving_build = true;
    	$builder_post = array();
        $builder_post['ID'] = $post_id; 
    	
    	if ( ! wp_is_post_revision( $post_id ) ){ 
    	
    		$set_template = get_post_meta( $post_id, '_wp_page_template', true );
    		
    		$do_build = false; 
    		$builder_out = '';
    		
    		if( '_template-acf-builder.php' == $set_template && have_rows('bc_builder') ){ 
    			$builder_out .= '<div class="bc_builder">'; 
    			while( have_rows('bc_builder') ){
    				the_row();
    				 
    				$layout = get_row_layout(); 
    				 
    				if( !empty($layout) ){
    					ob_start();
    					get_template_part('layout/builder', $layout.'-save_post'); 
    					$builder_out .= ob_get_clean();
    				}
    			}  
    			$builder_out .= '</div>';
    			$do_build = true;
    		}
    		
    		$builder_post['post_content'] = $builder_out; 
    		
    		if($do_build){ 
    			remove_filter('acf/save_post', 'builder_save_post', 20);  
    			wp_update_post( $builder_post );
    			add_action('acf/save_post', 'builder_save_post', 20); 
    		}
    		
    		$saving_build = false;
    	}
    }
    add_action('acf/save_post', 'builder_save_post', 20);

    As you can see i´m using get_template_part to get php files iniside /layout/ folder, this templates are named with same name of the flexible layout ($layout variable).
    So if layout is “gallery”, the template is layout/builder-gallery-save_post.php.

    It works perfectly, data is saved the right way. But….

    Let´s say i inside on of those templates i have something like this:

    The title: "<? the_title() ?>"
    The shortcode: <? echo do_shortcode('[shortcode]'); ?>

    Where “shortcode” is just an example, could be any shortcode. Let´s imagine that this shortcode outputs only a string like “Hello”, so, this is what is saved into the post_content till now:

    Post title: "My post title"
    The shortcode: Hello

    The last “The shortcode: Hello” is the problem, i don´t whant the result of the shortcode, i want this to be saved into post_content:

    Post title: "My post title"
    The shortcode: [shortcode]

    That´s it, i need to save just “[shortcode]” into the post_content, not the “Hello” result that this shortcode returns. So, i guess there´s must be some way to unfilter the do_shortcode before i get the template part or something like that.

    Sorry, it´s a complex situation and is hard to explain 🙂

  • Hi @rgdesign

    Then I think you need to check if the template is called from the acf/save_post hook or not.

    What I have in my mind is that I think you can use the global $_POST variable to check if there’s a posted data or not, which mean that the template is called from a saving process or it just included from another template. Maybe something like this:

    <?php
    // Check posted data
    if( isset($_POST['acf']['field_1234567890abc']) ){
        $the_shortcode = '[shortcode]';
    } else {
        $the_shortcode = do_shortcode('[shortcode]');
    }
    ?>
    The shortcode: <? echo $the_shortcode; ?>

    Where ‘field_1234567890abc’ is the field key that is posted.

    If that doesn’t work, I then you need to pass a variable to the template instead. This page should give you more idea about it: http://keithdevon.com/passing-variables-to-get_template_part-in-wordpress/.

    I hope this helps 🙂

  • Thanks so much James, sorry for taking your time since in fact now i realised this is nothing to do with ACF. But i thought could be something into the acf/save_post that i was missing.

    The idea you talked is similar what i first did and i think i´m going to do it that way at the end.

    All this think was becuse YOAST uses de post_content to analize things on the post edit screen, and tested some other things with yoast filters to let the custom fields to be analized, but no luck into that, so, i ended doing this thing with acf/save_post using the same front-end template to save the post_content.

    By the way this way makes the thing available to any other SEO plugin that uses the post_conntent. And in fact if i develope it more, i will able to use just the_content() on the template, since all the html is there on the saved post_content…. one day i will leave this on github, is like a page builder using flexible content, and i have all programed so the only thing i need to do is create a new layout and a template with same name, ready to use with just a few clicks 🙂

    At this moment i have created many layouts that are very general, so i can use them on almost any site and not creating custom particular things for every site. A lot of time saved when developing.

    Well, that´s all, have a nice day.

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

You must be logged in to reply to this topic.