1

I am trying to create a new post (later will convert it into custom post type), whenever a new user register on the website and if the welcome email was sent succesfully. The problem is that I want to add the new user as the author of the new post. Later all notification emails will be accesible from a special page.

I'm trying to use the wp_mail filter:

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;
}

I don't know how to get the user Id.

Any idea?

2

Instead of using wp_mail filter use action 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 );

Based on an assumption that you want this user to publish other posts later, I've changed his role to author. Of course you can set a new post to fit your needs.

  • Yes, you are right. In the meantime I've figured out, just forgot to mention here. But your answer is much appreciated and I like a lot the aproach, a little bit different from mine. Thanks a lot! :) – Grávuj Miklós Henrich Jun 21 '15 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.