Support

Account

Home Forums ACF PRO Problem with acf_form ()

Solving

Problem with acf_form ()

  • I wore in version 4 the issue on the front-end normally.
    Now I’m having trouble because it does not works.

    What happened to version 5 and this statement below

    This filter is not required to create a new post due to the new_post argument added to acf_form() in v5.0.0

    How do I edit posts with other fields that require the filter?

  • Hi @jpcastro4

    It means that you don’t need to use wp_insert_post() in that filter anymore. ACF 5 will create it for you automatically based on the new_post argument. This page should give you more idea about it: http://www.advancedcustomfields.com/resources/using-acf_form-to-create-a-new-post/.

    You still can use it or use acf/save_post instead.

    I hope this helps.

  • Many thanks for the reply. After reading the documentation I actually found this statement with respect to version 5. So far so good. But what intrigues me is that I was using the code below and functioned normally. From one moment to another stopped working. Why did this happen?

    I need to perform various functions to save a post and also use the form ACF as a bridge to other functions.

    What is the solution to all this routine I need to perform?

    Below is scprit I was using and stopped working.

    function salvar( $post_id ) {
    
        return $post_id;
        
        echo "<script> alert('Ok Joao'); </script>";
    
        $field = $_POST['acf'];//resume a chamada do form
        
        $form_type = $field['form_type'];//diz qual é a CTP que deve guardar
        
        // check if this is to be a new post
        if( $post_id != 'new_post' ) {
               
            switch ($form_type) {
    
                case 'advert':
    
                //Tratamento das Cidades como Taxonomias
    
                $exist_state = term_exists( $field['field_569b07ac73afc'], 'estado_cidade' );
    
                if( $exist_state == 0 && $exist_state == null){
    
                    $state = array(
                        'description'=> 'Encontre anuncios em '. $field['field_569b07ac73afc'] ,
                        'slug' => sanitize_title($field['field_569b07ac73afc']),
                        //'parent'=> $parent_term_id
                    );
    
                    $state_save = wp_insert_term($field['field_569b07ac73afc'], 'estado_cidade', $state );
    
                    $state_save = $state_save['term_id'];
    
                }else{
    
                     $state_save = get_term_by('id', $field['field_569b07ac73afc'], 'estado_cidade' );
                       
                }
                    
                $exist_city = term_exists( $field['field_569b07c873afd'], 'estado_cidade' );
    
                if($exist_city == 0 && $exist_city == null){
    
                    $cidade = array(
                        'description'=> 'O ADVirtual oferece diversos anuncios em '. $field['field_569b07c873afd'] ,
                        'slug' => sanitize_title($field['field_569b07c873afd']),
                        'parent'=> $estado_save
                        );
    
                    $city_save = wp_insert_term($field['field_569b07c873afd'], 'estado_cidade', $cidade );
    
                    $city_save = $city_save['term_id'];
    
                }else {
    
                    $city_save = get_term_by('id', $field['field_569b07c873afd'], 'estado_cidade' );
                }
                    
                $taxcidade = array( 
                    'estado_cidade' => array( $city_save ) 
                );
    
                //echo "<script> alert('".$taxcidade."'); </script>";
    
                    $post = array(
                        'ID'       => $post_id,
                        'post_type'     => 'advert',
                        'post_status'   => 'draft',
                        //'post_author'   => $logged->ID,
                        'tax_input' => $taxcidade
                    );  
                            
                    //insert the post
                    $post_id = wp_update_post( $post );
                
                    return $post_id;
    
                break;
    
                default:
                    echo "<script> alert('Problema ao editar os dados'". $form_type ."); </script>";
                break;
            }  
            
    
        }else{
    
            //se nao for novo cai pra cá
            switch ($form_type) {
    
                case 'user':
                
                $user_email = email_exists($field['field_569a597e95338']);
    
                if ( $user_email) {
                        
                        //$user = get_user_by( 'email', $user_email );
    
                        echo "<script> alert('Usuario'". $user->ID .");";
    
                        $creds = array();
                        $creds['user_login'] = $field['field_569a597e95338'];
                        $creds['user_password'] = $field['field_569a597e95338'];
                        $creds['remember'] = false;
                        $user = wp_signon( $creds, false );
    
                        $logged = get_user_by( 'email', $field['field_569a597e95338'] );
                        $logged = $logged->ID;
    
                        echo "<script> alert('Usuario logado'); </script>";
    
                        wp_redirect( site_url('/ad?email='. $field['field_569a597e95338'] .'&author_id='. $logged ) ); exit;
    
                       //return 'user_'. $user->ID;
    
                }else{
    
                     $postDataUser = array(
                        'ID'         => $user_id,
                        'user_login' => $field['field_569a597e95338'],
                        'user_email' => $field['field_569a597e95338'],
                        'user_pass'  => $field['field_569a597e95338'],
                        'role'       => 'advertiser'
                    );
    
                    //register user
                    $user_id = wp_insert_user($postDataUser);
                    //$user_id = 'user_'.$user_id;
    
                    if($user_id){
                        $creds = array();
                        $creds['user_login'] = $field['field_569a597e95338'];
                        $creds['user_password'] = $field['field_569a597e95338'];
                        $creds['remember'] = false;
                        $user = wp_signon( $creds, false );
    
                        $logged = get_user_by( 'email', $field['field_569a597e95338'] );
                        $logged = $logged->ID;
    
                        wp_redirect( site_url('/ad?id='. $field['field_569a597e95338'] .'&author_id='. $user_id ) ); exit;
    
                    }                
    
                    //return $user_id;
    
                }
    
            break;
    
            case 'advert':
                
                $title = $field['field_569b032a73af9'];
                $postname  = sanitize_title($field['field_569b032a73af9']);
    
                //Tratamento das Cidades como Taxonomias
    
                $exist_state = term_exists( $field['field_569b07ac73afc'], 'estado_cidade' );
    
                if( $exist_state == 0 && $exist_state == null){
    
                    $state = array(
                        'description'=> 'Encontre anuncios em '. $field['field_569b07ac73afc'] ,
                        'slug' => sanitize_title($field['field_569b07ac73afc']),
                        //'parent'=> $parent_term_id
                    );
    
                    $state_save = wp_insert_term($field['field_569b07ac73afc'], 'estado_cidade', $state );
    
                    $state_save = $state_save['term_id'];
    
                }else{
    
                     $state_save = get_term_by('id', $field['field_569b07ac73afc'], 'estado_cidade' );
                       
                }
                    
                $exist_city = term_exists( $field['field_569b07c873afd'], 'estado_cidade' );
    
                if($exist_city == 0 && $exist_city == null){
    
                    $cidade = array(
                        'description'=> 'O ADVirtual oferece diversos anuncios em '. $field['field_569b07c873afd'] ,
                        'slug' => sanitize_title($field['field_569b07c873afd']),
                        'parent'=> $estado_save
                        );
    
                    $city_save = wp_insert_term($field['field_569b07c873afd'], 'estado_cidade', $cidade );
    
                    $city_save = $city_save['term_id'];
    
                }else {
    
                    $city_save = get_term_by('id', $field['field_569b07c873afd'], 'estado_cidade' );
                }
                    
                $taxcidade = array( 
                    'estado_cidade' => array( $city_save ) 
                );
    
                $post = array(
                    'ID'            => $post_id,
                    'post_type'     => 'advert',
                    'post_status'   => 'draft',
                    'post_author'   => $logged->ID,
                    'post_title'    => $title,
                    'post_name'     => $postname,
                    'tax_input'     => $taxcidade,
                );  
                            
                // insert the post
                $post_id = wp_insert_post( $post );
                
                return $post_id;
    
                wp_redirect( site_url('/ad?edit_ad='.$post_id.'&email='. $field['field_569a597e95338'] .'&author_id='. $user_id ) ); exit;
    
                // $to = $field['field_565b0d414e11f'];
                // $subject = "AdVirtual";
                // $message = "Você cadastrou o anuncio" . $field['field_565a79b776849'] ;
                // $headers[] = "From: Sérgio <[email protected]>";
                // $headers[] = "Reply-To: no-reply <[email protected]>";
                // $headers[] = "Content-type: text/html; charset=UTF-8";
    
                // wp_mail( $to, $subject, $message, $headers );
            break;
    
            default:
                echo "<script> alert('ERRO - Nova CTP - Entre em contato com o suporte urgente.'); </script>";
            break;
    
            }
    
        }
    
    }
    add_filter('acf/pre_save_post' , 'salvar', 10, 1  );
  • Hi @jpcastro4

    Could you please tell me what’s wrong with that code? Which section is not working? Could you please test each logic so we can know which part of the code that doesn’t work as it should be. Also, please check this page about debugging ACF: https://www.advancedcustomfields.com/resources/debug/?

    Thanks!

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

The topic ‘Problem with acf_form ()’ is closed to new replies.