BuddyDev

Search

[Resolved] I want to execute the action only the first time

  • Participant
    Level: Initiated
    Posts: 17
    maimaimemain on #20587

    There is an action you want to execute only the first time the user logs in.

    What’s wrong with the code below?

    ` function my_add_meta( $user_id, $key, $user ) {
    add_user_meta( $user_id, ‘first_login’, ‘not_yet’ );
    }
    add_action( ‘bp_core_activated_user’, ‘my_add_meta’, 10, 3 );

    function my_check_meta( $redirect_to, $redirect_to_raw, $user ) {
    $meta = get_user_meta( $user->ID, ‘first_login’, true );
    if ( $meta == ” ) {
    // After the second time actions
    } else {
    // First time actions
    delete_user_meta( $user->ID, ‘first_login’ );
    }
    }
    add_filter( ‘bp_login_redirect’, ‘my_check_meta’, 10, 3 ); `

  • Participant
    Level: Initiated
    Posts: 17
    maimaimemain on #20589

    Or I tried the code below, but it did not work.

    function buddydev_create_post_on_activation( $user_id, $key, $user ) {
    
        // get user name	
        $user_data = get_user_by( 'id', bp_loggedin_user_id() )->data;
        $user_nicename = = $user_data->user_nicename;
    
        // create slug
        $new_slug = 'first-topic-' . $user_nicename;
    
        // create topic
        $post_value = array(
            'post_name' => $new_slug,
            'post_type' => 'topic',
            'post_author' => $userid,
            'post_title' => 'first topic',
            'post_content' => 'This topic was created when you registered.',
            'post_category' => array(1,5), 
            'tags_input' => array('tag1','tag2'), 
            'post_status' => 'publish' 
        );
        wp_insert_post($post_value);
    
    }
    add_action( 'bp_core_activated_user', 'buddydev_create_post_on_activation', 10, 3 );
  • Participant
    Level: Initiated
    Posts: 17
    maimaimemain on #20590

    Although it will work, but since it is executed with the number of times to access the login screen before logging in, articles are posted many times.

    function first_login_check( $redirect_to, $not_used, $user ) {
        // After the second time actions    
        if( bp_get_user_last_activity( $user->ID ) !== '' ) {
            // nothing much
        }
        // First time actions
        else{
            // get user name	
            $user_data = get_user_by( 'id', bp_loggedin_user_id() )->data;
            $user_nicename = = $user_data->user_nicename;
    
            // create slug
            $new_slug = 'first-topic-' . $user_nicename;
    
            // create topic
            $post_value = array(
                'post_name' => $new_slug,
                'post_type' => 'topic',
                'post_author' => $userid,
                'post_title' => 'first topic',
                'post_content' => 'This topic was created when you registered.',
                'post_category' => array(1,5), 
                'tags_input' => array('tag1','tag2'), 
                'post_status' => 'publish' 
            );
            wp_insert_post($post_value);
    	
        }
        exit();
    }
    add_filter( 'login_redirect', 'first_login_check', 20, 3 );
    • This reply was modified 5 years, 10 months ago by maimaimemain.
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #20594

    Hello,

    Please try the following code and let me know if it works or not.

    
    /**
     * Create post on activation
     *
     * @param int $user_id User id.
     */
    function buddydev_create_post_on_activation( $user_id ) {
    
    	$user = get_user_by( 'ID', $user_id );
    	$user_nicename = $user->user_nicename;
    
    	// create slug
    	$new_slug = 'first-topic-' . $user_nicename;
    
    	// create topic
    	$post_value = array(
    		'post_name'     => $new_slug,
    		'post_type'     => 'topic',
    		'post_author'   => $user_id,
    		'post_title'    => 'first topic',
    		'post_content'  => 'This topic was created when you registered.',
    		'post_category' => array( 1, 5 ),
    		'tags_input'    => array( 'tag1', 'tag2' ),
    		'post_status'   => 'publish',
    	);
    	wp_insert_post( $post_value );
    
    }
    add_action( 'bp_core_activated_user', 'buddydev_create_post_on_activation', 10 );
    

    Thank You
    Ravi

  • Participant
    Level: Initiated
    Posts: 17
    maimaimemain on #20595

    Thank you for the quick response.

    The code you presented did not work.

    I wrote it in “wordpress\wp-content\plugins\bp-custom.php”.

    I confirmed the topic list at the timing when the user registered and logged in the first time.

    There seems to be no error in particular.

    Regards.

    • This reply was modified 5 years, 10 months ago by maimaimemain.
  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #20597

    Hi,
    Do you want some code to be run when a user’s account is activated or when a user logs in to the site for the first time? Both are different cases.

    Please let me know and I will assist.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 17
    maimaimemain on #20606

    Hi, Brajesh.

    How are you?

    Nice to hear from you.

    I hope your family are doing well.

    It’s getting much colder here in Tokyo.

    I want to when a user logs in to the site for the first time.

    Regards
    maimaimemain

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #20607

    Hi maimaimemain,
    Thank you. I am doing well. Hope you are doing well too.

    Sincerely appreciate the kind words and concern.

    Wow, I never knew Tokyo is so much colder in day(Googled it):)

    About the code:- It needs slight update. Here is a another version(with a different hook)

    
    function buddydev_custom_first_login_action( $redirect_to, $redirect_url_specified, $user ) {
    
    	//check if we have a valid user and bp exists?
    	if ( is_wp_error( $user ) || ! function_exists( 'bp_get_user_last_activity' ) ) {
    		return $redirect_to;
    	}
    
    	//check for user's last activity
    	$last_activity = bp_get_user_last_activity( $user->ID );
    
    	if ( empty( $last_activity ) ) {
    		// since BuddyPress did not record activity earlier, This is our first login.
    		// you can put any action here.
    	}
    
    	return $redirect_to; // please do not forget to return it.
    }
    
    add_filter( 'login_redirect', 'buddydev_custom_first_login_action', 110, 3 );
    
    

    You can put any code inside that if block to execute, and it should.

    Please give it a try. If it does not work, Please post me the modified code and I will assist quickly.

    PS:- The strategy from your first post will work too, we just need to slightly modify that. I liked the simplicity of that strategy.

    Best regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 17
    maimaimemain on #20609

    Hi, Brajesh.

    I forgot “return” .
    Finally, I managed to do it!

    Thank you for giving me good advice.
    You are a lifesaver.

    And Ravi, I am grateful for your support.

    I’ll keep on trying.

    Regards
    maimaimemain

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #20612

    Hi maimaimemain,
    Thank you.

    Glad it worked.

    All the best with your project.

    Regards
    Brajesh

The topic ‘ [Resolved] I want to execute the action only the first time’ is closed to new replies.

This topic is: resolved