My widget has a number of options, set using the following method of the extended widget class:
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance[ 'title' ] = ( ! empty( $new_instance[ 'title' ] ) ) ? strip_tags( $new_instance[ 'title' ] ) : '';
$instance[ 'ellcv_category' ] = ( ! empty( $new_instance[ 'ellcv_category' ] ) ) ? $new_instance[ 'ellcv_category' ] : '';
$instance[ 'ellcv_displayTitle' ] = isset( $new_instance[ 'ellcv_displayTitle' ] ) && 'on' == $new_instance[ 'ellcv_displayTitle' ] ? true : false;
$instance[ 'ellcv_displayExcerpt' ] = isset( $new_instance[ 'ellcv_displayExcerpt' ] ) && 'on' == $new_instance[ 'ellcv_displayExcerpt' ] ? true : false;
$instance[ 'ellcv_displayLink' ] = isset( $new_instance[ 'ellcv_displayLink' ] ) && 'on' == $new_instance[ 'ellcv_displayLink' ] ? true : false;
$instance[ 'ellcv_excludeCategory' ] = isset( $new_instance[ 'ellcv_excludeCategory' ] ) && 'on' == $new_instance[ 'ellcv_excludeCategory' ] ? true : false;
$instance[ 'ellcv_hideOnSinglePost' ] = isset( $new_instance[ 'ellcv_hideOnSinglePost' ] ) && 'on' == $new_instance[ 'ellcv_hideOnSinglePost' ] ? true : false;
$instance[ 'ellcv_maxPosts' ] = ( ! empty( $new_instance[ 'ellcv_maxPosts' ] ) ) ? intval( $new_instance[ 'ellcv_maxPosts' ] ) : 5;
$instance[ 'ellcv_maxPostAge' ] = ( ! empty( $new_instance[ 'ellcv_maxPostAge' ] ) ) ? intval( $new_instance[ 'ellcv_maxPostAge' ] ) : null;
$instance[ 'ellcv_defMediaURL' ] = ( ! empty( $new_instance[ 'ellcv_defMediaURL' ] ) ) ? esc_url_raw( $new_instance[ 'ellcv_defMediaURL' ] ) : null;
// Save site options (required so constructor function can get option before widget loaded)
update_option( 'ellcv_excludeCat', $instance[ 'ellcv_excludeCategory' ] );
update_option( 'ellcv_catID', $instance[ 'ellcv_category' ] );
return $instance;
}
I’d like to be able to remove plugin options on plugin uninstall.
I’ve created an uninstall.php file for my plugin
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Exit if we don't see the uninstall flag
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
// Delete site options
delete_option('ellcv_excludeCat');
delete_option('ellcv_catID');
?>
So, I’m able to delete the options set as site options from the database, but what I’m still unclear about is how to also delete the other options, saved/recalled as instance properties from the WP database. I don’t know how to get a handle on them to remove them, from my uninstall.php.
I’m hazy on OOP techniques generally, unfortunately, being self-taught, and having been using functional programming techniques for many years.
The code for the entire widget is quite long, so I don’t want to post it here, but it’s basically the same, structurally as the example I link to above.
Any hints gratefully accepted.
a|x
Any tips
]]>When I uninstall my plugin I want to remove all events (custom post type) and event postmeta from database. I use this code in uninstall.php for that:
// Delete events + metadata
global $wpdb;
$wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_type IN ( 'event' );" );
$wpdb->query( "DELETE meta FROM {$wpdb->postmeta} meta LEFT JOIN {$wpdb->posts} posts ON posts.ID = meta.post_id WHERE posts.ID IS NULL;" );
Taken from Woocommerce, and it works like a charm.
My question: the second query removes the event postmeta, but I don’t understand how it’s part of the first query? I mean, why does it only remove postmeta from events and not from all posts?
Guido
]]>Codex:
//if uninstall not called from WordPress exit
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) )
exit ();
delete_option('example');
and alternate source (wptuts):
if(defined('WP_UNINSTALL_PLUGIN') ){
//delete options, tables or anything else
}
So which is the correct way? I’m leaning towards the wptuts way, but only because it seems to make a bit more sense to me.
Thanks guys
// my-plugin/my-plugin.php
<?php
/*
Plugin Name: My Plugin
*/
// my-plugin/uninstall.php
<?php
When I click ‘Delete’ and then confirm, I get the following error:
Plugin could not be deleted due to an error: Could not fully remove the plugin(s) my-plugin/my-plugin.php.
What’s wrong here?
—
~/Sites/wordpress/wp-content/plugins/my-plugin $ ls -ll
total 16
-rwxrwxrwx@ 1 me staff 34 13 Aug 21:43 my-plugin.php
-rwxrwxrwx@ 1 me staff 6 13 Aug 21:44 uninstall.php
]]>function sb_uninstall_plugin(){
global $wpdb, $sb_ip_log;
delete_option("sb_detection_of_spammers_cookies");
delete_option("sb_detection_of_spammers_time");
delete_option("sb_banned_message");
$wpdb->query("DROP TABLE $sb_ip_log");
}
register_uninstall_hook(__FILE__, 'sb_uninstall_plugin');
The value of the option uninstall_plugins in the table wp_options: a:2:{i:0;b:0;s:22:”sb/spammer-blocker.php”;s:19:”sb_uninstall_plugin”;}
I also tried using uninstall.php without any success.
Thank you for your help!
delete_option('my_plugin_option');
However, if my plugin was running on a multisite installation, and it was activated and/or used on multiple subsites, then the above method won’t work. The above method will remove the option from the wp_options
table, but it won’t remove it from tables wp_2_options
, wp_3_options
, etc.
I know I can just write a complete SQL script to go through all the tables and delete the options, but it seems like there needs to be a much more user-friendly way to help ensure all plugin authors are able to clean up after themselves.
Anyone know of such a way?
]]>