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' );
do_action( 'wpd_post_parse' );
and then addadd_action( 'wpd_post_parse', 'wpd_post_parse' );
. You're attempting to register an action at a late stage I believeadd_action
should be outside the scope, just like you did withadd_shortcode