0

在 wordpress 中,用户注册后,我使用下面的函数来创建两个不同自定义帖子类型的页面,然后我需要在他们的用户数据中存储一个自定义元值以帮助以后进行重定向。我发现如果我在注册期间(在注册表单上)指定自定义元值,我可以稍后通过以下方式检索这些值:

global $current_user;
    get_currentuserinfo();
    $theirRedirectKey = $current_user->rpr_redirect_key;

但是,在以下 functions.php 片段中,我无法保存元值以供以后检索。

function after_registration($user_id){
    // Get the Newly Created User ID
    $the_user = get_userdata($user_id);

    // Get the Newly Created User Name
    $new_user_name = $the_user->user_login;

    // Create a unique Tour Code Prefix from User ID
    $tourPrefix = $the_user->ID;

    // Check for Tour Code Key if entered into registration form
    $enteredKey = $the_user->rpr_redirect_key;

    if($enteredKey == ''){
        //Create the first Tour Builder Page
        $tourBuilder = array();
        $tourBuilder['post_title'] = $new_user_name . '| Custom Educational Tour';
        // Next line may not be important after hubpages are set up.
        $tourBuilder['post_name'] = 'builder-' . $tourPrefix;
        $tourBuilder['post_type'] = 'builder';
        $tourBuilder['post_content'] = 'This is the content!';
        $tourBuilder['post_author'] = $user_id;
        $tourBuilder['post_status'] = 'publish';
        $tour_id = wp_insert_post( $tourBuilder );

        // Build hubpage
        $hubpage = array();
        $hubpage['post_title'] = $new_user_name . '\'s Hubpage';
        // URL must be unique
        $hubpage['post_name'] = $new_user_name;
        $hubpage['post_type'] = 'hubpages';
        $hubpage['post_author'] = $user_id;
        $hubpage['post_status'] = 'publish';
        $hub_id = wp_insert_post( $hubpage );

        //Update User with proper redirect keys for some reason this line doesn't work.
        add_user_meta($the_user, 'rpr_redirect_key', '/hubpage/' . $new_user_name, true);
    }

}
add_action('user_register', 'after_registration');

帮助将不胜感激。

4

1 回答 1

0

在行

add_user_meta( $the_user, 'rpr_redirect_key', '/hubpage/' . $new_user_name, true);

$the_user不是身份证。尝试$the_user->ID$user_id代替

于 2013-05-06T22:00:13.630 回答