I am struggling to create a custom post in Wordpress using register_new_user hook. The code seems to be fine but nothing happens. I have even run a debug log but there is no data regarding the code. What could I be missing?

I ahev also referenced the other post: Creating a custom post type upon registration But still nothing works even if I select user_register hook or register_new_user hook.

The code:

add_action( 'register_new_user', 'create_portfolio_post', 10, 1 );

function create_portfolio_post( $user_id )
    error_log('create_portfolio_post called with user ID: ' . $user_id);
    // Get user info
    $user_info = array();
    $user_info = get_userdata( $user_id );
    // $user_roles = $user_info->roles;

    // New code added
    // $this_user_role = implode(', ', $user_roles );
    //if ($this_user_role == 'subscriber')

    // Create a new post
    $user_post = array(
        'post_title' => $user_info->nickname,
        'post_author' => $user_id,
        'post_content' => $user_info->nickname,
        'post_status' => 'publish', // <- here is to publish
        'post_type' => 'portfolio' // <- change to your cpt
    // Insert the post into the database
    wp_insert_post( $user_post );
    error_log('wp_insert_post called with arguments: ');
    error_log(print_r($user_post, true));
  • What do you get if you log the variable $post_id like it is used in the linked post answer? i.e. $post_id = wp_insert_post( $user_post ); Jan 6 '21 at 5:35
  • how are your users being registered? Where is the code in your question located?
    – Tom J Nowell
    Jan 6 '21 at 9:38
  • If $user_id is the parameter used by your callback function, you need to use user_register as the hook. Where are you adding this code? Jan 6 '21 at 9:38

Your Answer

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

Browse other questions tagged or ask your own question.