Custom form, shortcode, and submit handler

I have a frontend form with a bunch of input. My requirements force me to use a custom shortcode to create the form. I have already tested a page with that shortcode.

Here’s my :

Read More
<form name="myform" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" enctype="multipart/form-data">

Based on this, it should open up the same page (and it did). However, when I hit submit, I got 404 on that very same URL. Is there any solution?

UPDATE #1

I try different route, using add_action(‘init’):

add_action('init', 'mbro1_intercept_form_input');
function mbro1_intercept_form_input()
{
    if( !(isset($_POST['action_code']) && $_POST['action_code'] == 'mbro_intercept_form_input') )
        return "";
    if( isset( $_POST['submit'] ) )
    {
        //do my code here
        wp_redirect( get_permalink(35) );//page that has [shortcode]
    }
}

This successfully run my intended action on submit. But! upon redirection, it still got 404. I don’t know what is wrong.

Related posts

Leave a Reply

3 comments

  1. I’m assuming that the line near the end

    wp_redirect( get_permalink(35) );//page that has form

    is what is failing. I would change it to this

    $redirect_link =  get_permalink(35) ; //page that has form
    wp_redirect( $redirect_link );  // trigger redirect
    exit;
    

    that should cause it to function to work properly

  2. I take die() as solution. Though, I don’t prefer this.

    $redirect_link =  get_permalink(35) ; //page that has form
    $script_redirect = "<div>Your form is submitted. Please wait a moment. If your browser didn't redirect, click <a href='$redirect_link'>here</a>.</div>
    <script type='text/javasript' language='javascript'> 
        window.location = '$redirect_link';
    </script> ";
    die( $script_redirect );