Support

Account

Home Forums Front-end Issues Creation and edition of a post

Solved

Creation and edition of a post

  • Hello all,
    I need some help ! i am trying to open a post which is supposed to create it, then to edit it. The post is created, but none is edited. There is something I miss somewhere. An idea ? thanks.

    
    <?php
    /**
     * Template Name: Page Creation Vigneron3.0
     */
    
    ?>
    
    <?php
    function my_pre_save_post( $post_id ) {
        // check if this is to be a new post
        if( $post_id != 'new' ) {
            return $post_id;
        }
        // Create a new post
        $post = array(
           'post_status'  => 'publish' ,
            'post_title'  => 'Ici, le nom du Domaine, Vignoble ...' ,
            'post_type'  => 'page' ,
    	'page_template'  => 'page_vigneron30.php',		
        );  
        // insert the post
        $post_id = wp_insert_post( $post ); 
    
        // return the new ID
        return $post_id;
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
    ?>
    
    <?php acf_form_head(); ?>
    <?php get_header(); ?>
    
    	<div id="primary" class="content-area">
    		<div id="content" class="site-content" role="main">
    		<?php 
    		acf_form(array(
    			'post_id'		=> 'new',
    			'new_post'		=> false,
    			'submit_value'		=> 'Create a new event'
    		)); 
    		?>
    
    		</div><!-- #content -->
    	</div><!-- #primary -->
    
    <?php get_footer(); ?>
    
  • Ok, now, it is better, but not ok yet. In that case, I create a new record, and then I can edit it, at least the variables included in the acf group. But if I type something in the title field or the content field, I do net see that something in the record !

    <?php
    /**
     * Template Name: Page Creation Vigneron3.0 2
     */
    
    ?>
    <?php
    // fonction ACF --------------------------------------------
    // add_action('wp_head','acf_form_head');
    
    function my_pre_save_post( $post_id ) {
    // check if this is to be a new post
       if( $post_id != 'new' ) { return $post_id; }
       // Create a new post
    
       $post = array(
    		'post_status'  => 'publish' ,
            'post_title'  => 'Ici, le nom du Domaine, Vignoble ...' ,
            'post_type'  => 'page' ,
    		'page_template'  => 'page_vigneron30.php',		
        );  
    
       // insert the post
       $post_id = wp_insert_post( $post );
    
       $_POST['return'] = add_query_arg( 
    			array(
    					'post_id' => $post_id
    					), 
    			$_POST['return'] 
    			);  
       // return the new ID
       return $post_id;
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    
    // ---------------------------------------------------------------------------------
    
    ?>
    
    <?php acf_form_head(); ?>
    <?php get_header(); ?>
    
    	<div id="primary" class="content-area">
    		<div id="content" class="site-content" role="main">
    		<?php 
    			$args = array(
    				'post_id'    => 'new',
    				'post_title' => true,
    				'post_content' => true,
    				'field_groups' => array( 5015 ),
    				'submit_value' => "Valider cette nouvelle action"
    			);
    			acf_form( $args ); 
    		?>
    
    		</div><!-- #content -->
    	</div><!-- #primary -->
    
    <?php get_footer(); ?>
    
  • And now ? it is working perfectly !

    
    <?php
    /**
     * Template Name: Page Creation Vigneron3.0 2
     */
    
    ?>
    <?php
    // fonction ACF --------------------------------------------
    // with this i dont have to think to put acf_form_head whereever I need it, it is always there
    add_action('wp_head','acf_form_head');
    
    function my_pre_save_post( $post_id ) {
    // check if this is to be a new post
       if( $post_id != 'new' ) { return $post_id; }
       // Create a new post
       $post = array(
    		'post_status'  => 'publish' ,
    		'post_title'   => $_POST['fields']['field_5640802e05f78'],
     		'post_content'   => $_POST['fields']['field_5640802e05f79'],
    		'post_type'  => 'page' ,
    		'page_template'  => 'page_vigneron30.php',		
        );  
       // insert the post
       $post_id = wp_insert_post( $post );
       $_POST['return'] = add_query_arg( array('post_id' => $post_id),$_POST['return']);  
       // return the new ID
       return $post_id;
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    
    function my_acf_save_post( $post_id ) {
        // Don't do this on the ACF post type
        if ( get_post_type( $post_id ) == 'acf' ) return;
    
        // Get the Fields
        $fields = get_field_objects( $post_id );
    
        // Grab Post Data from the Form
        $post = array(
    		'ID'			=> $post_id,
      		'post_title'   	=> $fields['titre']['value'],
     		'post_content'  => $fields['contenu']['value'],
       );
        // Update the Post
    	wp_update_post( $post,true );
    }
    add_action( 'acf/save_post', 'my_acf_save_post', 10, 1 );
    ?>
    
    <?php get_header(); ?>
    
    	<div id="primary" class="content-area">
    		<?php 
    			$args = array(
    				'post_id'    => 'new',
    				'post_title' => false,
    				'post_content' => false,
    				'field_groups' => array( 5015 ),
    				'submit_value' => "Valider cette nouvelle action"
    			);
    			acf_form( $args ); 
    		?>
    
    	</div><!-- #primary -->
    
    <?php get_footer(); ?>
    
  • Hi mouzaia,

    Good day

    Have you encounter an issue with the code? I have tried the code but when I submit the form it will display a blank white page but it saves the data that I’ve fill-in.

    Hope to hear from your end.

  • Hello,

    For me, it works fine, did you change the names of the fields ?
    If the screen is blank, you should have a php error, with an error message. Do you “display_errors=1” in your php.ini ? If you dont have access to php.ini, i think you can put that instruction in .htaccess

    Good luck

  • I figure it out I need to put in the function and action on the page template itself to have it work.

    If I put the function and action in function.php/somewhere outside the page template it will display blank white space.

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

The topic ‘Creation and edition of a post’ is closed to new replies.