繁体   English   中英

Wordpress:创建新用户后创建新帖子的功能

[英]Wordpress : function create new post after creating a new user

创建新用户(仅编辑者)后,我需要创建一个新帖子(具有自定义帖子类型)。 我想我必须使用这样的钩子:

do_action('user_register', $user_id);
add_action ('user_register', "create_post");
function create_post()
{
// Create the post
}

但是我不知道如何在此功能中创建帖子。 我在新的用户表单中有一些自定义字段...

谢谢纪尧姆

尝试在add_action中使用参数,这是简单的示例

add_action( 'user_register', 'create_post', 10, 1 );

function create_post( $user_id ) {

    if ( isset( $_POST['first_name'] ) )
        ..... your code  here

}

对于惰性帖子,请在给定链接http://codex.wordpress.org/Function_Reference/wp_insert_post中使用此功能

从WordPress的Codex用户注册

你可以做这样的代码

add_action( 'user_register', 'myplugin_registration_save', 10, 1 );

function myplugin_registration_save( $user_id ) {
    //Here you can update user information after they registered
    if ( isset( $_POST['first_name'] ) ) {
        update_user_meta($user_id, 'first_name', $_POST['first_name']);
    }

}

要在用户注册后插入帖子,请尝试扩展代码:

add_action( 'user_register', 'myplugin_registration_save', 10, 1 );

function myplugin_registration_save( $user_id ) {
    //Here you can update user information after they registered
    if ( isset( $_POST['first_name'] ) ) {
        update_user_meta($user_id, 'first_name', $_POST['first_name']);
    }

    // Here you can insert new post for registered users
    $my_post = array(
        'post_title' => 'Title',
        'post_content' => 'some content',
        'post_status' => 'publish',
        'post_author' => $user_id, //If to assign post to the currently registered user otherwise put 1
        'post_type' => 'post'
    );

    // Insert the post into the database
    $_post_id = wp_insert_post($my_post);
    if ($_post_id) {
        return 'post inserted';
    }
}

update_user_meta使您可以在用户注册后更新用户的其他信息
wp_insert_post允许您以编程方式插入帖子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM