Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,

    i understand you are using the auto-delete-expired.php snippet? If so then somewhere at the end of it you would need to add the below function

    function auto_delete_user_without_ads( $user_id ) {
        $user = get_user_by( "ID", $user_id );
        if( ! $user ) {
            return;
        }
        $query = new WP_Query([
            "post_type" => "any",
            "posts_per_page" => 1,
            "author" => $user_id,
            "fields" => "ids"
        ]);
        if( $query->post_count === 0 ) {
            require_once( ABSPATH.'wp-admin/includes/user.php' );
            wp_delete_user( $user_id );
        }
    }

    Then go to a line about 99 and after

    if( $force_delete ) {
        wp_delete_post( $ad->ID, $force_delete );
    } else {
        wp_trash_post( $ad->ID );
    }

    add

    auto_delete_user_without_ads($ad->post_author);

    Backup your database before doing this and check if the users are being deleted correctly.

    Thread Starter titusb

    (@titusb)

    Exactly, thank you!

    One question: Does the snippet only delete those users without ads that have been created by wpadverts during creation of an advert? Or could it also happen that a “normal” user like me gets deleted after my last advert gets deleted after expiration?

    If this would be the case one option to distuinguish between “normal” users and adverts-posters would be to check the user role: “Normal” users have a role, adverts-posters don’t have one.

    Titus

    • This reply was modified 1 year, 2 months ago by titusb.
    Plugin Author Greg Winiarski

    (@gwin)

    Hi,

    this function can delete any user, when the user last Ad will expire and get deleted the user account will be removed along with it.

    The users created by WPAdverts on Ad publication have a Subscriber role.

    Thread Starter titusb

    (@titusb)

    OK, perhaps that’s only true for my site. I changed most of my rules and deleted “subscribers”… So Ad posters indeed have no assinged role.

    But anyway: Would it be possible to change your code in a way that only users with a specific role (or in my case “non-roles”) could be deleted automatically? So that no admins, authors, editors, … would be deleted in the case they post an ad that expires one day.

    Titus

    Plugin Author Greg Winiarski

    (@gwin)

    After the

        if( ! $user ) {
            return;
        }

    You can try adding something like

    if( $user->has_cap( "read" ) ) {
        return;
    }

    Now only users without “read” capability should be deleted, i suppose this would be only users without a role set, you would need to test it.

    Thread Starter titusb

    (@titusb)

    How has the code to look like if I check the user role instead the capability?

    This is what chatgpt gave me to my query “Write me code that automatically deletes a wordpress user without a role after his last advert (plugin: wpadverts) got deleted automatically”

    function delete_user_without_role_after_last_ad_deleted($ad_id) {
        // Get the advert being deleted
        $advert = get_post($ad_id);
    
        // Check if it's a valid advert created by WPAdverts
        if ($advert && $advert->post_type === 'advert') {
            $author_id = $advert->post_author;
    
            // Get the user's roles
            $user = get_userdata($author_id);
            $user_roles = $user->roles;
    
            // Check if the user has no roles
            if (empty($user_roles)) {
                wp_delete_user($author_id);
            }
        }
    }
    
    add_action('wpadverts_ad_deleted', 'delete_user_without_role_after_last_ad_deleted');
    

    Does it make sense to combine your code and parts of this one?

    Plugin Author Greg Winiarski

    (@gwin)

    This code will not do anything as there is no wpadverts_ad_deleted action, so the code will never run.

    Thread Starter titusb

    (@titusb)

    This is also what I expect. But is there the possibility to include the user role-part into your suggestion?

    Plugin Author Greg Winiarski

    (@gwin)

    Hi,

    to check if a user has some specific role instead of “read” capability, you can replace the

    $user->has_cap( "read" )

    with

    in_array( 'author', (array) $user->roles )

    where ‘author’ is the role you want to check for.

    Thread Starter titusb

    (@titusb)

    OK, thank you. I’ll check the behavior and will come back to you

    Thread Starter titusb

    (@titusb)

    So, I’m back.

    I had to change your code suggestion a little bit since your lines

    if( in_array( 'author', (array) $user->roles ) ) {
            return;
        }

    stop the script if the persons role is “author” (or whatever I choose). Every other user would have been deleted.

    But I want the script to check the other way around: the script should stop if the users role isn’t “subscriber” – and this way only delete subscribers:

    
    function auto_delete_user_without_ads( $user_id ) {
        $user = get_user_by( "ID", $user_id );
        if( ! $user ) {
            return;
        }
        if( !in_array( 'subscriber', (array) $user->roles ) ) { // if role is not "subscriber" --> stop
            return;
        }
        $query = new WP_Query([
            "post_type" => "any",
            "posts_per_page" => 1,
            "author" => $user_id,
            "fields" => "ids"
        ]);
        if( $query->post_count === 0 ) {
            require_once( ABSPATH.'wp-admin/includes/user.php' );
            wp_delete_user( $user_id );
        }
    }

    During my testing I saw a new point that I didn’t see before:

    There are still hundreds of user accounts without adverts (they have been expired and then deleted automatically or they have been deleted manually) in my database that I also would like to have deleted automatically.

    How is it possible to execute the code above not after a post got deleted automatically but reguarly, on an every day base? This would clean up the database since every subscriber-user-account without ads (no active and no expired ones) would be deleted.

    Thank you!
    Titus

    Plugin Author Greg Winiarski

    (@gwin)

    Hi,

    it would be best to have an event running once daily that would delete users without any posts, but writing a code that does this is beyond the support offered here i am afraid.

    One other way to do that is to use some plugin that will bulk delete the users

    Thread Starter titusb

    (@titusb)

    Sure, I understand!

    Thank you for your suggestions. I appreciate them and will see what I can do.

    Titus

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Automatically delete user after last advert got deleted automatically’ is closed to new replies.