在用户注册时,如果发送了欢迎邮件,则添加新用户为作者的帖子

时间:2015-06-18 作者:Grávuj Miklós Henrich

我正在尝试创建一个新帖子(稍后将其转换为自定义帖子类型),无论何时新用户在网站上注册,以及欢迎电子邮件是否成功发送。问题是我想添加新用户作为新帖子的作者。稍后,所有通知电子邮件将可从特殊页面访问。

我正在尝试使用wp_mail 过滤器:

add_filter( \'wp_mail\', \'my_wp_mail_filter\' );
function my_wp_mail_filter( $args ) {

    $new_wp_mail = array(
        \'to\'          => $args[\'to\'],
        \'subject\'     => $args[\'subject\'],
        \'message\'     => $args[\'message\'],
        \'headers\'     => $args[\'headers\'],
        \'attachments\' => $args[\'attachments\'],
    );

    $new_post = array(
        \'post_title\'    => $args[\'subject\'] . \' - \' . date(\'Y-m-d H:i:s\'),
        \'post_content\'  => $new_wp_mail,
        \'post_status\'   => \'publish\',
        \'post_date\'     => date(\'Y-m-d H:i:s\'),
        \'post_author\'   => $user_id,
        \'post_type\'     => \'post\',
        \'post_category\' => array(0)
    );

    $post_id = wp_insert_post( $new_post );

    return $new_wp_mail;
}
我不知道如何获取用户Id。

有什么想法吗?

1 个回复
最合适的回答,由SO网友:Frank P. Walentynowicz 整理而成

而不是使用wp_mail 筛选器使用操作user_register:

function fpw_new_user_post( $user_id ) {
    global $wpdb;
    update_user_meta( $user_id, $wpdb->base_prefix . \'capabilities\', array( \'author\' => TRUE ) );
    $new_post = array (
        \'post_title\'    => \'User \' . get_user_by( \'id\', $user_id )->user_login . \'  registered @ \' .  date( \'Y-m-d H:i:s\', current_time( \'timestamp\' ) ),
        \'post_content\'  => \'This is my first post.\',
        \'post_status\'   => \'publish\',
        \'post_author\'   => $user_id,
        \'post_category\' => array( 0 )
    );
    $post_id = wp_insert_post( $new_post );
    if ( 0 == $post_id ) {
        // do some error action
    }
}
add_action( \'user_register\', \'fpw_new_user_post\', 10, 1 );
基于您希望该用户稍后发布其他帖子的假设,我将其角色更改为author. 当然,你可以根据自己的需要设置一个新职位。

结束

相关推荐