Support

Account

Home Forums Backend Issues (wp-admin) Redirect from pre_save_post

Solved

Redirect from pre_save_post

  • Hi,

    I’m following tutorial to generate post in backend, and it works great!

    Link here

    But I would need to redirect user after part where we update post args

    
        // update $_POST['return']
        $_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
        // wp_redirect('/');
    
    

    my last line is not working .. do you have idea how to go about that?

    thanks for great pluggin!
    cheers

  • The last line is not working because it’s commented out. Is that what you’re referring to?

  • Hi,
    I intentionaly commented it out cause it does not work… that part I would like to change but don’t know to what to work 🙂

  • Can you post more of your code?

  • Sure,

    I call this in my custom plugin..

    
    /* acf post generate */
    
    function create_acf_post( $post_id )
    {
        // check if this is to be a new post
        if( $post_id != 'new' )
        {
            return $post_id;
        }
    
        // Create a new post
        $mytitle = get_user_meta( get_current_user_id(), 'nickname', true ) . ' - ' . $_POST['fields']['field_547959d79b64c'];
        $post = array(
            'post_status'  => 'draft' ,
            'post_title'  => $mytitle,
            'post_type'  => 'ifcc'
        );  
    
        // insert the post
        $post_id = wp_insert_post( $post );
    
        // update $_POST['return']
        $_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );    
    
        $user_id = get_current_user_id();
        update_user_meta( $user_id, 'done_free', '1' );     
        // wp_redirect('/');
    
        // return the new ID
        return $post_id;
    }
    
    add_filter('acf/pre_save_post' , 'create_acf_post' );
    

    and that is fired on frontend while after couple of pars are meet,, frontend, is single, is custom post (ifcc) and logged in..

    
    // =============================
    // frontend single ifcc logic 
    // =============================
    
    function cpt_front($query) {
        if ( !is_admin() && $query->is_main_query() ) {
            if ($query->is_single() && $query->post_type == 'ifcc' ) {
                if( is_user_logged_in() ){
                    $user_id = get_current_user_id();
                    $usernick = get_user_meta( $user_id, 'nickname', true );
                    update_field('submitee_name', $usernick);
                    $free = get_user_meta( $user_id, 'done_free', true );
                    if( $free == '1' ){
                        echo 'need to buy additional licence';
                    } else {
                        get_template_part( 'content', get_post_format() );
                        $args = array(
                            'post_id' => 'new',
                            'field_groups' => array( 68 )
                        );
                        acf_form( $args );
                    }
                } else {
                    // echo get_permalink( get_option('woocommerce_myaccount_page_id') );
                    echo do_shortcode('[woocommerce_my_account]');
                }
            }
        }
    }
    
    add_action('pre_get_posts','cpt_front');
    

    so, everything works cool, and post is inserted in backend and all, but redirect part if off somehow,,

    thanks

  • OK, I’ve come to this problem from another angle and did resolve that part like this:

    
    // =============================
    // frontend single ifcc logic 
    // =============================
    
    function cpt_front() {
        global $post;
        // this here is for making sure not to fall in redirect loop trick :)
        if( $post->ID == get_option('woocommerce_myaccount_page_id') ){
            return;
        }
    
        if ( $post->ID == 53 && !is_admin() ) {
    
            if( is_user_logged_in() ){
    
                acf_form_head();
                add_action( 'the_content', 'callsall' );
    
            } else {
                wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) ); 
                exit();            
            }
    
        }
    }
    
    add_action( 'template_redirect' , 'cpt_front' );
    

    this is only thing in my plugin to get handle on my cpt here, not from template file..

    the only ugly thing here is hardcoded postID inside,, but that would need to be saved in some variable and read..

    thanks

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

The topic ‘Redirect from pre_save_post’ is closed to new replies.