titusb
Forum Replies Created
-
Sure, I understand!
Thank you for your suggestions. I appreciate them and will see what I can do.
Titus
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!
TitusOK, thank you. I’ll check the behavior and will come back to you
This is also what I expect. But is there the possibility to include the user role-part into your suggestion?
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?
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
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, 5 months ago by titusb.
Forum: Plugins
In reply to: [WPAdverts - Classifieds Plugin] Space between amount and currency symbolYes, that worked. Thank you!
Hey Jarno,
thank you for the update! Now I indeed found a new problem…
Create a new advert
Click preview
under “location” there should be the map now with the marker set to the right place – but it doesn’t appear
The advert still can be publishedIf the WP Adverts-integration is deactivated the map shows up as expected in the preview.
The behavior is reproducible with your example site.
Titus
Hi Jarno,
as far as I could see everything works fine now! In the case that I still find something, I’ll let you know.
Titus
Hi Jarno,
I deactivated the integration and the MU Plugin. Since the map doesn’t show the “click here to accept cookies-button” now (this is where we started) I added the accept-shortcode below the map. That’s fine for now.
If you want to you can send me your location/IP so I can ask our host to unblock your region.
Titus
Hello Jarno,
I experienced a few times now that the map on https://www.campus-connect.de/wohnungsmarkt/ isn’t loading properly (in Opera) even if the cookies have been accepted before: The map isn’t loaded at all or no markers are set (and so the focus is on the Pacific).
And in firefox no markers are set at all.
This only happens with the new code.
Can you reproduce this behavior?
Titus
The code is getting longer ??
As far as I can see both things are working now: the map and also google location autocomplete.
I also tried to accept the cookies, revoke, accept again – everything works fine!
Thank you very much for your update!
Titus
Hello Jarno,
now the page is reloaded the moment cookies are accepted and the page loads as excepted – but pre-entered information in the form gets lost.
So probably I won’t use the reload function and for now simply put a note under the map that users have to reload the page if the map isn’t shown the right way.
And let’s see which idea the WPAdverts developer has.
Thank you!
TitusIt seems that the code fixed the problem. Thank you!
But now the maps-integration on https://www.campus-connect.de/wohnungsmarkt/ stopped working properly:
Clicking on “click here to activate the content” either loads the map somewhere in the Pacific or doesn’t load it at all. Only after reloading the page the right map section is shown.Titus