Is it possible to show a warning message for a certain plugin update? For example from version 0.3 or lower to 0.4 or higher?
EDIT: I need something like upgrade notice (wich doesn't work correctly at the moment) i found a interesting article here.
Is it possible to show a warning message for a certain plugin update? For example from version 0.3 or lower to 0.4 or higher?
EDIT: I need something like upgrade notice (wich doesn't work correctly at the moment) i found a interesting article here.
Share Improve this question edited Jun 2, 2016 at 5:09 GDY asked Jun 1, 2016 at 12:17 GDYGDY 4484 silver badges19 bronze badges 3- 1 Could you please elaborate what you want and what you have tried so far? – cjbj Commented Jun 1, 2016 at 14:55
- before update, after update, during update? please be more descriptive – Mark Kaplun Commented Jun 1, 2016 at 15:36
- it is for critical updates where the users have to modify some theme components needed for the plugin. So the warning should be definitely before the update – GDY Commented Jun 2, 2016 at 4:57
3 Answers
Reset to default 1Not possible unless you plan in advance in your plugin for such a possibility. And even if it was somehow possible you are still not likely to have any user read the message and actually understand it.
A more realistic approach is to do a two steps upgrade, in the first vesion of your plugin to maintain appropriate backward compatibility and only later you remove it from your code, giving both you the option to give proper notification, and the user time to adjust whatever he needs.
Being that this is over 2 years old and all, I imagine, at least I would hope, that by now the updates have rolled out and were either handled without issues or caused a bunch of breakages that then had to be fixed. For anyone reading this, I think it does a perfect job at pointing out what Mark Kaplun eluded to in the comments of the accepted answer. There are an absurd number of plugins out there for WordPress, and it is actually very common even in very popular plugins, for a developer or whole development team to just go about doing something in so much the wrong way. This is why updates so often break sites. If you're listening, please stop it, LOL.
In this case, as revealed in the comments, the developer has a plugin that takes direct function calls from the theme, presumably not a unique theme that he has any control over and can push updates to, hence the predicament. There is a very obvious and simple solution to the problem that would have solved it cleanly, and avoided any breakages for all future versions regardless of whether or not the functions in the theme were ever updated. Not to mention, it's also very bad practice to tell your users that they have to go through ever piece of their code in their theme where they called your function and change it now, because you couldn't be bothered to handle the function update correctly. Issuing a notice to warn users about this "required" change to all function calls, is the completely wrong approach to solving this problem, however the developer couldn't see that. He only knew how he wanted to try to solve it, and so he asked only the question that mattered to him, and it was also the wrong question.
Assuming the code goes something like this:
// Plugin functions
...
// version 0.3
public function get_module($id, $name){
// code that only handles $id and $name parameters
}
// version 0.4
public function get_module($module){
// code that only handles $module as an object
}
// Theme code
$modulator = new Plugin_Class();
$module = $modulator->get_module(21, 'My Module');
The real problem has nothing to do with notices or warnings to display to the user. As soon as version 0.4 pushes out, presumably everyone who's ever used this plugin will see there site break on a totally avoidable fatal error because the plugin code is now expecting an object and they are passing it an id and a name, potentially in a lot of places throughout their code, that they now have to track down and deal with because the developer didn't bother handling it correctly. The real solution should look like this:
// Plugin functions
...
// version 0.4
public function get_module_by_id($id){
//code to retrieve and return the module object by its ID
return $module
}
public function get_module($module){
if(!is_object($module)){
$module = $this->get_module_by_id($module);
}
// now you have your module object that you need, the rest of the code should be exactly as it would have for version 0.4
}
// Theme code
$modulator = new Plugin_Class();
$module = $modulator->get_module(21, 'My Module');
// Still works in every future version of the plugin, until you decide to break it another way with an update of course.
As I said before, there's a time and place for notices/warnings to the user, but this isn't one of them. Think before you code, your users will appreciate it even if they don't know that they do. ;-)
You can access the full list of available plugin updates with get_plugin_updates()
function.
You just need to verify if your plugin is there, then check with the versions, and the best way to output a warning in the admin endpoint is with admin_notices
hook
Specify your plugin name in the $requested_name
variable (line:4)
add_action('admin_init', function() {
// Specify here the plugin name
$requested_name = 'AJAX File Upload';
function se_plugin_has_update( $name ) {
if ( ! function_exists( 'get_plugin_updates' ) ) {
require_once ABSPATH . 'wp-admin/includes/update.php';
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$list = get_plugin_updates();
$data = array();
foreach( $list as $i => $item ) {
if( strtolower( $name ) == strtolower( $item->Name ) ) {
$data = $list[$i];
}
}
if( ! empty( $data ) ) {
$data = array(
'name' => $data->Name,
'version' => $data->Version,
'new_version' => $data->update->new_version,
'url' => $data->update->url,
'package' => $data->update->package
);
} else {
$data = array();
}
return $data;
}
$update_data = se_plugin_has_update( $requested_name );
// var_dump( $update_data ) or print_r( $update_data ) for debugging
if( ! empty( $update_data ) ) { // has update avail
// You can avoid for specific versions (current: $update_data['version'] and latest: $update_data['new_version'])
$GLOBALS['se_plugin_has_update_data'] = $update_data;
add_action( 'admin_notices', function() {
global $se_plugin_has_update_data;
$update_data = $se_plugin_has_update_data;
?>
<div id="updated" class="error notice is-dismissible">
<p><?php echo sprintf(
"\"%s\" plugin has updates available. You are running v. %s while the latest version is %s",
$update_data['name'],
$update_data['version'],
$update_data['new_version']
); ?></p>
</div>
<?php
});
}
});
Hope that helps.