3 * WordPress Plugin Administration API
6 * @subpackage Administration
10 * Parses the plugin contents to retrieve plugin's metadata.
12 * The metadata of the plugin's data searches for the following in the plugin's
13 * header. All plugin data must be on its own line. For plugin description, it
14 * must not have any newlines or only parts of the description will be displayed
15 * and the same goes for the plugin data. The below is formatted for printing.
18 * Plugin Name: Name of Plugin
19 * Plugin URI: Link to plugin information
20 * Description: Plugin Description
21 * Author: Plugin author's name
22 * Author URI: Link to the author's web site
23 * Version: Must be set in the plugin for WordPress 2.3+
24 * Text Domain: Optional. Unique identifier, should be same as the one used in
25 * load_plugin_textdomain()
26 * Domain Path: Optional. Only useful if the translations are located in a
27 * folder above the plugin's base path. For example, if .mo files are
28 * located in the locale folder then Domain Path will be "/locale/" and
29 * must have the first slash. Defaults to the base folder the plugin is
31 * Network: Optional. Specify "Network: true" to require that a plugin is activated
32 * across all sites in an installation. This will prevent a plugin from being
33 * activated on a single site when Multisite is enabled.
34 * * / # Remove the space to close comment
36 * Some users have issues with opening large files and manipulating the contents
37 * for want is usually the first 1kiB or 2kiB. This function stops pulling in
38 * the plugin contents when it has all of the required plugin data.
40 * The first 8kiB of the file will be pulled in and if the plugin data is not
41 * within that first 8kiB, then the plugin author should correct their plugin
42 * and move the plugin data headers to the top.
44 * The plugin file is assumed to have permissions to allow for scripts to read
45 * the file. This is not checked however and the file is only opened for
50 * @param string $plugin_file Path to the plugin file
51 * @param bool $markup Optional. If the returned data should have HTML markup applied.
53 * @param bool $translate Optional. If the returned data should be translated. Default true.
55 * Plugin data. Values will be empty if not supplied by the plugin.
57 * @type string $Name Name of the plugin. Should be unique.
58 * @type string $Title Title of the plugin and link to the plugin's site (if set).
59 * @type string $Description Plugin description.
60 * @type string $Author Author's name.
61 * @type string $AuthorURI Author's website address (if set).
62 * @type string $Version Plugin version.
63 * @type string $TextDomain Plugin textdomain.
64 * @type string $DomainPath Plugins relative directory path to .mo files.
65 * @type bool $Network Whether the plugin can only be activated network-wide.
68 function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
70 $default_headers = array(
71 'Name' => 'Plugin Name',
72 'PluginURI' => 'Plugin URI',
73 'Version' => 'Version',
74 'Description' => 'Description',
76 'AuthorURI' => 'Author URI',
77 'TextDomain' => 'Text Domain',
78 'DomainPath' => 'Domain Path',
79 'Network' => 'Network',
80 // Site Wide Only is deprecated in favor of Network.
81 '_sitewide' => 'Site Wide Only',
84 $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
86 // Site Wide Only is the old header for Network
87 if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) {
88 /* translators: 1: Site Wide Only: true, 2: Network: true */
89 _deprecated_argument( __FUNCTION__, '3.0.0', sprintf( __( 'The %1$s plugin header is deprecated. Use %2$s instead.' ), '<code>Site Wide Only: true</code>', '<code>Network: true</code>' ) );
90 $plugin_data['Network'] = $plugin_data['_sitewide'];
92 $plugin_data['Network'] = ( 'true' == strtolower( $plugin_data['Network'] ) );
93 unset( $plugin_data['_sitewide'] );
95 // If no text domain is defined fall back to the plugin slug.
96 if ( ! $plugin_data['TextDomain'] ) {
97 $plugin_slug = dirname( plugin_basename( $plugin_file ) );
98 if ( '.' !== $plugin_slug && false === strpos( $plugin_slug, '/' ) ) {
99 $plugin_data['TextDomain'] = $plugin_slug;
103 if ( $markup || $translate ) {
104 $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
106 $plugin_data['Title'] = $plugin_data['Name'];
107 $plugin_data['AuthorName'] = $plugin_data['Author'];
114 * Sanitizes plugin data, optionally adds markup, optionally translates.
118 * @see get_plugin_data()
120 function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) {
122 // Sanitize the plugin filename to a WP_PLUGIN_DIR relative path
123 $plugin_file = plugin_basename( $plugin_file );
127 if ( $textdomain = $plugin_data['TextDomain'] ) {
128 if ( ! is_textdomain_loaded( $textdomain ) ) {
129 if ( $plugin_data['DomainPath'] ) {
130 load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] );
132 load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) );
135 } elseif ( 'hello.php' == basename( $plugin_file ) ) {
136 $textdomain = 'default';
139 foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field )
140 $plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain );
145 $allowed_tags = $allowed_tags_in_links = array(
146 'abbr' => array( 'title' => true ),
147 'acronym' => array( 'title' => true ),
152 $allowed_tags['a'] = array( 'href' => true, 'title' => true );
154 // Name is marked up inside <a> tags. Don't allow these.
155 // Author is too, but some plugins have used <a> here (omitting Author URI).
156 $plugin_data['Name'] = wp_kses( $plugin_data['Name'], $allowed_tags_in_links );
157 $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $allowed_tags );
159 $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags );
160 $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $allowed_tags );
162 $plugin_data['PluginURI'] = esc_url( $plugin_data['PluginURI'] );
163 $plugin_data['AuthorURI'] = esc_url( $plugin_data['AuthorURI'] );
165 $plugin_data['Title'] = $plugin_data['Name'];
166 $plugin_data['AuthorName'] = $plugin_data['Author'];
170 if ( $plugin_data['PluginURI'] && $plugin_data['Name'] )
171 $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '">' . $plugin_data['Name'] . '</a>';
173 if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] )
174 $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '">' . $plugin_data['Author'] . '</a>';
176 $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
178 if ( $plugin_data['Author'] )
179 $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s.'), $plugin_data['Author'] ) . '</cite>';
186 * Get a list of a plugin's files.
190 * @param string $plugin Plugin ID
191 * @return array List of files relative to the plugin root.
193 function get_plugin_files($plugin) {
194 $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
195 $dir = dirname($plugin_file);
196 $plugin_files = array($plugin);
197 if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) {
198 $plugins_dir = @ opendir( $dir );
199 if ( $plugins_dir ) {
200 while (($file = readdir( $plugins_dir ) ) !== false ) {
201 if ( substr($file, 0, 1) == '.' )
203 if ( is_dir( $dir . '/' . $file ) ) {
204 $plugins_subdir = @ opendir( $dir . '/' . $file );
205 if ( $plugins_subdir ) {
206 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
207 if ( substr($subfile, 0, 1) == '.' )
209 $plugin_files[] = plugin_basename("$dir/$file/$subfile");
211 @closedir( $plugins_subdir );
214 if ( plugin_basename("$dir/$file") != $plugin )
215 $plugin_files[] = plugin_basename("$dir/$file");
218 @closedir( $plugins_dir );
222 return $plugin_files;
226 * Check the plugins directory and retrieve all plugin files with plugin data.
228 * WordPress only supports plugin files in the base plugins directory
229 * (wp-content/plugins) and in one directory above the plugins directory
230 * (wp-content/plugins/my-plugin). The file it looks for has the plugin data
231 * and must be found in those two locations. It is recommended to keep your
232 * plugin files in their own directories.
234 * The file with the plugin data is the file that will be included and therefore
235 * needs to have the main execution for the plugin. This does not mean
236 * everything must be contained in the file and it is recommended that the file
237 * be split for maintainability. Keep everything in one file for extreme
238 * optimization purposes.
242 * @param string $plugin_folder Optional. Relative path to single plugin folder.
243 * @return array Key is the plugin file path and the value is an array of the plugin data.
245 function get_plugins($plugin_folder = '') {
247 if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
248 $cache_plugins = array();
250 if ( isset($cache_plugins[ $plugin_folder ]) )
251 return $cache_plugins[ $plugin_folder ];
253 $wp_plugins = array ();
254 $plugin_root = WP_PLUGIN_DIR;
255 if ( !empty($plugin_folder) )
256 $plugin_root .= $plugin_folder;
258 // Files in wp-content/plugins directory
259 $plugins_dir = @ opendir( $plugin_root);
260 $plugin_files = array();
261 if ( $plugins_dir ) {
262 while (($file = readdir( $plugins_dir ) ) !== false ) {
263 if ( substr($file, 0, 1) == '.' )
265 if ( is_dir( $plugin_root.'/'.$file ) ) {
266 $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
267 if ( $plugins_subdir ) {
268 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
269 if ( substr($subfile, 0, 1) == '.' )
271 if ( substr($subfile, -4) == '.php' )
272 $plugin_files[] = "$file/$subfile";
274 closedir( $plugins_subdir );
277 if ( substr($file, -4) == '.php' )
278 $plugin_files[] = $file;
281 closedir( $plugins_dir );
284 if ( empty($plugin_files) )
287 foreach ( $plugin_files as $plugin_file ) {
288 if ( !is_readable( "$plugin_root/$plugin_file" ) )
291 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
293 if ( empty ( $plugin_data['Name'] ) )
296 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
299 uasort( $wp_plugins, '_sort_uname_callback' );
301 $cache_plugins[ $plugin_folder ] = $wp_plugins;
302 wp_cache_set('plugins', $cache_plugins, 'plugins');
308 * Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data.
310 * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins).
313 * @return array Key is the mu-plugin file path and the value is an array of the mu-plugin data.
315 function get_mu_plugins() {
316 $wp_plugins = array();
317 // Files in wp-content/mu-plugins directory
318 $plugin_files = array();
320 if ( ! is_dir( WPMU_PLUGIN_DIR ) )
322 if ( $plugins_dir = @ opendir( WPMU_PLUGIN_DIR ) ) {
323 while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
324 if ( substr( $file, -4 ) == '.php' )
325 $plugin_files[] = $file;
331 @closedir( $plugins_dir );
333 if ( empty($plugin_files) )
336 foreach ( $plugin_files as $plugin_file ) {
337 if ( !is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) )
340 $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
342 if ( empty ( $plugin_data['Name'] ) )
343 $plugin_data['Name'] = $plugin_file;
345 $wp_plugins[ $plugin_file ] = $plugin_data;
348 if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php') <= 30 ) // silence is golden
349 unset( $wp_plugins['index.php'] );
351 uasort( $wp_plugins, '_sort_uname_callback' );
357 * Callback to sort array by a 'Name' key.
362 function _sort_uname_callback( $a, $b ) {
363 return strnatcasecmp( $a['Name'], $b['Name'] );
367 * Check the wp-content directory and retrieve all drop-ins with any plugin data.
370 * @return array Key is the file path and the value is an array of the plugin data.
372 function get_dropins() {
374 $plugin_files = array();
376 $_dropins = _get_dropins();
378 // These exist in the wp-content directory
379 if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) {
380 while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
381 if ( isset( $_dropins[ $file ] ) )
382 $plugin_files[] = $file;
388 @closedir( $plugins_dir );
390 if ( empty($plugin_files) )
393 foreach ( $plugin_files as $plugin_file ) {
394 if ( !is_readable( WP_CONTENT_DIR . "/$plugin_file" ) )
396 $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
397 if ( empty( $plugin_data['Name'] ) )
398 $plugin_data['Name'] = $plugin_file;
399 $dropins[ $plugin_file ] = $plugin_data;
402 uksort( $dropins, 'strnatcasecmp' );
408 * Returns drop-ins that WordPress uses.
410 * Includes Multisite drop-ins only when is_multisite()
413 * @return array Key is file name. The value is an array, with the first value the
414 * purpose of the drop-in and the second value the name of the constant that must be
415 * true for the drop-in to be used, or true if no constant is required.
417 function _get_dropins() {
419 'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE
420 'db.php' => array( __( 'Custom database class.' ), true ), // auto on load
421 'db-error.php' => array( __( 'Custom database error message.' ), true ), // auto on error
422 'install.php' => array( __( 'Custom install script.' ), true ), // auto on install
423 'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // auto on maintenance
424 'object-cache.php' => array( __( 'External object cache.' ), true ), // auto on load
427 if ( is_multisite() ) {
428 $dropins['sunrise.php' ] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE
429 $dropins['blog-deleted.php' ] = array( __( 'Custom site deleted message.' ), true ); // auto on deleted blog
430 $dropins['blog-inactive.php' ] = array( __( 'Custom site inactive message.' ), true ); // auto on inactive blog
431 $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // auto on archived or spammed blog
438 * Check whether a plugin is active.
440 * Only plugins installed in the plugins/ folder can be active.
442 * Plugins in the mu-plugins/ folder can't be "activated," so this function will
443 * return false for those plugins.
447 * @param string $plugin Base plugin path from plugins directory.
448 * @return bool True, if in the active plugins list. False, not in the list.
450 function is_plugin_active( $plugin ) {
451 return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
455 * Check whether the plugin is inactive.
457 * Reverse of is_plugin_active(). Used as a callback.
460 * @see is_plugin_active()
462 * @param string $plugin Base plugin path from plugins directory.
463 * @return bool True if inactive. False if active.
465 function is_plugin_inactive( $plugin ) {
466 return ! is_plugin_active( $plugin );
470 * Check whether the plugin is active for the entire network.
472 * Only plugins installed in the plugins/ folder can be active.
474 * Plugins in the mu-plugins/ folder can't be "activated," so this function will
475 * return false for those plugins.
479 * @param string $plugin Base plugin path from plugins directory.
480 * @return bool True, if active for the network, otherwise false.
482 function is_plugin_active_for_network( $plugin ) {
483 if ( !is_multisite() )
486 $plugins = get_site_option( 'active_sitewide_plugins');
487 if ( isset($plugins[$plugin]) )
494 * Checks for "Network: true" in the plugin header to see if this should
495 * be activated only as a network wide plugin. The plugin would also work
496 * when Multisite is not enabled.
498 * Checks for "Site Wide Only: true" for backward compatibility.
502 * @param string $plugin Plugin to check
503 * @return bool True if plugin is network only, false otherwise.
505 function is_network_only_plugin( $plugin ) {
506 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
508 return $plugin_data['Network'];
513 * Attempts activation of plugin in a "sandbox" and redirects on success.
515 * A plugin that is already activated will not attempt to be activated again.
517 * The way it works is by setting the redirection to the error before trying to
518 * include the plugin file. If the plugin fails, then the redirection will not
519 * be overwritten with the success message. Also, the options will not be
520 * updated and the activation hook will not be called on plugin error.
522 * It should be noted that in no way the below code will actually prevent errors
523 * within the file. The code should not be used elsewhere to replicate the
524 * "sandbox", which uses redirection to work.
527 * If any errors are found or text is outputted, then it will be captured to
528 * ensure that the success redirection will update the error redirection.
532 * @param string $plugin Plugin path to main plugin file with plugin data.
533 * @param string $redirect Optional. URL to redirect to.
534 * @param bool $network_wide Optional. Whether to enable the plugin for all sites in the network
535 * or just the current site. Multisite only. Default false.
536 * @param bool $silent Optional. Whether to prevent calling activation hooks. Default false.
537 * @return WP_Error|null WP_Error on invalid file or null on success.
539 function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
540 $plugin = plugin_basename( trim( $plugin ) );
542 if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) {
543 $network_wide = true;
544 $current = get_site_option( 'active_sitewide_plugins', array() );
545 $_GET['networkwide'] = 1; // Back compat for plugins looking for this value.
547 $current = get_option( 'active_plugins', array() );
550 $valid = validate_plugin($plugin);
551 if ( is_wp_error($valid) )
554 if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current ) ) ) {
555 if ( !empty($redirect) )
556 wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error
558 wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
559 $_wp_plugin_file = $plugin;
560 include_once( WP_PLUGIN_DIR . '/' . $plugin );
561 $plugin = $_wp_plugin_file; // Avoid stomping of the $plugin variable in a plugin.
565 * Fires before a plugin is activated.
567 * If a plugin is silently activated (such as during an update),
568 * this hook does not fire.
572 * @param string $plugin Plugin path to main plugin file with plugin data.
573 * @param bool $network_wide Whether to enable the plugin for all sites in the network
574 * or just the current site. Multisite only. Default is false.
576 do_action( 'activate_plugin', $plugin, $network_wide );
579 * Fires as a specific plugin is being activated.
581 * This hook is the "activation" hook used internally by register_activation_hook().
582 * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
584 * If a plugin is silently activated (such as during an update), this hook does not fire.
588 * @param bool $network_wide Whether to enable the plugin for all sites in the network
589 * or just the current site. Multisite only. Default is false.
591 do_action( "activate_{$plugin}", $network_wide );
594 if ( $network_wide ) {
595 $current = get_site_option( 'active_sitewide_plugins', array() );
596 $current[$plugin] = time();
597 update_site_option( 'active_sitewide_plugins', $current );
599 $current = get_option( 'active_plugins', array() );
600 $current[] = $plugin;
602 update_option('active_plugins', $current);
607 * Fires after a plugin has been activated.
609 * If a plugin is silently activated (such as during an update),
610 * this hook does not fire.
614 * @param string $plugin Plugin path to main plugin file with plugin data.
615 * @param bool $network_wide Whether to enable the plugin for all sites in the network
616 * or just the current site. Multisite only. Default is false.
618 do_action( 'activated_plugin', $plugin, $network_wide );
621 if ( ob_get_length() > 0 ) {
622 $output = ob_get_clean();
623 return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
632 * Deactivate a single plugin or multiple plugins.
634 * The deactivation hook is disabled by the plugin upgrader by using the $silent
639 * @param string|array $plugins Single plugin or list of plugins to deactivate.
640 * @param bool $silent Prevent calling deactivation hooks. Default is false.
641 * @param mixed $network_wide Whether to deactivate the plugin for all sites in the network.
642 * A value of null (the default) will deactivate plugins for both the site and the network.
644 function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) {
645 if ( is_multisite() )
646 $network_current = get_site_option( 'active_sitewide_plugins', array() );
647 $current = get_option( 'active_plugins', array() );
648 $do_blog = $do_network = false;
650 foreach ( (array) $plugins as $plugin ) {
651 $plugin = plugin_basename( trim( $plugin ) );
652 if ( ! is_plugin_active($plugin) )
655 $network_deactivating = false !== $network_wide && is_plugin_active_for_network( $plugin );
659 * Fires before a plugin is deactivated.
661 * If a plugin is silently deactivated (such as during an update),
662 * this hook does not fire.
666 * @param string $plugin Plugin path to main plugin file with plugin data.
667 * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
668 * or just the current site. Multisite only. Default is false.
670 do_action( 'deactivate_plugin', $plugin, $network_deactivating );
673 if ( false !== $network_wide ) {
674 if ( is_plugin_active_for_network( $plugin ) ) {
676 unset( $network_current[ $plugin ] );
677 } elseif ( $network_wide ) {
682 if ( true !== $network_wide ) {
683 $key = array_search( $plugin, $current );
684 if ( false !== $key ) {
686 unset( $current[ $key ] );
692 * Fires as a specific plugin is being deactivated.
694 * This hook is the "deactivation" hook used internally by register_deactivation_hook().
695 * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
697 * If a plugin is silently deactivated (such as during an update), this hook does not fire.
701 * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
702 * or just the current site. Multisite only. Default is false.
704 do_action( "deactivate_{$plugin}", $network_deactivating );
707 * Fires after a plugin is deactivated.
709 * If a plugin is silently deactivated (such as during an update),
710 * this hook does not fire.
714 * @param string $plugin Plugin basename.
715 * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
716 * or just the current site. Multisite only. Default false.
718 do_action( 'deactivated_plugin', $plugin, $network_deactivating );
723 update_option('active_plugins', $current);
725 update_site_option( 'active_sitewide_plugins', $network_current );
729 * Activate multiple plugins.
731 * When WP_Error is returned, it does not mean that one of the plugins had
732 * errors. It means that one or more of the plugins file path was invalid.
734 * The execution will be halted as soon as one of the plugins has an error.
738 * @param string|array $plugins Single plugin or list of plugins to activate.
739 * @param string $redirect Redirect to page after successful activation.
740 * @param bool $network_wide Whether to enable the plugin for all sites in the network.
741 * @param bool $silent Prevent calling activation hooks. Default is false.
742 * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
744 function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
745 if ( !is_array($plugins) )
746 $plugins = array($plugins);
749 foreach ( $plugins as $plugin ) {
750 if ( !empty($redirect) )
751 $redirect = add_query_arg('plugin', $plugin, $redirect);
752 $result = activate_plugin($plugin, $redirect, $network_wide, $silent);
753 if ( is_wp_error($result) )
754 $errors[$plugin] = $result;
757 if ( !empty($errors) )
758 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
764 * Remove directory and files of a plugin for a list of plugins.
768 * @global WP_Filesystem_Base $wp_filesystem
770 * @param array $plugins List of plugins to delete.
771 * @param string $deprecated Deprecated.
772 * @return bool|null|WP_Error True on success, false is $plugins is empty, WP_Error on failure.
773 * Null if filesystem credentials are required to proceed.
775 function delete_plugins( $plugins, $deprecated = '' ) {
776 global $wp_filesystem;
778 if ( empty($plugins) )
782 foreach ( $plugins as $plugin )
783 $checked[] = 'checked[]=' . $plugin;
785 $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-plugins');
788 $credentials = request_filesystem_credentials( $url );
789 $data = ob_get_clean();
791 if ( false === $credentials ) {
792 if ( ! empty($data) ){
793 include_once( ABSPATH . 'wp-admin/admin-header.php');
795 include( ABSPATH . 'wp-admin/admin-footer.php');
801 if ( ! WP_Filesystem( $credentials ) ) {
803 request_filesystem_credentials( $url, '', true ); // Failed to connect, Error and request again.
804 $data = ob_get_clean();
806 if ( ! empty($data) ){
807 include_once( ABSPATH . 'wp-admin/admin-header.php');
809 include( ABSPATH . 'wp-admin/admin-footer.php');
815 if ( ! is_object($wp_filesystem) )
816 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
818 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
819 return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
821 // Get the base plugin folder.
822 $plugins_dir = $wp_filesystem->wp_plugins_dir();
823 if ( empty( $plugins_dir ) ) {
824 return new WP_Error( 'fs_no_plugins_dir', __( 'Unable to locate WordPress plugin directory.' ) );
827 $plugins_dir = trailingslashit( $plugins_dir );
829 $plugin_translations = wp_get_installed_translations( 'plugins' );
833 foreach ( $plugins as $plugin_file ) {
834 // Run Uninstall hook.
835 if ( is_uninstallable_plugin( $plugin_file ) ) {
836 uninstall_plugin($plugin_file);
840 * Fires immediately before a plugin deletion attempt.
844 * @param string $plugin_file Plugin file name.
846 do_action( 'delete_plugin', $plugin_file );
848 $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin_file ) );
850 // If plugin is in its own directory, recursively delete the directory.
851 if ( strpos( $plugin_file, '/' ) && $this_plugin_dir != $plugins_dir ) { //base check on if plugin includes directory separator AND that it's not the root plugin folder
852 $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
854 $deleted = $wp_filesystem->delete( $plugins_dir . $plugin_file );
858 * Fires immediately after a plugin deletion attempt.
862 * @param string $plugin_file Plugin file name.
863 * @param bool $deleted Whether the plugin deletion was successful.
865 do_action( 'deleted_plugin', $plugin_file, $deleted );
868 $errors[] = $plugin_file;
872 // Remove language files, silently.
873 $plugin_slug = dirname( $plugin_file );
874 if ( '.' !== $plugin_slug && ! empty( $plugin_translations[ $plugin_slug ] ) ) {
875 $translations = $plugin_translations[ $plugin_slug ];
877 foreach ( $translations as $translation => $data ) {
878 $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.po' );
879 $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.mo' );
884 // Remove deleted plugins from the plugin updates list.
885 if ( $current = get_site_transient('update_plugins') ) {
886 // Don't remove the plugins that weren't deleted.
887 $deleted = array_diff( $plugins, $errors );
889 foreach ( $deleted as $plugin_file ) {
890 unset( $current->response[ $plugin_file ] );
893 set_site_transient( 'update_plugins', $current );
896 if ( ! empty($errors) )
897 return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s.'), implode(', ', $errors)) );
903 * Validate active plugins
905 * Validate all active plugins, deactivates invalid and
906 * returns an array of deactivated ones.
909 * @return array invalid plugins, plugin as key, error as value
911 function validate_active_plugins() {
912 $plugins = get_option( 'active_plugins', array() );
913 // Validate vartype: array.
914 if ( ! is_array( $plugins ) ) {
915 update_option( 'active_plugins', array() );
919 if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
920 $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
921 $plugins = array_merge( $plugins, array_keys( $network_plugins ) );
924 if ( empty( $plugins ) )
929 // Invalid plugins get deactivated.
930 foreach ( $plugins as $plugin ) {
931 $result = validate_plugin( $plugin );
932 if ( is_wp_error( $result ) ) {
933 $invalid[$plugin] = $result;
934 deactivate_plugins( $plugin, true );
941 * Validate the plugin path.
943 * Checks that the file exists and is a valid file. See validate_file().
947 * @param string $plugin Plugin Path.
948 * @return WP_Error|int 0 on success, WP_Error on failure.
950 function validate_plugin($plugin) {
951 if ( validate_file($plugin) )
952 return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
953 if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
954 return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
956 $installed_plugins = get_plugins();
957 if ( ! isset($installed_plugins[$plugin]) )
958 return new WP_Error('no_plugin_header', __('The plugin does not have a valid header.'));
963 * Whether the plugin can be uninstalled.
967 * @param string $plugin Plugin path to check.
968 * @return bool Whether plugin can be uninstalled.
970 function is_uninstallable_plugin($plugin) {
971 $file = plugin_basename($plugin);
973 $uninstallable_plugins = (array) get_option('uninstall_plugins');
974 if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
981 * Uninstall a single plugin.
983 * Calls the uninstall hook, if it is available.
987 * @param string $plugin Relative plugin path from Plugin Directory.
988 * @return true True if a plugin's uninstall.php file has been found and included.
990 function uninstall_plugin($plugin) {
991 $file = plugin_basename($plugin);
993 $uninstallable_plugins = (array) get_option('uninstall_plugins');
996 * Fires in uninstall_plugin() immediately before the plugin is uninstalled.
1000 * @param string $plugin Relative plugin path from plugin directory.
1001 * @param array $uninstallable_plugins Uninstallable plugins.
1003 do_action( 'pre_uninstall_plugin', $plugin, $uninstallable_plugins );
1005 if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
1006 if ( isset( $uninstallable_plugins[$file] ) ) {
1007 unset($uninstallable_plugins[$file]);
1008 update_option('uninstall_plugins', $uninstallable_plugins);
1010 unset($uninstallable_plugins);
1012 define('WP_UNINSTALL_PLUGIN', $file);
1013 wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
1014 include( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' );
1019 if ( isset( $uninstallable_plugins[$file] ) ) {
1020 $callable = $uninstallable_plugins[$file];
1021 unset($uninstallable_plugins[$file]);
1022 update_option('uninstall_plugins', $uninstallable_plugins);
1023 unset($uninstallable_plugins);
1025 wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
1026 include( WP_PLUGIN_DIR . '/' . $file );
1028 add_action( 'uninstall_' . $file, $callable );
1031 * Fires in uninstall_plugin() once the plugin has been uninstalled.
1033 * The action concatenates the 'uninstall_' prefix with the basename of the
1034 * plugin passed to uninstall_plugin() to create a dynamically-named action.
1038 do_action( 'uninstall_' . $file );
1047 * Add a top-level menu page.
1049 * This function takes a capability which will be used to determine whether
1050 * or not a page is included in the menu.
1052 * The function which is hooked in to handle the output of the page must check
1053 * that the user has the required capability as well.
1055 * @global array $menu
1056 * @global array $admin_page_hooks
1057 * @global array $_registered_pages
1058 * @global array $_parent_pages
1060 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1061 * @param string $menu_title The text to be used for the menu.
1062 * @param string $capability The capability required for this menu to be displayed to the user.
1063 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1064 * @param callable $function The function to be called to output the content for this page.
1065 * @param string $icon_url The URL to the icon to be used for this menu.
1066 * * Pass a base64-encoded SVG using a data URI, which will be colored to match
1067 * the color scheme. This should begin with 'data:image/svg+xml;base64,'.
1068 * * Pass the name of a Dashicons helper class to use a font icon,
1069 * e.g. 'dashicons-chart-pie'.
1070 * * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.
1071 * @param int $position The position in the menu order this one should appear.
1072 * @return string The resulting page's hook_suffix.
1074 function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) {
1075 global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
1077 $menu_slug = plugin_basename( $menu_slug );
1079 $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title );
1081 $hookname = get_plugin_page_hookname( $menu_slug, '' );
1083 if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) )
1084 add_action( $hookname, $function );
1086 if ( empty($icon_url) ) {
1087 $icon_url = 'dashicons-admin-generic';
1088 $icon_class = 'menu-icon-generic ';
1090 $icon_url = set_url_scheme( $icon_url );
1094 $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url );
1096 if ( null === $position ) {
1097 $menu[] = $new_menu;
1098 } elseif ( isset( $menu[ "$position" ] ) ) {
1099 $position = $position + substr( base_convert( md5( $menu_slug . $menu_title ), 16, 10 ) , -5 ) * 0.00001;
1100 $menu[ "$position" ] = $new_menu;
1102 $menu[ $position ] = $new_menu;
1105 $_registered_pages[$hookname] = true;
1107 // No parent as top level
1108 $_parent_pages[$menu_slug] = false;
1114 * Add a submenu page.
1116 * This function takes a capability which will be used to determine whether
1117 * or not a page is included in the menu.
1119 * The function which is hooked in to handle the output of the page must check
1120 * that the user has the required capability as well.
1122 * @global array $submenu
1123 * @global array $menu
1124 * @global array $_wp_real_parent_file
1125 * @global bool $_wp_submenu_nopriv
1126 * @global array $_registered_pages
1127 * @global array $_parent_pages
1129 * @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page).
1130 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1131 * @param string $menu_title The text to be used for the menu.
1132 * @param string $capability The capability required for this menu to be displayed to the user.
1133 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1134 * @param callable $function The function to be called to output the content for this page.
1135 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1137 function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1138 global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv,
1139 $_registered_pages, $_parent_pages;
1141 $menu_slug = plugin_basename( $menu_slug );
1142 $parent_slug = plugin_basename( $parent_slug);
1144 if ( isset( $_wp_real_parent_file[$parent_slug] ) )
1145 $parent_slug = $_wp_real_parent_file[$parent_slug];
1147 if ( !current_user_can( $capability ) ) {
1148 $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true;
1153 * If the parent doesn't already have a submenu, add a link to the parent
1154 * as the first item in the submenu. If the submenu file is the same as the
1155 * parent file someone is trying to link back to the parent manually. In
1156 * this case, don't automatically add a link back to avoid duplication.
1158 if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) {
1159 foreach ( (array)$menu as $parent_menu ) {
1160 if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) )
1161 $submenu[$parent_slug][] = array_slice( $parent_menu, 0, 4 );
1165 $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title );
1167 $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);
1168 if (!empty ( $function ) && !empty ( $hookname ))
1169 add_action( $hookname, $function );
1171 $_registered_pages[$hookname] = true;
1174 * Backward-compatibility for plugins using add_management page.
1175 * See wp-admin/admin.php for redirect from edit.php to tools.php
1177 if ( 'tools.php' == $parent_slug )
1178 $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true;
1180 // No parent as top level.
1181 $_parent_pages[$menu_slug] = $parent_slug;
1187 * Add submenu page to the Tools main menu.
1189 * This function takes a capability which will be used to determine whether
1190 * or not a page is included in the menu.
1192 * The function which is hooked in to handle the output of the page must check
1193 * that the user has the required capability as well.
1195 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1196 * @param string $menu_title The text to be used for the menu.
1197 * @param string $capability The capability required for this menu to be displayed to the user.
1198 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1199 * @param callable $function The function to be called to output the content for this page.
1200 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1202 function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1203 return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1207 * Add submenu page to the Settings main menu.
1209 * This function takes a capability which will be used to determine whether
1210 * or not a page is included in the menu.
1212 * The function which is hooked in to handle the output of the page must check
1213 * that the user has the required capability as well.
1215 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1216 * @param string $menu_title The text to be used for the menu.
1217 * @param string $capability The capability required for this menu to be displayed to the user.
1218 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1219 * @param callable $function The function to be called to output the content for this page.
1220 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1222 function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1223 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1227 * Add submenu page to the Appearance main menu.
1229 * This function takes a capability which will be used to determine whether
1230 * or not a page is included in the menu.
1232 * The function which is hooked in to handle the output of the page must check
1233 * that the user has the required capability as well.
1235 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1236 * @param string $menu_title The text to be used for the menu.
1237 * @param string $capability The capability required for this menu to be displayed to the user.
1238 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1239 * @param callable $function The function to be called to output the content for this page.
1240 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1242 function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1243 return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1247 * Add submenu page to the Plugins main menu.
1249 * This function takes a capability which will be used to determine whether
1250 * or not a page is included in the menu.
1252 * The function which is hooked in to handle the output of the page must check
1253 * that the user has the required capability as well.
1255 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1256 * @param string $menu_title The text to be used for the menu.
1257 * @param string $capability The capability required for this menu to be displayed to the user.
1258 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1259 * @param callable $function The function to be called to output the content for this page.
1260 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1262 function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1263 return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1267 * Add submenu page to the Users/Profile main menu.
1269 * This function takes a capability which will be used to determine whether
1270 * or not a page is included in the menu.
1272 * The function which is hooked in to handle the output of the page must check
1273 * that the user has the required capability as well.
1275 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1276 * @param string $menu_title The text to be used for the menu.
1277 * @param string $capability The capability required for this menu to be displayed to the user.
1278 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1279 * @param callable $function The function to be called to output the content for this page.
1280 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1282 function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1283 if ( current_user_can('edit_users') )
1284 $parent = 'users.php';
1286 $parent = 'profile.php';
1287 return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
1290 * Add submenu page to the Dashboard main menu.
1292 * This function takes a capability which will be used to determine whether
1293 * or not a page is included in the menu.
1295 * The function which is hooked in to handle the output of the page must check
1296 * that the user has the required capability as well.
1298 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1299 * @param string $menu_title The text to be used for the menu.
1300 * @param string $capability The capability required for this menu to be displayed to the user.
1301 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1302 * @param callable $function The function to be called to output the content for this page.
1303 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1305 function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1306 return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1310 * Add submenu page to the Posts main menu.
1312 * This function takes a capability which will be used to determine whether
1313 * or not a page is included in the menu.
1315 * The function which is hooked in to handle the output of the page must check
1316 * that the user has the required capability as well.
1318 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1319 * @param string $menu_title The text to be used for the menu.
1320 * @param string $capability The capability required for this menu to be displayed to the user.
1321 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1322 * @param callable $function The function to be called to output the content for this page.
1323 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1325 function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1326 return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1330 * Add submenu page to the Media main menu.
1332 * This function takes a capability which will be used to determine whether
1333 * or not a page is included in the menu.
1335 * The function which is hooked in to handle the output of the page must check
1336 * that the user has the required capability as well.
1338 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1339 * @param string $menu_title The text to be used for the menu.
1340 * @param string $capability The capability required for this menu to be displayed to the user.
1341 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1342 * @param callable $function The function to be called to output the content for this page.
1343 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1345 function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1346 return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1350 * Add submenu page to the Links main menu.
1352 * This function takes a capability which will be used to determine whether
1353 * or not a page is included in the menu.
1355 * The function which is hooked in to handle the output of the page must check
1356 * that the user has the required capability as well.
1358 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1359 * @param string $menu_title The text to be used for the menu.
1360 * @param string $capability The capability required for this menu to be displayed to the user.
1361 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1362 * @param callable $function The function to be called to output the content for this page.
1363 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1365 function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1366 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1370 * Add submenu page to the Pages main menu.
1372 * This function takes a capability which will be used to determine whether
1373 * or not a page is included in the menu.
1375 * The function which is hooked in to handle the output of the page must check
1376 * that the user has the required capability as well.
1378 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1379 * @param string $menu_title The text to be used for the menu.
1380 * @param string $capability The capability required for this menu to be displayed to the user.
1381 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1382 * @param callable $function The function to be called to output the content for this page.
1383 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1385 function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1386 return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function );
1390 * Add submenu page to the Comments main menu.
1392 * This function takes a capability which will be used to determine whether
1393 * or not a page is included in the menu.
1395 * The function which is hooked in to handle the output of the page must check
1396 * that the user has the required capability as well.
1398 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1399 * @param string $menu_title The text to be used for the menu.
1400 * @param string $capability The capability required for this menu to be displayed to the user.
1401 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1402 * @param callable $function The function to be called to output the content for this page.
1403 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1405 function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1406 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1410 * Remove a top-level admin menu.
1414 * @global array $menu
1416 * @param string $menu_slug The slug of the menu.
1417 * @return array|bool The removed menu on success, false if not found.
1419 function remove_menu_page( $menu_slug ) {
1422 foreach ( $menu as $i => $item ) {
1423 if ( $menu_slug == $item[2] ) {
1433 * Remove an admin submenu.
1437 * @global array $submenu
1439 * @param string $menu_slug The slug for the parent menu.
1440 * @param string $submenu_slug The slug of the submenu.
1441 * @return array|bool The removed submenu on success, false if not found.
1443 function remove_submenu_page( $menu_slug, $submenu_slug ) {
1446 if ( !isset( $submenu[$menu_slug] ) )
1449 foreach ( $submenu[$menu_slug] as $i => $item ) {
1450 if ( $submenu_slug == $item[2] ) {
1451 unset( $submenu[$menu_slug][$i] );
1460 * Get the url to access a particular menu page based on the slug it was registered with.
1462 * If the slug hasn't been registered properly no url will be returned
1466 * @global array $_parent_pages
1468 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1469 * @param bool $echo Whether or not to echo the url - default is true
1470 * @return string the url
1472 function menu_page_url($menu_slug, $echo = true) {
1473 global $_parent_pages;
1475 if ( isset( $_parent_pages[$menu_slug] ) ) {
1476 $parent_slug = $_parent_pages[$menu_slug];
1477 if ( $parent_slug && ! isset( $_parent_pages[$parent_slug] ) ) {
1478 $url = admin_url( add_query_arg( 'page', $menu_slug, $parent_slug ) );
1480 $url = admin_url( 'admin.php?page=' . $menu_slug );
1486 $url = esc_url($url);
1495 // Pluggable Menu Support -- Private
1499 * @global string $parent_file
1500 * @global array $menu
1501 * @global array $submenu
1502 * @global string $pagenow
1503 * @global string $typenow
1504 * @global string $plugin_page
1505 * @global array $_wp_real_parent_file
1506 * @global array $_wp_menu_nopriv
1507 * @global array $_wp_submenu_nopriv
1509 function get_admin_page_parent( $parent = '' ) {
1510 global $parent_file, $menu, $submenu, $pagenow, $typenow,
1511 $plugin_page, $_wp_real_parent_file, $_wp_menu_nopriv, $_wp_submenu_nopriv;
1513 if ( !empty ( $parent ) && 'admin.php' != $parent ) {
1514 if ( isset( $_wp_real_parent_file[$parent] ) )
1515 $parent = $_wp_real_parent_file[$parent];
1519 if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
1520 foreach ( (array)$menu as $parent_menu ) {
1521 if ( $parent_menu[2] == $plugin_page ) {
1522 $parent_file = $plugin_page;
1523 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1524 $parent_file = $_wp_real_parent_file[$parent_file];
1525 return $parent_file;
1528 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
1529 $parent_file = $plugin_page;
1530 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1531 $parent_file = $_wp_real_parent_file[$parent_file];
1532 return $parent_file;
1536 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
1537 $parent_file = $pagenow;
1538 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1539 $parent_file = $_wp_real_parent_file[$parent_file];
1540 return $parent_file;
1543 foreach (array_keys( (array)$submenu ) as $parent) {
1544 foreach ( $submenu[$parent] as $submenu_array ) {
1545 if ( isset( $_wp_real_parent_file[$parent] ) )
1546 $parent = $_wp_real_parent_file[$parent];
1547 if ( !empty($typenow) && ($submenu_array[2] == "$pagenow?post_type=$typenow") ) {
1548 $parent_file = $parent;
1550 } elseif ( $submenu_array[2] == $pagenow && empty($typenow) && ( empty($parent_file) || false === strpos($parent_file, '?') ) ) {
1551 $parent_file = $parent;
1553 } elseif ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
1554 $parent_file = $parent;
1560 if ( empty($parent_file) )
1567 * @global string $title
1568 * @global array $menu
1569 * @global array $submenu
1570 * @global string $pagenow
1571 * @global string $plugin_page
1572 * @global string $typenow
1574 function get_admin_page_title() {
1575 global $title, $menu, $submenu, $pagenow, $plugin_page, $typenow;
1577 if ( ! empty ( $title ) )
1580 $hook = get_plugin_page_hook( $plugin_page, $pagenow );
1582 $parent = $parent1 = get_admin_page_parent();
1584 if ( empty ( $parent) ) {
1585 foreach ( (array)$menu as $menu_array ) {
1586 if ( isset( $menu_array[3] ) ) {
1587 if ( $menu_array[2] == $pagenow ) {
1588 $title = $menu_array[3];
1589 return $menu_array[3];
1590 } elseif ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
1591 $title = $menu_array[3];
1592 return $menu_array[3];
1595 $title = $menu_array[0];
1600 foreach ( array_keys( $submenu ) as $parent ) {
1601 foreach ( $submenu[$parent] as $submenu_array ) {
1602 if ( isset( $plugin_page ) &&
1603 ( $plugin_page == $submenu_array[2] ) &&
1605 ( $parent == $pagenow ) ||
1606 ( $parent == $plugin_page ) ||
1607 ( $plugin_page == $hook ) ||
1608 ( $pagenow == 'admin.php' && $parent1 != $submenu_array[2] ) ||
1609 ( !empty($typenow) && $parent == $pagenow . '?post_type=' . $typenow)
1612 $title = $submenu_array[3];
1613 return $submenu_array[3];
1616 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
1619 if ( isset( $submenu_array[3] ) ) {
1620 $title = $submenu_array[3];
1621 return $submenu_array[3];
1623 $title = $submenu_array[0];
1628 if ( empty ( $title ) ) {
1629 foreach ( $menu as $menu_array ) {
1630 if ( isset( $plugin_page ) &&
1631 ( $plugin_page == $menu_array[2] ) &&
1632 ( $pagenow == 'admin.php' ) &&
1633 ( $parent1 == $menu_array[2] ) )
1635 $title = $menu_array[3];
1636 return $menu_array[3];
1648 * @param string $plugin_page
1649 * @param string $parent_page
1650 * @return string|null
1652 function get_plugin_page_hook( $plugin_page, $parent_page ) {
1653 $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
1654 if ( has_action($hook) )
1662 * @global array $admin_page_hooks
1663 * @param string $plugin_page
1664 * @param string $parent_page
1666 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
1667 global $admin_page_hooks;
1669 $parent = get_admin_page_parent( $parent_page );
1671 $page_type = 'admin';
1672 if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
1673 if ( isset( $admin_page_hooks[$plugin_page] ) ) {
1674 $page_type = 'toplevel';
1675 } elseif ( isset( $admin_page_hooks[$parent] )) {
1676 $page_type = $admin_page_hooks[$parent];
1678 } elseif ( isset( $admin_page_hooks[$parent] ) ) {
1679 $page_type = $admin_page_hooks[$parent];
1682 $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
1684 return $page_type . '_page_' . $plugin_name;
1689 * @global string $pagenow
1690 * @global array $menu
1691 * @global array $submenu
1692 * @global array $_wp_menu_nopriv
1693 * @global array $_wp_submenu_nopriv
1694 * @global string $plugin_page
1695 * @global array $_registered_pages
1697 function user_can_access_admin_page() {
1698 global $pagenow, $menu, $submenu, $_wp_menu_nopriv, $_wp_submenu_nopriv,
1699 $plugin_page, $_registered_pages;
1701 $parent = get_admin_page_parent();
1703 if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
1706 if ( isset( $plugin_page ) ) {
1707 if ( isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
1710 $hookname = get_plugin_page_hookname($plugin_page, $parent);
1712 if ( !isset($_registered_pages[$hookname]) )
1716 if ( empty( $parent) ) {
1717 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
1719 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
1721 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
1723 if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1725 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
1726 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
1728 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
1734 if ( isset( $plugin_page ) && ( $plugin_page == $parent ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1737 if ( isset( $submenu[$parent] ) ) {
1738 foreach ( $submenu[$parent] as $submenu_array ) {
1739 if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
1740 if ( current_user_can( $submenu_array[1] ))
1744 } elseif ( $submenu_array[2] == $pagenow ) {
1745 if ( current_user_can( $submenu_array[1] ))
1753 foreach ( $menu as $menu_array ) {
1754 if ( $menu_array[2] == $parent) {
1755 if ( current_user_can( $menu_array[1] ))
1765 /* Whitelist functions */
1768 * Refreshes the value of the options whitelist available via the 'whitelist_options' hook.
1770 * See the {@see 'whitelist_options'} filter.
1774 * @global array $new_whitelist_options
1776 * @param array $options
1779 function option_update_filter( $options ) {
1780 global $new_whitelist_options;
1782 if ( is_array( $new_whitelist_options ) )
1783 $options = add_option_whitelist( $new_whitelist_options, $options );
1789 * Adds an array of options to the options whitelist.
1793 * @global array $whitelist_options
1795 * @param array $new_options
1796 * @param string|array $options
1799 function add_option_whitelist( $new_options, $options = '' ) {
1800 if ( $options == '' )
1801 global $whitelist_options;
1803 $whitelist_options = $options;
1805 foreach ( $new_options as $page => $keys ) {
1806 foreach ( $keys as $key ) {
1807 if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
1808 $whitelist_options[ $page ] = array();
1809 $whitelist_options[ $page ][] = $key;
1811 $pos = array_search( $key, $whitelist_options[ $page ] );
1812 if ( $pos === false )
1813 $whitelist_options[ $page ][] = $key;
1818 return $whitelist_options;
1822 * Removes a list of options from the options whitelist.
1826 * @global array $whitelist_options
1828 * @param array $del_options
1829 * @param string|array $options
1832 function remove_option_whitelist( $del_options, $options = '' ) {
1833 if ( $options == '' )
1834 global $whitelist_options;
1836 $whitelist_options = $options;
1838 foreach ( $del_options as $page => $keys ) {
1839 foreach ( $keys as $key ) {
1840 if ( isset($whitelist_options[ $page ]) && is_array($whitelist_options[ $page ]) ) {
1841 $pos = array_search( $key, $whitelist_options[ $page ] );
1842 if ( $pos !== false )
1843 unset( $whitelist_options[ $page ][ $pos ] );
1848 return $whitelist_options;
1852 * Output nonce, action, and option_page fields for a settings page.
1856 * @param string $option_group A settings group name. This should match the group name used in register_setting().
1858 function settings_fields($option_group) {
1859 echo "<input type='hidden' name='option_page' value='" . esc_attr($option_group) . "' />";
1860 echo '<input type="hidden" name="action" value="update" />';
1861 wp_nonce_field("$option_group-options");
1865 * Clears the Plugins cache used by get_plugins() and by default, the Plugin Update cache.
1869 * @param bool $clear_update_cache Whether to clear the Plugin updates cache
1871 function wp_clean_plugins_cache( $clear_update_cache = true ) {
1872 if ( $clear_update_cache )
1873 delete_site_transient( 'update_plugins' );
1874 wp_cache_delete( 'plugins', 'plugins' );
1878 * @param string $plugin
1880 function plugin_sandbox_scrape( $plugin ) {
1881 wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
1882 include( WP_PLUGIN_DIR . '/' . $plugin );