1

I have written a plugin whose job is to create a custom page with a user subscription form. The form page is created like this:

$page = [                              
    'post_type' => 'page',             
    'post_content' => '',              
    'post_status' => 'publish',        
    'post_title' => 'Create a new account', 
    'post_name' => 'account new'        
];                                     
                                       
$page_id = wp_insert_post($page);     

This creates a new page at http://example.com/account-new/ that renders a subscription sign up form. I used the same process to create a page that receives the POST data at http://example.com/account-pending/.

In my theme, which is independent of the plugin, the form's opening tag looks like this:

<form method="post" id="new-account-form" action="/account-pending/">

On submit, I can see in Chrome that the post data are sent to the account-pending page, but when I debug the PHP code with PHPStorm I find that the $_POST object is not set.

The /account-pending/ page contains the following function that is meant to process the POST data:

public function get_account_pending()
{

    if (isset( $_POST['my_check_value'])) {
        $username =       $_POST['username'];
        $password =       $_POST['userpass'];
        $firstname =      $_POST['firstname'];
etc...

I have also added an action for capturing the post action with

add_action( 'wp', array( &$this, 'get_account_pending' ) );

but I am still missing something. How do I find what is stripping away the $_POST object? At this point in the code only the following variables are set:

$this
$_GET
$_REQUEST
$_SERVER
$GLOBALS

Am I missing a step somewhere that is required to properly set up the POST data for receiving it on the /account-pending/ page?

Edit:

I am able to see the $_POST data if I send the data via a cURL command as below:

curl -X POST 'firstname=myfirst' -F 'lastname=mylast' -F 'username=username21' -F 'usermail=me%40example.com' -F 'userpass=thisone' -F 'userpass_again=thisone' -F 'my_check_value=on' -F 'billing_address=452+Kennebec+Rd' -F 'billing_apt=' -F 'billing_city=Heretown' -F 'billing_state=ME' -F 'billing_zip=04444' -F 'card_type=4' -F 'card_number=4111111111111111' -F 'expire_month=07' -F 'expire_year=23' http://example.com/account-pending/?XDEBUG_SESSION_START=10663

This seems to indicate that the problem lies with either wordpress (or PHP) clearing the $_POST variable, or that my form is not submitting the data properly.

3
  • 1
    You shouldn't put & infront of $this, it's there to prevent PHP4 breaking, but WordPress hasn't supported PHP4 in years. btw have you eliminated WordPress as a cause? You can test that by submitting to a standalone PHP file that just prints out the POST value, or var_dump( $_POST );. Also keep in mind that $_POST is not a normal variable, it's a superglobal
    – Tom J Nowell
    Commented Jul 8, 2016 at 22:57
  • Thank you @TomJNowell. I removed the & and updated the question - curl does generate a filled-in $_POST variable. I'm still searching for causes for the lost $_POST.
    – katahdin
    Commented Jul 8, 2016 at 23:27
  • 1
    Make sure to do actions related to form data before content is served, maybe hook into wp or init something like add_action("wp", function() { if ( "account-pending" === get_post_field( "post_name", get_the_ID() ) ) { echo var_dump( $_POST ); } }); 'cause I doubt it would be successful doing to upon content served (you mentioned "The /account-pending/ page contains the following function that is meant to process the POST data..")..
    – Ismail
    Commented Jul 9, 2016 at 1:23

1 Answer 1

1

I don't know why your $_POST data disappears. However, consider using the admin_post/admin_post_nopriv hook to process the form data using a function on the back end. That's what the function was made for. At the end of the function, you can then redirect to the account-pending page using this wp_redirect(home_url('/account-pending')) .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.