Disinstalla, attiva, disattiva un plugin: caratteristiche tipiche e come fare
-
-
Hoperso cosìtantotempo cercando difarlofunzionare.Ilproblema è che l'hookinit nonfunziona all'interno degli hook di registrazione.Suppongo chenessun hook (azione ofiltro)nonfunzionerà cosìpresto.Leggi lenotetramiteil link sottostante. http://codex.wordpress.org/Function_Reference/register_activation_hook Dice: "La registrazione dell'hook all'interno deltuo hookplugins_loaded ètroppotardie non verràeseguito! (Anche quando sembrafunzionareper register_deactivation_hookfino all'hook wp_loaded.)"I wasted so much time trying to get it working. The problem is that init hook not working inside of registering hooks. I suppose that not one hook (action or filter) will not work so early. Read the notes by link below. http://codex.wordpress.org/Function_Reference/register_activation_hook It says: "Registering the hook inside your plugins_loaded hook is too late and it won't run! (Even when it seems to work for register_deactivation_hook until the wp_loaded hook.)"
- 0
- 2011-11-09
- Anton
-
Sonoio che ho aggiornatoil codice a quello che haimenzionato,quindi questo è consideratonella ↑ risposta sopra.:)I'm the one who updated the codex to what you mentioned, so this is considered in the above ↑ answer. :)
- 0
- 2012-02-28
- kaiser
-
1 risposta
- voti
-
- 2013-04-09
Pertestareil sistema correnteper le caratteristiche richieste come la versione PHP o leestensioniinstallatepuoi usare qualcosa delgenere:
<?php # -*- coding: utf-8 -*- /** * Plugin Name: T5 Check Plugin Requirements * Description: Test for PHP version and installed extensions * Plugin URI: * Version: 2013.03.31 * Author: Thomas Scholz * Author URI: http://toscho.de * Licence: MIT * License URI: http://opensource.org/licenses/MIT */ /* * Don't start on every page, the plugin page is enough. */ if ( ! empty ( $GLOBALS['pagenow'] ) && 'plugins.php' === $GLOBALS['pagenow'] ) add_action( 'admin_notices', 't5_check_admin_notices', 0 ); /** * Test current system for the features the plugin needs. * * @return array Errors or empty array */ function t5_check_plugin_requirements() { $php_min_version = '5.4'; // see http://www.php.net/manual/en/extensions.alphabetical.php $extensions = array ( 'iconv', 'mbstring', 'id3' ); $errors = array (); $php_current_version = phpversion(); if ( version_compare( $php_min_version, $php_current_version, '>' ) ) $errors[] = "Your server is running PHP version $php_current_version but this plugin requires at least PHP $php_min_version. Please run an upgrade."; foreach ( $extensions as $extension ) if ( ! extension_loaded( $extension ) ) $errors[] = "Please install the extension $extension to run this plugin."; return $errors; } /** * Call t5_check_plugin_requirements() and deactivate this plugin if there are error. * * @wp-hook admin_notices * @return void */ function t5_check_admin_notices() { $errors = t5_check_plugin_requirements(); if ( empty ( $errors ) ) return; // Suppress "Plugin activated" notice. unset( $_GET['activate'] ); // this plugin's name $name = get_file_data( __FILE__, array ( 'Plugin Name' ), 'plugin' ); printf( '<div class="error"><p>%1$s</p> <p><i>%2$s</i> has been deactivated.</p></div>', join( '</p><p>', $errors ), $name[0] ); deactivate_plugins( plugin_basename( __FILE__ ) ); }
Prova con un controlloper PHP 5.5:
To test the current system for required featurs like PHP version or installed extensions you can use something like that:
<?php # -*- coding: utf-8 -*- /** * Plugin Name: T5 Check Plugin Requirements * Description: Test for PHP version and installed extensions * Plugin URI: * Version: 2013.03.31 * Author: Thomas Scholz * Author URI: http://toscho.de * Licence: MIT * License URI: http://opensource.org/licenses/MIT */ /* * Don't start on every page, the plugin page is enough. */ if ( ! empty ( $GLOBALS['pagenow'] ) && 'plugins.php' === $GLOBALS['pagenow'] ) add_action( 'admin_notices', 't5_check_admin_notices', 0 ); /** * Test current system for the features the plugin needs. * * @return array Errors or empty array */ function t5_check_plugin_requirements() { $php_min_version = '5.4'; // see http://www.php.net/manual/en/extensions.alphabetical.php $extensions = array ( 'iconv', 'mbstring', 'id3' ); $errors = array (); $php_current_version = phpversion(); if ( version_compare( $php_min_version, $php_current_version, '>' ) ) $errors[] = "Your server is running PHP version $php_current_version but this plugin requires at least PHP $php_min_version. Please run an upgrade."; foreach ( $extensions as $extension ) if ( ! extension_loaded( $extension ) ) $errors[] = "Please install the extension $extension to run this plugin."; return $errors; } /** * Call t5_check_plugin_requirements() and deactivate this plugin if there are error. * * @wp-hook admin_notices * @return void */ function t5_check_admin_notices() { $errors = t5_check_plugin_requirements(); if ( empty ( $errors ) ) return; // Suppress "Plugin activated" notice. unset( $_GET['activate'] ); // this plugin's name $name = get_file_data( __FILE__, array ( 'Plugin Name' ), 'plugin' ); printf( '<div class="error"><p>%1$s</p> <p><i>%2$s</i> has been deactivated.</p></div>', join( '</p><p>', $errors ), $name[0] ); deactivate_plugins( plugin_basename( __FILE__ ) ); }
Test with a check for PHP 5.5:
-
Tocca confuso,quindifondamentalmentenon c'è una chiamata a `register_activation_hook` qui -perchénon usarlo?Inoltre questo si attiveràprima o dopo `register_activation_hook`e` register_activation_hook` si attiverà anche se quanto sopranonpassa?Touch confused, so basically there isn't a call to `register_activation_hook` here -- why not use it? Also will this fire before or after `register_activation_hook` and will `register_activation_hook` fire even if the above doesn't pass?
- 0
- 2013-07-31
- orionrush
-
Funziona solo dopo l'hook di attivazionenellapagina delplugin.It runs after the activation hook on the plugin page only.
- 0
- 2013-07-31
- fuxia
-
Capisco,ma seilplug-in viene attivato al difuori dellapagina delplug-in (adesempio comeparte di una dipendenza daltema),i controlli verrannoignoratino?Quindi hoprovato a spostare `add_action ('admin_notices','t5_check_admin_notices',0);`in un hook di attivazionee ilplugin si attiva senzaeseguirei controlli...I see - but if the plugin is activated outside the plugin page (say as part of a theme dependency) then your checks will get skipped no? So I tried moving `add_action( 'admin_notices', 't5_check_admin_notices', 0 );` into an activation hook and the plugin activates without performing the checks . . .
- 0
- 2013-07-31
- orionrush
-
@kaiser ha spiegato comefunziona l'hook di attivazione,volevomostrare un'alternativa.Seilplug-innon viene attivatoper ognipagina delplug-in,potrebbe verificarsi unerrorefatale,sì.Questo approcciononpuòfunzionare su un hook di attivazione senza una seria riscrittura,perché quell'hook si attiva dopo `admin_notices`.@kaiser has explained how the activation hook works, I wanted to show an alternative. If the plugin is not activated per plugin page a fatal error might happen, yes. This approach cannot work on an activation hook without serious rewrite, because that hook fires after `admin_notices`.
- 0
- 2013-07-31
- fuxia
-
In realtà sono appenainciampatonelmodopiù semplice: http://stackoverflow.com/a/13927297/362445Actually just stumbled on the easy way: http://stackoverflow.com/a/13927297/362445
- 0
- 2013-07-31
- orionrush
-
Penso che questo dovrebbefarlo: `register_activation_hook (__FILE__,function () { add_option ('Activated_Plugin','Plugin-Slug'); }); add_action ('admin_init','load_plugin'); funzione load_plugin () { if (! current_user_can ('activation_plugins')) ritorna; if (is_admin () &&get_option ('Activated_Plugin')=='Plugin-Slug') { delete_option ('Activated_Plugin'); add_action ('admin_notices','t5_check_admin_notices',0); } } "Think this should do it: `register_activation_hook( __FILE__, function() { add_option('Activated_Plugin','Plugin-Slug'); }); add_action('admin_init', 'load_plugin'); function load_plugin() { if ( ! current_user_can( 'activate_plugins' ) ) return; if(is_admin()&&get_option('Activated_Plugin')=='Plugin-Slug') { delete_option('Activated_Plugin'); add_action( 'admin_notices', 't5_check_admin_notices', 0 ); } }`
- 0
- 2013-07-31
- orionrush
Sto creando unpluginper WordPress.Quali sono le cosetipiche che dovreiincluderenellafunzione di disinstallazione?
Adesempio,dovreieliminare letabelle che ho creatonellafunzione diinstallazione?
Pulisco le voci dellemie opzioni?
Qualcos'altro?