• Resolved erik-pixelkicks

    (@erik-pixelkicks)


    Hi guys,
    Is there a way how to create a post on user first login?
    Currently i have a code which automatically creates a post when user registers on the site, but i want to change it so the post is created only if user is approved and logged in first time. This is the code which i used so far:

    function my_create_page($user_id){
    $the_user = get_userdata($user_id);
    $new_user_name = $the_user->user_login;
    $new_user_id = $the_user->ID;
    $shop_name = $the_user->tds_shop_name;

    $my_post = array();
    $my_post[‘post_title’] = $shop_name;
    $my_post[‘post_author’] = $new_user_id;
    $my_post[‘post_type’] = ‘business’;
    $my_post[‘post_content’] = ”;
    $my_post[‘post_status’] = ‘private’;
    wp_insert_post( $my_post );
    }
    add_action(‘user_register’, ‘my_create_page’);

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t think there is a single handy action for first time logins. You can use the ‘authenticate’ filter which fires every time anyone logs in. To differentiate a first time user you could store something in user meta when the post is created (or just query for that post) and if it exists don’t do anything special.

    WordPress does not store login attempts or times in the database. So here’s an idea, instead of trying to do it on their first login, why not just create the page if it doesn’t exist ON user login.

    The idea is to check if the user already has a shop page, if not, then we check if the page already exists. If either of the two checks fail, we bail via return;

    Otherwise, we can create a page, so we do, following the same arguments you provided. However, with an added addition. If the page creation is successful, we update the user’s meta data giving WordPress the newly created page ID so we can query it later if need be ( as seen in the top of the function ).

    Here’s the gist https://gist.github.com/a902d60525e22b6f5add

    If you want users to have the ability to create duplicate shops, you’ll have to do some magic with the page_exists checks, but other than that, this should do what you’re looking for.

    Thread Starter erik-pixelkicks

    (@erik-pixelkicks)

    @jerry Thanks mate, that’s exactly what i was looking for. Great Job!!!

    Thread Starter erik-pixelkicks

    (@erik-pixelkicks)

    Problem Solved! 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Automatically create a post on user first login’ is closed to new replies.