动作挂钩内部Wordpress插件短代码

时间:2017-03-10 16:38:26

标签: php wordpress http-post

我正在开发一个Wordpress插件,但我在使用短代码中的操作时无法获取$ _POST数据。我正在解析的表单位于我已将短代码添加到的页面上。

以下是我所拥有的:

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 

更新:到目前为止,感谢您的帮助。我试图在表单提交上获得$ _POST值(名字),但值不在$ _POST [" first_name"]内。打印到文件的所有内容都是" wp hook:first name:"以下是代码的外观:

//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] on thedinnerdaily.com/subscribe/ page in Wordpress 

add_action( 'parse_request', 'wpd_post_parse' );

1 个答案:

答案 0 :(得分:0)

这种方法不起作用,它在Wordpress上引起了网站范围的问题。我将代码从注册页面移到已完成的表单的重定向页面上。从那里我能够捕获我正在寻找的表单数据。