Support

Account

Home Forums ACF PRO Problem with acf_form () Reply To: Problem with acf_form ()

  • 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 <contato@advert.com>";
                // $headers[] = "Reply-To: no-reply <no-reply@advert.com>";
                // $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  );