0

I'm developing a Wordpress plugin and I'm having trouble getting $_POST data using an action inside my shortcode. The form I'm parsing is on a page that I've added my shortcode to.

Here's what I've got:

function wpd_post_parse() {

$string = 'wp hook: ' . var_dump($_POST) . "\r\n";

//$string = 'wp hook: first name: ' . $_POST["first_name"] . "\r\n";
//$string .= 'wp hook: last name: ' . $_POST["last_name"] . "\r\n";
//$string .= 'wp hook: email: ' . $_POST["email"] . "\r\n";

$fileName = dirname(__FILE__) . '/log.txt';     

file_put_contents($fileName, $string, FILE_APPEND | LOCK_EX);
//file_put_contents($fileName, $string, FILE_APPEND);

/*
if (!empty($_POST)){

    $string = 'wp hook: ' . var_dump($_POST) . "\r\n";

    $fileName = dirname(__FILE__) . '/log.txt';     

    file_put_contents($fileName, $string, FILE_APPEND | LOCK_EX);
    //file_put_contents($fileName, $string, FILE_APPEND);

}else{

    $string = 'wp hook: POST empty' . "\r\n";

    $fileName = dirname(__FILE__) . '/log.txt';     

    file_put_contents($fileName, $string, FILE_APPEND | LOCK_EX);
}
*/

}

function mgc_init(){

   $htmlStr = 'Hello World!' . "\r\n";

   $fileName = dirname(__FILE__) . '/log.txt';

   file_put_contents($fileName, $htmlStr, FILE_APPEND | LOCK_EX);
  //file_put_contents($fileName, $htmlStr, FILE_APPEND);  

  //Add hook to capture $_POST data
  add_action( 'parse_request', 'wpd_post_parse' );

}

add_shortcode('mgc','mgc_init'); //put this shortcode [mgc] on page in Wordpress 

UPDATE: Thanks for your help so far. I'm trying to get a $_POST value (first name) on form submit but the value isn't inside $_POST["first_name"]. All that gets printed to the file is "wp hook: first name: " Here's how the code looks now:

//Capture the $_POST data
function wpd_post_parse() {

  //$string = 'wp hook: ' . var_dump($_POST) . "\r\n";

  $string = 'wp hook: first name: ' . $_POST["first_name"] . "\r\n";
  //$string .= 'wp hook: last name: ' . $_POST["last_name"] . "\r\n";
  //$string .= 'wp hook: email: ' . $_POST["email"] . "\r\n";

  $fileName = dirname(__FILE__) . '/log.txt';       

  file_put_contents($fileName, $string, FILE_APPEND | LOCK_EX);
  //file_put_contents($fileName, $string, FILE_APPEND);

  /*
  if (!empty($_POST)){

    $string = 'wp hook: ' . var_dump($_POST) . "\r\n";

    $fileName = dirname(__FILE__) . '/log.txt';     

    file_put_contents($fileName, $string, FILE_APPEND | LOCK_EX);
    //file_put_contents($fileName, $string, FILE_APPEND);

  }else{

    $string = 'wp hook: POST empty' . "\r\n";

    $fileName = dirname(__FILE__) . '/log.txt';     

    file_put_contents($fileName, $string, FILE_APPEND | LOCK_EX);
}
*/

//exit;
}

function mgc_init(){


  $htmlStr = 'Hello World!' . "\r\n";

  $fileName = dirname(__FILE__) . '/log.txt';

  file_put_contents($fileName, $htmlStr, FILE_APPEND | LOCK_EX);
  //file_put_contents($fileName, $htmlStr, FILE_APPEND);    

  do_action( 'wpd_post_parse' );

}

add_shortcode('mgc','mgc_init'); //put this shortcode [mgc] page in Wordpress 

add_action( 'parse_request', 'wpd_post_parse' );
5
  • Where in the code are you having the trouble? Does nothing get written to log? Or what do you see in the log.txt file? Commented Mar 10, 2017 at 17:50
  • Use an action listener inside the shortcode instead, as do_action( 'wpd_post_parse' ); and then add add_action( 'wpd_post_parse', 'wpd_post_parse' );. You're attempting to register an action at a late stage I believe
    – Ismail
    Commented Mar 10, 2017 at 17:57
  • @SamuelElh Do I put the add_action inside the shortcode as well as the do_action?
    – zzMzz
    Commented Mar 10, 2017 at 18:10
  • @czerspalace The newest code outputs "wp hook: first name:" and nothing else. There should be more in the log file.
    – zzMzz
    Commented Mar 10, 2017 at 18:32
  • add_action should be outside the scope, just like you did with add_shortcode
    – Ismail
    Commented Mar 10, 2017 at 18:38

1 Answer 1

1

It's not entirely clear what your ultimate goal is, but a simple way to catch a form submission is to hook init and check if something is set:

function wpd_check_post_vars(){
    if( isset( $_POST['first_name'] ) ){
        // do something
    }
}
add_action( 'init', 'wpd_check_post_vars' );

There's no connection here between the action and your Shortcode, it's not really necessary. An important point to remember is that actions don't persist beyond the request they're added on. If you add_action after its corresponding do_action is triggered, nothing will happen.

You should also probably use something more unique than first_name.

2
  • Thanks for the reply @Milo. I'm trying to get values from a form when the user submits. According to Wordpress docs "init is useful for intercepting $_GET or $_POST triggers." and that is exactly what I want. Although I only want the add_action('init') to work on the page that the shortcode is on. That's why I thought I'd use the add_action inside the shortcode and not outside the shortcode.
    – zzMzz
    Commented Mar 10, 2017 at 18:52
  • Any action that's added in a Shortcode has to execute after Shortcodes are rendered, which is pretty late in the process. It all depends on what you want to do with that data, how you plan to handle errors, redirection, etc.. It is suggested to use init because the main query hasn't run yet and output hasn't started. Output starts before Shortcodes are executed, so it makes those things difficult/impossible if you wait until that point to run your code.
    – Milo
    Commented Mar 10, 2017 at 20:22

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.