Search code examples
phpwordpressauthor

Wordpress - Update function if users deleted their account


I'm stuck with this, How can i update this function if one of our users deleted their account? It should update it self and i don't know what can i do to update automatically. Any help please.

/**
 * Retrieve following count
 *
 * Gets the total number of users that the specified user is following
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to retrieve a count for
 * @return      int
 */

function pwuf_get_following_count( $user_id = 0 ) {

    if ( empty( $user_id ) ) {
        $user_id = get_current_user_id();
    }

    $following = pwuf_get_following( $user_id );

    $count = 0;

    if ( $following ) {
        $count = count( $following );
    }

    return (int) apply_filters( 'pwuf_get_following_count', $count, $user_id );
}

here is the function to get the following users

/**
 * Retrieves all users that the specified user follows
 *
 * Gets all users that $user_id followers
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to retrieve following for
 * @return      array
 */

function pwuf_get_following( $user_id = 0 ) {

    if ( empty( $user_id ) ) {
        $user_id = get_current_user_id();
    }

    $following = get_user_meta( $user_id, '_pwuf_following', true );

    if ( empty( $following ) ) {

    return;

    }
    return (array) apply_filters( 'pwuf_get_following', $following, $user_id );
}

And this one for Increase or Decrease the users number.

/**
 * Increase follower count
 *
 * Increments the total count for how many users a specified user is followed by
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to increease the count for
 * @return      int
 */

function pwuf_increase_followed_by_count( $user_id = 0 ) {

    do_action( 'pwuf_pre_increase_followed_count', $user_id );

    $followed_count = pwuf_get_follower_count( $user_id );

    if ( $followed_count !== false ) {

        $new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', $followed_count + 1 );

    } else {

        $new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', 1 );

    }

    do_action( 'pwuf_post_increase_followed_count', $user_id );

    return $new_followed_count;
}


/**
 * Decrease follower count
 *
 * Decrements the total count for how many users a specified user is followed by
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to decrease the count for
 * @return      int
 */

function pwuf_decrease_followed_by_count( $user_id ) {

    do_action( 'pwuf_pre_decrease_followed_count', $user_id );

    $followed_count = pwuf_get_follower_count( $user_id );

    if ( $followed_count ) {

        $count = update_user_meta( $user_id, '_pwuf_followed_by_count', ( $followed_count - 1 ) );

        do_action( 'pwuf_post_increase_followed_count', $user_id );

    }
    return $count;
}

Now i don't know why the count not changed when some users deleted their accounts.


Solution

  • Have a backup of your database before trying this, or try using in a staging site before implementing in live. Add this to your theme's 'functions.php'.

    function follow_delete_user( $user_id ) {
        $userslist = get_users();
    
        foreach ( $userslist as $user ) {
            pwuf_unfollow_user( $user->ID, $user_id );
            pwuf_unfollow_user( $user_id, $user->ID );
        }
    }
    add_action( 'delete_user', 'follow_delete_user' );