Marko I.
Marko I.

Reputation: 562

Redirecting form submission to another page issue

On my Wordpress site, I would like to redirect it to a page like: example.com/user after submitting the form.

:method:POST
:path:/dash/product/edit/

here is the beginning of the form:

<!--  Submission Form -->
<form method="post" action="" id="wcv-product-edit" class="wcv-form wcv-formvalidator"> 

and the end of the form:

<?php WCVendors_Pro_Product_Form::form_data( $object_id, $post_status ); ?>
            <?php WCVendors_Pro_Product_Form::save_button( $title ); ?>

I tried placing this inside of a form:

header('Location: http://www.example.com/user');

But this redirects whole page where the form is located without form submission to example.com/user. Form is located on this path: /dash/product/edit/. So when I request this path (where the form is located) it just redirects to example.com/user

Form controller:

public function process_submit() { 

        if ( ! isset( $_POST[ '_wcv-save_product' ] ) || !wp_verify_nonce( $_POST[ '_wcv-save_product' ], 'wcv-save_product' ) || !is_user_logged_in() ) { 
            return; 
        }

        $can_submit_live        = WC_Vendors::$pv_options->get_option( 'can_submit_live_products' ); 
        $current_post_status    = isset( $_POST[ 'post_status' ] ) ? $_POST[ 'post_status' ] : ''; 
        $can_edit_approved      = WC_Vendors::$pv_options->get_option( 'can_edit_approved_products' ); 
        $trusted_vendor         = ( get_user_meta( get_current_user_id(), '_wcv_trusted_vendor', true ) == 'yes' ) ? true: false;
        $untrusted_vendor       = ( get_user_meta( get_current_user_id(), '_wcv_untrusted_vendor', true ) == 'yes' ) ? true: false;

        if ( $trusted_vendor ) $can_submit_live = true; 
        if ( $untrusted_vendor ) $can_submit_live = false; 


        $text = array( 'notice' => '', 'type' => 'success' );

How can I redirect it to example.com/user only after successfully submitting the form?

Upvotes: 0

Views: 310

Answers (1)

Prav
Prav

Reputation: 2894

You would need some kind of hook that give the status of the form and bind that to the redirection to act on the hook. Like making the redirection to be a function and you call that function when the form get submitted properly. There're third party plugin's like Gravity From that can do this for you. Its up to you if you want a paid service like this.

public function process_submit() { 

  if ( ! isset( $_POST[ '_wcv-save_product' ] ) || !wp_verify_nonce( $_POST[ '_wcv-save_product' ], 'wcv-save_product' ) || !is_user_logged_in() ) { 
    return; 
  }

  $can_submit_live        = WC_Vendors::$pv_options->get_option( 'can_submit_live_products' ); 
  $current_post_status    = isset( $_POST[ 'post_status' ] ) ? $_POST[ 'post_status' ] : ''; 
  $can_edit_approved      = WC_Vendors::$pv_options->get_option( 'can_edit_approved_products' ); 
  $trusted_vendor         = ( get_user_meta( get_current_user_id(), '_wcv_trusted_vendor', true ) == 'yes' ) ? true: false;
  $untrusted_vendor       = ( get_user_meta( get_current_user_id(), '_wcv_untrusted_vendor', true ) == 'yes' ) ? true: false;

  if ( $trusted_vendor ) {
    header('Location: http://www.example.com/user');
  }else {
    # code...
  }


  $text = array( 'notice' => '', 'type' => 'success' );
}

public function process_submit() { 

    if ( ! isset( $_POST[ '_wcv-save_product' ] ) || !wp_verify_nonce( $_POST[ '_wcv-save_product' ], 'wcv-save_product' ) || !is_user_logged_in() ) { 
      return; 
    }

    $can_submit_live        = WC_Vendors::$pv_options->get_option( 'can_submit_live_products' ); 
    $current_post_status    = isset( $_POST[ 'post_status' ] ) ? $_POST[ 'post_status' ] : ''; 
    $can_edit_approved      = WC_Vendors::$pv_options->get_option( 'can_edit_approved_products' ); 
    $trusted_vendor         = ( get_user_meta( get_current_user_id(), '_wcv_trusted_vendor', true ) == 'yes' ) ? true: false;
    $untrusted_vendor       = ( get_user_meta( get_current_user_id(), '_wcv_untrusted_vendor', true ) == 'yes' ) ? true: false;

    if ( $trusted_vendor ) $can_submit_live = true; 
    if ( $untrusted_vendor ) $can_submit_live = false; 

    if ($can_submit_live) {
      header('Location: http://www.example.com/user');
    }else {
      # code...
    }
    
    $text = array( 'notice' => '', 'type' => 'success' );
}

Upvotes: 1

Related Questions