Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My uninstall.php file won't delete my custom posts and the meta data related to them on uninstall. The plugin files get deleted, but nothing from the database.

If anyone is able to have a look at this and see if I've got something wrong or I'm missing something, I'd greatly appreciate it.

Thanks!

// If uninstall is not called from WordPress, or the current user doesn't have the capability to delete plugins
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) || !current_user_can_for_blog( $GLOBALS['blog_id'], 'delete_plugins' ) ) {
    exit();
}

// Remove posts of the 'mrlpt_client' type from DB and the post meta

function mrlpt_delete_plugin() {

    $mrlpt_client_posts = get_posts( array(
        'numberposts' => -1,
        'post_type' => 'mrlpt_client',
        'post_status' => 'any' ) );

    foreach ( $mrlpt_client_posts as $mrlpt_client_post ) {
        delete_post_meta( $mrlpt_client_post->ID, '_mrlpt_client_email' );
        delete_post_meta( $mrlpt_client_post->ID, '_mrlpt_client_phone_num' );
        wp_delete_post( $mrlpt_client_post->ID, true );
    }

}

mrlpt_delete_plugin();

Edit:

I have tried using sql queries instead, and again it's not working quite right. I'm working in a multisite set-up, and the posts and post meta data ARE deleted on the MAIN site (wp_). However, the second site (wp_4_) is not working, nothing is deleted.

What am I missing to get this working in multisite? It's very odd because I have another sql query in my main plugin file to dynamically populate a drop-down field in a form and it works fine.

global $wpdb; // Must have this or else!

$postmeta_table = $wpdb->postmeta;
$posts_table = $wpdb->posts;

$wpdb->query("DELETE FROM " . $postmeta_table . " WHERE meta_key = '_mrlpt_client_email'");
$wpdb->query("DELETE FROM " . $postmeta_table . " WHERE meta_key = '_mrlpt_client_phone_num'");
$wpdb->query("DELETE FROM " . $posts_table . " WHERE post_type = 'mrlpt_client'");

Edit 2:

Hard-coding in the prefix of the second site (wp_4_) instead of using $wpdb, it then DOES delete the posts and meta data in the database. But, obviously I can't be hard-coding that in.

Any ideas as to what's going on here? I'm truly stumped.

share|improve this question
    
Anyone? Nothing at all? –  CourtFantinato Apr 21 '12 at 4:27

1 Answer 1

I just came across this in a search and thought I'd reply just in case you didn't figure this out.

I'm not 100% sure the best way to go about this but I can provide some information that may be helpful.

$wpdb has both prefix and base_prefix members, so you can get the 'wp_4_' by referencing $wpdb->prefix and making use of that in your query.

Given what you have you can do a string replace on $postmeta_table and $posts_table so that if they are set to wp_ instead of your prefix (in this case 'wp_4_') then replace it and run your query.

It would be something like:

global $wpdb; // Must have this or else!

$postmeta_table = $wpdb->postmeta;
$posts_table = $wpdb->posts;

$postmeta_table = str_replace($wpdb->base_prefix, $wpdb->prefix, $postmeta_table);
$postmeta_table = str_replace($wpdb->base_prefix, $wpdb->prefix, $postmeta_table);

$wpdb->query("DELETE FROM " . $postmeta_table . " WHERE meta_key = '_mrlpt_client_email'");
$wpdb->query("DELETE FROM " . $postmeta_table . " WHERE meta_key = '_mrlpt_client_phone_num'");
$wpdb->query("DELETE FROM " . $posts_table . " WHERE post_type = 'mrlpt_client'");    

Again, I'm not sure this is the best way to do it but it should get you to where you are trying to go.

References:

share|improve this answer
    
Hi, thanks for the idea but it doesn't work either. Does $wpdb not store both wp_ and wp_#_ prefixes? I am able to call $wpdb->posts for example in the main plugin file and it has no problem knowing which site it is dealing with. The one it does not work with is wp_4_. If I use the plugin on both sites, then deactivate it on both, and delete it from Network Admin, the rows are deleted from the wp_ tables, but not the wp_4_ tables. It's not a question of deleting from one or the other. When the plugin is deleted, all rows created by the plugin should be removed from all sites it was used on. –  CourtFantinato Sep 30 '12 at 22:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.