Désinstaller, activer, désactiver un plugin: fonctionnalités typiques et comment faire
-
-
J'aiperdutellement detemps àessayer de lefairefonctionner.Leproblèmeest que le hookinit nefonctionnepas à l'intérieur des hooks d'enregistrement.Je suppose qu'aucun crochet (action oufiltre)ne fonctionnera sitôt.Lisez lesnotespar lien ci-dessous. http://codex.wordpress.org/Function_Reference/register_activation_hook Cela dit: "L'enregistrement du hook à l'intérieur de votre hookplugins_loadedesttroptardet ilne fonctionnerapas! (Même s'il semblefonctionnerpour register_deactivation_hookjusqu'au 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
-
Je suis celui qui amis àjour le codex à ce que vous avezmentionné,donc ceciest considéré dans la réponse ↑ ci-dessus.:)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 réponses
- votes
-
- 2013-04-09
Pourtester le système actuelpour lesfonctionnalités requises comme la version PHP ou lesextensionsinstallées,vouspouvez utiliser quelque chose comme ça:
<?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 avec un chèquepour 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:
-
Touchez confus,iln'y a doncpas d'appel à `register_activation_hook`ici -pourquoine pas l'utiliser?Est-ce que cela se déclenchera également avant ou après `register_activation_hook`et` register_activation_hook` se déclenchera-t-ilmême si ce quiprécèdene passepas?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
-
Il s'exécute uniquement après le hook d'activation sur lapage duplugin.It runs after the activation hook on the plugin page only.
- 0
- 2013-07-31
- fuxia
-
Je vois -mais si lepluginest activéen dehors de lapage duplugin (parexemple dans le cadre d'une dépendance dethème),vos vérifications serontignoréesnon?J'ai doncessayé de déplacer `add_action ('admin_notices','t5_check_admin_notices',0);` dans un hook d'activationet leplugin s'active sanseffectuer les vérifications...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 aexpliqué lefonctionnement du hook d'activation,je voulaismontrer une alternative.Si lepluginn'estpas activéparpage deplugin,uneerreurfatalepeut seproduire,oui.Cette approchene peutpasfonctionner sur un hook d'activation sans réécriture sérieuse,car ce hook se déclenche après `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
-
Enfait,je suisjustetombé sur lemoyenfacile: http://stackoverflow.com/a/13927297/362445Actually just stumbled on the easy way: http://stackoverflow.com/a/13927297/362445
- 0
- 2013-07-31
- orionrush
-
Pensez que cela devrait lefaire: `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); } } `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
Je crée unplugin WordPress.Quels élémentstypiques dois-jeinclure dans lafonction de désinstallation?
Parexemple,dois-je supprimer lestables quej'ai créées dans lafonction d'installation?
Dois-jenettoyermesentrées d'options?
Autre chose?