执行另一个表单函数后登录WordPress

时间:2016-10-24 作者:Ben

这是一种独特的情况。基本上,我正在构建的站点在主页上有一个登录表单,该表单也有用于注册邮件列表的字段,但前提是选中了复选框。我想对其进行设置,以便在运行邮件列表的订阅代码后,我将表单发布到的操作url将提交表单上的相关字段,以便用户登录Wordpress。

这就是我迄今为止的行动。我将表单提交给的php文件:

<?
$url = \'/wp-login.php\';
$data = array(\'log\' => $_POST[\'log\'], \'pwd\' => $_POST[\'pwd\'],\'redirect_to\'=> $_POST[\'redirect_to\']);
$options = array(
    \'http\' => array(
    \'header\'  => "Content-type: application/x-www-form-urlencoded\\r\\n",
    \'method\'  => \'POST\',
    \'content\' => http_build_query($data),
    )
);

$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);


if ($_POST[\'subscribe\'] == \'yes\') {

$api_key = "KEYKEY";
$list_id = LISTLIST";
require(\'Mailchimp.php\');

$email = $_POST[\'stripeEmail\'];
$merge_vars = array(\'FNAME\'=>htmlentities($_POST[\'fname\']), \'LNAME\'=>htmlentities($_POST[\'lname\']) );

$Mailchimp = new Mailchimp($api_key);
$Mailchimp_Lists = new Mailchimp_Lists($Mailchimp);

try{
    $subscriber = $Mailchimp_Lists->subscribe( $list_id, array( \'email\' => htmlentities($_POST[\'stripeEmail\']) ), $merge_vars );
} catch (\\Exception $e) {
    if ($e instanceof \\Mailchimp_List_AlreadySubscribed) {
    // In case they are already subscribed:
    $errors[] = \'\';
} else {
    // In case something else went wrong.
    $errors[] = \'\';
}
};
};?>

1 个回复
最合适的回答,由SO网友:jgraup 整理而成

如果你上钩wp_authenticate 在普通表单提交中,您可以在继续进行身份验证之前快速绕过subscribe函数。

add_action( \'wp_authenticate\', \'wp_authenticate_by_email\' );

function wp_authenticate_by_email( $username ) {

    // check for subscribe action
    $is_subscribe = \'yes\' == $_POST[ \'subscribe\' ];

    // invalid username 
    if ( ! username_exists( $username ) ) {

        // lookup as email
        $user = get_user_by( \'email\', $username );
        if ( $user ) {

            $email    = $username;
            $username = $user->user_login;

            if ( $is_subscribe ) {

                // subscribe to mailing list
                do_subscribe( $email );
            }
        }

        return;
    }

    // lookup user by login
    $user = get_user_by( \'login\', $username );
    if ( $user ) {

        // get email from user login
        $email = $user->user_email;
        if ( $is_subscribe ) {

            // subscribe to mailing list
            do_subscribe( $email );
        }
    }
}

function do_subscribe( $email ) {

    if ( empty( $email ) ) {
        return;
    }

    // maybe check for password ????

    // do subscribe actions

    sleep( 2 );
}