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', 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 ( $markup || $translate ) {
96 $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
98 $plugin_data['Title'] = $plugin_data['Name'];
99 $plugin_data['AuthorName'] = $plugin_data['Author'];
106 * Sanitizes plugin data, optionally adds markup, optionally translates.
110 * @see get_plugin_data()
112 function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) {
114 // Sanitize the plugin filename to a WP_PLUGIN_DIR relative path
115 $plugin_file = plugin_basename( $plugin_file );
119 if ( $textdomain = $plugin_data['TextDomain'] ) {
120 if ( ! is_textdomain_loaded( $textdomain ) ) {
121 if ( $plugin_data['DomainPath'] ) {
122 load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] );
124 load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) );
127 } elseif ( 'hello.php' == basename( $plugin_file ) ) {
128 $textdomain = 'default';
131 foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field )
132 $plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain );
137 $allowed_tags = $allowed_tags_in_links = array(
138 'abbr' => array( 'title' => true ),
139 'acronym' => array( 'title' => true ),
144 $allowed_tags['a'] = array( 'href' => true, 'title' => true );
146 // Name is marked up inside <a> tags. Don't allow these.
147 // Author is too, but some plugins have used <a> here (omitting Author URI).
148 $plugin_data['Name'] = wp_kses( $plugin_data['Name'], $allowed_tags_in_links );
149 $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $allowed_tags );
151 $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags );
152 $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $allowed_tags );
154 $plugin_data['PluginURI'] = esc_url( $plugin_data['PluginURI'] );
155 $plugin_data['AuthorURI'] = esc_url( $plugin_data['AuthorURI'] );
157 $plugin_data['Title'] = $plugin_data['Name'];
158 $plugin_data['AuthorName'] = $plugin_data['Author'];
162 if ( $plugin_data['PluginURI'] && $plugin_data['Name'] )
163 $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '">' . $plugin_data['Name'] . '</a>';
165 if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] )
166 $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '">' . $plugin_data['Author'] . '</a>';
168 $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
170 if ( $plugin_data['Author'] )
171 $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s.'), $plugin_data['Author'] ) . '</cite>';
178 * Get a list of a plugin's files.
182 * @param string $plugin Plugin ID
183 * @return array List of files relative to the plugin root.
185 function get_plugin_files($plugin) {
186 $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
187 $dir = dirname($plugin_file);
188 $plugin_files = array($plugin);
189 if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) {
190 $plugins_dir = @ opendir( $dir );
191 if ( $plugins_dir ) {
192 while (($file = readdir( $plugins_dir ) ) !== false ) {
193 if ( substr($file, 0, 1) == '.' )
195 if ( is_dir( $dir . '/' . $file ) ) {
196 $plugins_subdir = @ opendir( $dir . '/' . $file );
197 if ( $plugins_subdir ) {
198 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
199 if ( substr($subfile, 0, 1) == '.' )
201 $plugin_files[] = plugin_basename("$dir/$file/$subfile");
203 @closedir( $plugins_subdir );
206 if ( plugin_basename("$dir/$file") != $plugin )
207 $plugin_files[] = plugin_basename("$dir/$file");
210 @closedir( $plugins_dir );
214 return $plugin_files;
218 * Check the plugins directory and retrieve all plugin files with plugin data.
220 * WordPress only supports plugin files in the base plugins directory
221 * (wp-content/plugins) and in one directory above the plugins directory
222 * (wp-content/plugins/my-plugin). The file it looks for has the plugin data
223 * and must be found in those two locations. It is recommended to keep your
224 * plugin files in their own directories.
226 * The file with the plugin data is the file that will be included and therefore
227 * needs to have the main execution for the plugin. This does not mean
228 * everything must be contained in the file and it is recommended that the file
229 * be split for maintainability. Keep everything in one file for extreme
230 * optimization purposes.
234 * @param string $plugin_folder Optional. Relative path to single plugin folder.
235 * @return array Key is the plugin file path and the value is an array of the plugin data.
237 function get_plugins($plugin_folder = '') {
239 if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
240 $cache_plugins = array();
242 if ( isset($cache_plugins[ $plugin_folder ]) )
243 return $cache_plugins[ $plugin_folder ];
245 $wp_plugins = array ();
246 $plugin_root = WP_PLUGIN_DIR;
247 if ( !empty($plugin_folder) )
248 $plugin_root .= $plugin_folder;
250 // Files in wp-content/plugins directory
251 $plugins_dir = @ opendir( $plugin_root);
252 $plugin_files = array();
253 if ( $plugins_dir ) {
254 while (($file = readdir( $plugins_dir ) ) !== false ) {
255 if ( substr($file, 0, 1) == '.' )
257 if ( is_dir( $plugin_root.'/'.$file ) ) {
258 $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
259 if ( $plugins_subdir ) {
260 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
261 if ( substr($subfile, 0, 1) == '.' )
263 if ( substr($subfile, -4) == '.php' )
264 $plugin_files[] = "$file/$subfile";
266 closedir( $plugins_subdir );
269 if ( substr($file, -4) == '.php' )
270 $plugin_files[] = $file;
273 closedir( $plugins_dir );
276 if ( empty($plugin_files) )
279 foreach ( $plugin_files as $plugin_file ) {
280 if ( !is_readable( "$plugin_root/$plugin_file" ) )
283 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
285 if ( empty ( $plugin_data['Name'] ) )
288 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
291 uasort( $wp_plugins, '_sort_uname_callback' );
293 $cache_plugins[ $plugin_folder ] = $wp_plugins;
294 wp_cache_set('plugins', $cache_plugins, 'plugins');
300 * Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data.
302 * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins).
305 * @return array Key is the mu-plugin file path and the value is an array of the mu-plugin data.
307 function get_mu_plugins() {
308 $wp_plugins = array();
309 // Files in wp-content/mu-plugins directory
310 $plugin_files = array();
312 if ( ! is_dir( WPMU_PLUGIN_DIR ) )
314 if ( $plugins_dir = @ opendir( WPMU_PLUGIN_DIR ) ) {
315 while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
316 if ( substr( $file, -4 ) == '.php' )
317 $plugin_files[] = $file;
323 @closedir( $plugins_dir );
325 if ( empty($plugin_files) )
328 foreach ( $plugin_files as $plugin_file ) {
329 if ( !is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) )
332 $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
334 if ( empty ( $plugin_data['Name'] ) )
335 $plugin_data['Name'] = $plugin_file;
337 $wp_plugins[ $plugin_file ] = $plugin_data;
340 if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php') <= 30 ) // silence is golden
341 unset( $wp_plugins['index.php'] );
343 uasort( $wp_plugins, '_sort_uname_callback' );
349 * Callback to sort array by a 'Name' key.
354 function _sort_uname_callback( $a, $b ) {
355 return strnatcasecmp( $a['Name'], $b['Name'] );
359 * Check the wp-content directory and retrieve all drop-ins with any plugin data.
362 * @return array Key is the file path and the value is an array of the plugin data.
364 function get_dropins() {
366 $plugin_files = array();
368 $_dropins = _get_dropins();
370 // These exist in the wp-content directory
371 if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) {
372 while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
373 if ( isset( $_dropins[ $file ] ) )
374 $plugin_files[] = $file;
380 @closedir( $plugins_dir );
382 if ( empty($plugin_files) )
385 foreach ( $plugin_files as $plugin_file ) {
386 if ( !is_readable( WP_CONTENT_DIR . "/$plugin_file" ) )
388 $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
389 if ( empty( $plugin_data['Name'] ) )
390 $plugin_data['Name'] = $plugin_file;
391 $dropins[ $plugin_file ] = $plugin_data;
394 uksort( $dropins, 'strnatcasecmp' );
400 * Returns drop-ins that WordPress uses.
402 * Includes Multisite drop-ins only when is_multisite()
405 * @return array Key is file name. The value is an array, with the first value the
406 * purpose of the drop-in and the second value the name of the constant that must be
407 * true for the drop-in to be used, or true if no constant is required.
409 function _get_dropins() {
411 'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE
412 'db.php' => array( __( 'Custom database class.' ), true ), // auto on load
413 'db-error.php' => array( __( 'Custom database error message.' ), true ), // auto on error
414 'install.php' => array( __( 'Custom install script.' ), true ), // auto on install
415 'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // auto on maintenance
416 'object-cache.php' => array( __( 'External object cache.' ), true ), // auto on load
419 if ( is_multisite() ) {
420 $dropins['sunrise.php' ] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE
421 $dropins['blog-deleted.php' ] = array( __( 'Custom site deleted message.' ), true ); // auto on deleted blog
422 $dropins['blog-inactive.php' ] = array( __( 'Custom site inactive message.' ), true ); // auto on inactive blog
423 $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // auto on archived or spammed blog
430 * Check whether a plugin is active.
432 * Only plugins installed in the plugins/ folder can be active.
434 * Plugins in the mu-plugins/ folder can't be "activated," so this function will
435 * return false for those plugins.
439 * @param string $plugin Base plugin path from plugins directory.
440 * @return bool True, if in the active plugins list. False, not in the list.
442 function is_plugin_active( $plugin ) {
443 return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
447 * Check whether the plugin is inactive.
449 * Reverse of is_plugin_active(). Used as a callback.
452 * @see is_plugin_active()
454 * @param string $plugin Base plugin path from plugins directory.
455 * @return bool True if inactive. False if active.
457 function is_plugin_inactive( $plugin ) {
458 return ! is_plugin_active( $plugin );
462 * Check whether the plugin is active for the entire network.
464 * Only plugins installed in the plugins/ folder can be active.
466 * Plugins in the mu-plugins/ folder can't be "activated," so this function will
467 * return false for those plugins.
471 * @param string $plugin Base plugin path from plugins directory.
472 * @return bool True, if active for the network, otherwise false.
474 function is_plugin_active_for_network( $plugin ) {
475 if ( !is_multisite() )
478 $plugins = get_site_option( 'active_sitewide_plugins');
479 if ( isset($plugins[$plugin]) )
486 * Checks for "Network: true" in the plugin header to see if this should
487 * be activated only as a network wide plugin. The plugin would also work
488 * when Multisite is not enabled.
490 * Checks for "Site Wide Only: true" for backwards compatibility.
494 * @param string $plugin Plugin to check
495 * @return bool True if plugin is network only, false otherwise.
497 function is_network_only_plugin( $plugin ) {
498 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
500 return $plugin_data['Network'];
505 * Attempts activation of plugin in a "sandbox" and redirects on success.
507 * A plugin that is already activated will not attempt to be activated again.
509 * The way it works is by setting the redirection to the error before trying to
510 * include the plugin file. If the plugin fails, then the redirection will not
511 * be overwritten with the success message. Also, the options will not be
512 * updated and the activation hook will not be called on plugin error.
514 * It should be noted that in no way the below code will actually prevent errors
515 * within the file. The code should not be used elsewhere to replicate the
516 * "sandbox", which uses redirection to work.
519 * If any errors are found or text is outputted, then it will be captured to
520 * ensure that the success redirection will update the error redirection.
524 * @param string $plugin Plugin path to main plugin file with plugin data.
525 * @param string $redirect Optional. URL to redirect to.
526 * @param bool $network_wide Optional. Whether to enable the plugin for all sites in the network
527 * or just the current site. Multisite only. Default false.
528 * @param bool $silent Optional. Whether to prevent calling activation hooks. Default false.
529 * @return WP_Error|null WP_Error on invalid file or null on success.
531 function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
532 $plugin = plugin_basename( trim( $plugin ) );
534 if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) {
535 $network_wide = true;
536 $current = get_site_option( 'active_sitewide_plugins', array() );
537 $_GET['networkwide'] = 1; // Back compat for plugins looking for this value.
539 $current = get_option( 'active_plugins', array() );
542 $valid = validate_plugin($plugin);
543 if ( is_wp_error($valid) )
546 if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current ) ) ) {
547 if ( !empty($redirect) )
548 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
550 wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
551 $_wp_plugin_file = $plugin;
552 include_once( WP_PLUGIN_DIR . '/' . $plugin );
553 $plugin = $_wp_plugin_file; // Avoid stomping of the $plugin variable in a plugin.
557 * Fires before a plugin is activated.
559 * If a plugin is silently activated (such as during an update),
560 * this hook does not fire.
564 * @param string $plugin Plugin path to main plugin file with plugin data.
565 * @param bool $network_wide Whether to enable the plugin for all sites in the network
566 * or just the current site. Multisite only. Default is false.
568 do_action( 'activate_plugin', $plugin, $network_wide );
571 * Fires as a specific plugin is being activated.
573 * This hook is the "activation" hook used internally by
574 * {@see register_activation_hook()}. The dynamic portion of the
575 * hook name, `$plugin`, refers to the plugin basename.
577 * If a plugin is silently activated (such as during an update),
578 * this hook does not fire.
582 * @param bool $network_wide Whether to enable the plugin for all sites in the network
583 * or just the current site. Multisite only. Default is false.
585 do_action( 'activate_' . $plugin, $network_wide );
588 if ( $network_wide ) {
589 $current = get_site_option( 'active_sitewide_plugins', array() );
590 $current[$plugin] = time();
591 update_site_option( 'active_sitewide_plugins', $current );
593 $current = get_option( 'active_plugins', array() );
594 $current[] = $plugin;
596 update_option('active_plugins', $current);
601 * Fires after a plugin has been activated.
603 * If a plugin is silently activated (such as during an update),
604 * this hook does not fire.
608 * @param string $plugin Plugin path to main plugin file with plugin data.
609 * @param bool $network_wide Whether to enable the plugin for all sites in the network
610 * or just the current site. Multisite only. Default is false.
612 do_action( 'activated_plugin', $plugin, $network_wide );
615 if ( ob_get_length() > 0 ) {
616 $output = ob_get_clean();
617 return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
626 * Deactivate a single plugin or multiple plugins.
628 * The deactivation hook is disabled by the plugin upgrader by using the $silent
633 * @param string|array $plugins Single plugin or list of plugins to deactivate.
634 * @param bool $silent Prevent calling deactivation hooks. Default is false.
635 * @param mixed $network_wide Whether to deactivate the plugin for all sites in the network.
636 * A value of null (the default) will deactivate plugins for both the site and the network.
638 function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) {
639 if ( is_multisite() )
640 $network_current = get_site_option( 'active_sitewide_plugins', array() );
641 $current = get_option( 'active_plugins', array() );
642 $do_blog = $do_network = false;
644 foreach ( (array) $plugins as $plugin ) {
645 $plugin = plugin_basename( trim( $plugin ) );
646 if ( ! is_plugin_active($plugin) )
649 $network_deactivating = false !== $network_wide && is_plugin_active_for_network( $plugin );
653 * Fires before a plugin is deactivated.
655 * If a plugin is silently deactivated (such as during an update),
656 * this hook does not fire.
660 * @param string $plugin Plugin path to main plugin file with plugin data.
661 * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
662 * or just the current site. Multisite only. Default is false.
664 do_action( 'deactivate_plugin', $plugin, $network_deactivating );
667 if ( false !== $network_wide ) {
668 if ( is_plugin_active_for_network( $plugin ) ) {
670 unset( $network_current[ $plugin ] );
671 } elseif ( $network_wide ) {
676 if ( true !== $network_wide ) {
677 $key = array_search( $plugin, $current );
678 if ( false !== $key ) {
680 unset( $current[ $key ] );
686 * Fires as a specific plugin is being deactivated.
688 * This hook is the "deactivation" hook used internally by
689 * {@see register_deactivation_hook()}. The dynamic portion of the
690 * hook name, `$plugin`, refers to the plugin basename.
692 * If a plugin is silently deactivated (such as during an update),
693 * this hook does not fire.
697 * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
698 * or just the current site. Multisite only. Default is false.
700 do_action( 'deactivate_' . $plugin, $network_deactivating );
703 * Fires after a plugin is deactivated.
705 * If a plugin is silently deactivated (such as during an update),
706 * this hook does not fire.
710 * @param string $plugin Plugin basename.
711 * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
712 * or just the current site. Multisite only. Default false.
714 do_action( 'deactivated_plugin', $plugin, $network_deactivating );
719 update_option('active_plugins', $current);
721 update_site_option( 'active_sitewide_plugins', $network_current );
725 * Activate multiple plugins.
727 * When WP_Error is returned, it does not mean that one of the plugins had
728 * errors. It means that one or more of the plugins file path was invalid.
730 * The execution will be halted as soon as one of the plugins has an error.
734 * @param string|array $plugins Single plugin or list of plugins to activate.
735 * @param string $redirect Redirect to page after successful activation.
736 * @param bool $network_wide Whether to enable the plugin for all sites in the network.
737 * @param bool $silent Prevent calling activation hooks. Default is false.
738 * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
740 function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
741 if ( !is_array($plugins) )
742 $plugins = array($plugins);
745 foreach ( $plugins as $plugin ) {
746 if ( !empty($redirect) )
747 $redirect = add_query_arg('plugin', $plugin, $redirect);
748 $result = activate_plugin($plugin, $redirect, $network_wide, $silent);
749 if ( is_wp_error($result) )
750 $errors[$plugin] = $result;
753 if ( !empty($errors) )
754 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
760 * Remove directory and files of a plugin for a list of plugins.
764 * @global WP_Filesystem_Base $wp_filesystem
766 * @param array $plugins List of plugins to delete.
767 * @param string $deprecated Deprecated.
768 * @return bool|null|WP_Error True on success, false is $plugins is empty, WP_Error on failure.
769 * Null if filesystem credentials are required to proceed.
771 function delete_plugins( $plugins, $deprecated = '' ) {
772 global $wp_filesystem;
774 if ( empty($plugins) )
778 foreach ( $plugins as $plugin )
779 $checked[] = 'checked[]=' . $plugin;
782 $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-plugins');
783 if ( false === ($credentials = request_filesystem_credentials($url)) ) {
784 $data = ob_get_clean();
786 if ( ! empty($data) ){
787 include_once( ABSPATH . 'wp-admin/admin-header.php');
789 include( ABSPATH . 'wp-admin/admin-footer.php');
795 if ( ! WP_Filesystem($credentials) ) {
796 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
797 $data = ob_get_clean();
799 if ( ! empty($data) ){
800 include_once( ABSPATH . 'wp-admin/admin-header.php');
802 include( ABSPATH . 'wp-admin/admin-footer.php');
808 if ( ! is_object($wp_filesystem) )
809 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
811 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
812 return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
814 // Get the base plugin folder.
815 $plugins_dir = $wp_filesystem->wp_plugins_dir();
816 if ( empty( $plugins_dir ) ) {
817 return new WP_Error( 'fs_no_plugins_dir', __( 'Unable to locate WordPress Plugin directory.' ) );
820 $plugins_dir = trailingslashit( $plugins_dir );
822 $plugin_translations = wp_get_installed_translations( 'plugins' );
826 foreach ( $plugins as $plugin_file ) {
827 // Run Uninstall hook.
828 if ( is_uninstallable_plugin( $plugin_file ) ) {
829 uninstall_plugin($plugin_file);
833 * Fires immediately before a plugin deletion attempt.
837 * @param string $plugin_file Plugin file name.
839 do_action( 'delete_plugin', $plugin_file );
841 $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin_file ) );
843 // If plugin is in its own directory, recursively delete the directory.
844 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
845 $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
847 $deleted = $wp_filesystem->delete( $plugins_dir . $plugin_file );
851 * Fires immediately after a plugin deletion attempt.
855 * @param string $plugin_file Plugin file name.
856 * @param bool $deleted Whether the plugin deletion was successful.
858 do_action( 'deleted_plugin', $plugin_file, $deleted );
861 $errors[] = $plugin_file;
865 // Remove language files, silently.
866 $plugin_slug = dirname( $plugin_file );
867 if ( '.' !== $plugin_slug && ! empty( $plugin_translations[ $plugin_slug ] ) ) {
868 $translations = $plugin_translations[ $plugin_slug ];
870 foreach ( $translations as $translation => $data ) {
871 $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.po' );
872 $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.mo' );
877 // Remove deleted plugins from the plugin updates list.
878 if ( $current = get_site_transient('update_plugins') ) {
879 // Don't remove the plugins that weren't deleted.
880 $deleted = array_diff( $plugins, $errors );
882 foreach ( $deleted as $plugin_file ) {
883 unset( $current->response[ $plugin_file ] );
886 set_site_transient( 'update_plugins', $current );
889 if ( ! empty($errors) )
890 return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s.'), implode(', ', $errors)) );
896 * Validate active plugins
898 * Validate all active plugins, deactivates invalid and
899 * returns an array of deactivated ones.
902 * @return array invalid plugins, plugin as key, error as value
904 function validate_active_plugins() {
905 $plugins = get_option( 'active_plugins', array() );
906 // Validate vartype: array.
907 if ( ! is_array( $plugins ) ) {
908 update_option( 'active_plugins', array() );
912 if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
913 $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
914 $plugins = array_merge( $plugins, array_keys( $network_plugins ) );
917 if ( empty( $plugins ) )
922 // Invalid plugins get deactivated.
923 foreach ( $plugins as $plugin ) {
924 $result = validate_plugin( $plugin );
925 if ( is_wp_error( $result ) ) {
926 $invalid[$plugin] = $result;
927 deactivate_plugins( $plugin, true );
934 * Validate the plugin path.
936 * Checks that the file exists and {@link validate_file() is valid file}.
940 * @param string $plugin Plugin Path
941 * @return WP_Error|int 0 on success, WP_Error on failure.
943 function validate_plugin($plugin) {
944 if ( validate_file($plugin) )
945 return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
946 if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
947 return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
949 $installed_plugins = get_plugins();
950 if ( ! isset($installed_plugins[$plugin]) )
951 return new WP_Error('no_plugin_header', __('The plugin does not have a valid header.'));
956 * Whether the plugin can be uninstalled.
960 * @param string $plugin Plugin path to check.
961 * @return bool Whether plugin can be uninstalled.
963 function is_uninstallable_plugin($plugin) {
964 $file = plugin_basename($plugin);
966 $uninstallable_plugins = (array) get_option('uninstall_plugins');
967 if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
974 * Uninstall a single plugin.
976 * Calls the uninstall hook, if it is available.
980 * @param string $plugin Relative plugin path from Plugin Directory.
981 * @return true True if a plugin's uninstall.php file has been found and included.
983 function uninstall_plugin($plugin) {
984 $file = plugin_basename($plugin);
986 $uninstallable_plugins = (array) get_option('uninstall_plugins');
989 * Fires in uninstall_plugin() immediately before the plugin is uninstalled.
993 * @param string $plugin Relative plugin path from plugin directory.
994 * @param array $uninstallable_plugins Uninstallable plugins.
996 do_action( 'pre_uninstall_plugin', $plugin, $uninstallable_plugins );
998 if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
999 if ( isset( $uninstallable_plugins[$file] ) ) {
1000 unset($uninstallable_plugins[$file]);
1001 update_option('uninstall_plugins', $uninstallable_plugins);
1003 unset($uninstallable_plugins);
1005 define('WP_UNINSTALL_PLUGIN', $file);
1006 wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . dirname( $file ) );
1007 include( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' );
1012 if ( isset( $uninstallable_plugins[$file] ) ) {
1013 $callable = $uninstallable_plugins[$file];
1014 unset($uninstallable_plugins[$file]);
1015 update_option('uninstall_plugins', $uninstallable_plugins);
1016 unset($uninstallable_plugins);
1018 wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
1019 include( WP_PLUGIN_DIR . '/' . $file );
1021 add_action( 'uninstall_' . $file, $callable );
1024 * Fires in uninstall_plugin() once the plugin has been uninstalled.
1026 * The action concatenates the 'uninstall_' prefix with the basename of the
1027 * plugin passed to {@see uninstall_plugin()} to create a dynamically-named action.
1031 do_action( 'uninstall_' . $file );
1040 * Add a top-level menu page.
1042 * This function takes a capability which will be used to determine whether
1043 * or not a page is included in the menu.
1045 * The function which is hooked in to handle the output of the page must check
1046 * that the user has the required capability as well.
1048 * @global array $menu
1049 * @global array $admin_page_hooks
1050 * @global array $_registered_pages
1051 * @global array $_parent_pages
1053 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1054 * @param string $menu_title The text to be used for the menu.
1055 * @param string $capability The capability required for this menu to be displayed to the user.
1056 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1057 * @param callable $function The function to be called to output the content for this page.
1058 * @param string $icon_url The URL to the icon to be used for this menu.
1059 * * Pass a base64-encoded SVG using a data URI, which will be colored to match
1060 * the color scheme. This should begin with 'data:image/svg+xml;base64,'.
1061 * * Pass the name of a Dashicons helper class to use a font icon,
1062 * e.g. 'dashicons-chart-pie'.
1063 * * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.
1064 * @param int $position The position in the menu order this one should appear.
1065 * @return string The resulting page's hook_suffix.
1067 function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) {
1068 global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
1070 $menu_slug = plugin_basename( $menu_slug );
1072 $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title );
1074 $hookname = get_plugin_page_hookname( $menu_slug, '' );
1076 if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) )
1077 add_action( $hookname, $function );
1079 if ( empty($icon_url) ) {
1080 $icon_url = 'dashicons-admin-generic';
1081 $icon_class = 'menu-icon-generic ';
1083 $icon_url = set_url_scheme( $icon_url );
1087 $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url );
1089 if ( null === $position ) {
1090 $menu[] = $new_menu;
1091 } elseif ( isset( $menu[ "$position" ] ) ) {
1092 $position = $position + substr( base_convert( md5( $menu_slug . $menu_title ), 16, 10 ) , -5 ) * 0.00001;
1093 $menu[ "$position" ] = $new_menu;
1095 $menu[ $position ] = $new_menu;
1098 $_registered_pages[$hookname] = true;
1100 // No parent as top level
1101 $_parent_pages[$menu_slug] = false;
1107 * Add a submenu page.
1109 * This function takes a capability which will be used to determine whether
1110 * or not a page is included in the menu.
1112 * The function which is hooked in to handle the output of the page must check
1113 * that the user has the required capability as well.
1115 * @global array $submenu
1116 * @global array $menu
1117 * @global array $_wp_real_parent_file
1118 * @global bool $_wp_submenu_nopriv
1119 * @global array $_registered_pages
1120 * @global array $_parent_pages
1122 * @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page).
1123 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1124 * @param string $menu_title The text to be used for the menu.
1125 * @param string $capability The capability required for this menu to be displayed to the user.
1126 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1127 * @param callable $function The function to be called to output the content for this page.
1128 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1130 function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1131 global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv,
1132 $_registered_pages, $_parent_pages;
1134 $menu_slug = plugin_basename( $menu_slug );
1135 $parent_slug = plugin_basename( $parent_slug);
1137 if ( isset( $_wp_real_parent_file[$parent_slug] ) )
1138 $parent_slug = $_wp_real_parent_file[$parent_slug];
1140 if ( !current_user_can( $capability ) ) {
1141 $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true;
1146 * If the parent doesn't already have a submenu, add a link to the parent
1147 * as the first item in the submenu. If the submenu file is the same as the
1148 * parent file someone is trying to link back to the parent manually. In
1149 * this case, don't automatically add a link back to avoid duplication.
1151 if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) {
1152 foreach ( (array)$menu as $parent_menu ) {
1153 if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) )
1154 $submenu[$parent_slug][] = array_slice( $parent_menu, 0, 4 );
1158 $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title );
1160 $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);
1161 if (!empty ( $function ) && !empty ( $hookname ))
1162 add_action( $hookname, $function );
1164 $_registered_pages[$hookname] = true;
1167 * Backward-compatibility for plugins using add_management page.
1168 * See wp-admin/admin.php for redirect from edit.php to tools.php
1170 if ( 'tools.php' == $parent_slug )
1171 $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true;
1173 // No parent as top level.
1174 $_parent_pages[$menu_slug] = $parent_slug;
1180 * Add submenu page to the Tools main menu.
1182 * This function takes a capability which will be used to determine whether
1183 * or not a page is included in the menu.
1185 * The function which is hooked in to handle the output of the page must check
1186 * that the user has the required capability as well.
1188 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1189 * @param string $menu_title The text to be used for the menu.
1190 * @param string $capability The capability required for this menu to be displayed to the user.
1191 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1192 * @param callable $function The function to be called to output the content for this page.
1193 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1195 function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1196 return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1200 * Add submenu page to the Settings main menu.
1202 * This function takes a capability which will be used to determine whether
1203 * or not a page is included in the menu.
1205 * The function which is hooked in to handle the output of the page must check
1206 * that the user has the required capability as well.
1208 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1209 * @param string $menu_title The text to be used for the menu.
1210 * @param string $capability The capability required for this menu to be displayed to the user.
1211 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1212 * @param callable $function The function to be called to output the content for this page.
1213 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1215 function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1216 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1220 * Add submenu page to the Appearance main menu.
1222 * This function takes a capability which will be used to determine whether
1223 * or not a page is included in the menu.
1225 * The function which is hooked in to handle the output of the page must check
1226 * that the user has the required capability as well.
1228 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1229 * @param string $menu_title The text to be used for the menu.
1230 * @param string $capability The capability required for this menu to be displayed to the user.
1231 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1232 * @param callable $function The function to be called to output the content for this page.
1233 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1235 function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1236 return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1240 * Add submenu page to the Plugins main menu.
1242 * This function takes a capability which will be used to determine whether
1243 * or not a page is included in the menu.
1245 * The function which is hooked in to handle the output of the page must check
1246 * that the user has the required capability as well.
1248 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1249 * @param string $menu_title The text to be used for the menu.
1250 * @param string $capability The capability required for this menu to be displayed to the user.
1251 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1252 * @param callable $function The function to be called to output the content for this page.
1253 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1255 function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1256 return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1260 * Add submenu page to the Users/Profile main menu.
1262 * This function takes a capability which will be used to determine whether
1263 * or not a page is included in the menu.
1265 * The function which is hooked in to handle the output of the page must check
1266 * that the user has the required capability as well.
1268 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1269 * @param string $menu_title The text to be used for the menu.
1270 * @param string $capability The capability required for this menu to be displayed to the user.
1271 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1272 * @param callable $function The function to be called to output the content for this page.
1273 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1275 function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1276 if ( current_user_can('edit_users') )
1277 $parent = 'users.php';
1279 $parent = 'profile.php';
1280 return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
1283 * Add submenu page to the Dashboard main menu.
1285 * This function takes a capability which will be used to determine whether
1286 * or not a page is included in the menu.
1288 * The function which is hooked in to handle the output of the page must check
1289 * that the user has the required capability as well.
1291 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1292 * @param string $menu_title The text to be used for the menu.
1293 * @param string $capability The capability required for this menu to be displayed to the user.
1294 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1295 * @param callable $function The function to be called to output the content for this page.
1296 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1298 function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1299 return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1303 * Add submenu page to the Posts main menu.
1305 * This function takes a capability which will be used to determine whether
1306 * or not a page is included in the menu.
1308 * The function which is hooked in to handle the output of the page must check
1309 * that the user has the required capability as well.
1311 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1312 * @param string $menu_title The text to be used for the menu.
1313 * @param string $capability The capability required for this menu to be displayed to the user.
1314 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1315 * @param callable $function The function to be called to output the content for this page.
1316 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1318 function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1319 return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1323 * Add submenu page to the Media main menu.
1325 * This function takes a capability which will be used to determine whether
1326 * or not a page is included in the menu.
1328 * The function which is hooked in to handle the output of the page must check
1329 * that the user has the required capability as well.
1331 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1332 * @param string $menu_title The text to be used for the menu.
1333 * @param string $capability The capability required for this menu to be displayed to the user.
1334 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1335 * @param callable $function The function to be called to output the content for this page.
1336 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1338 function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1339 return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1343 * Add submenu page to the Links main menu.
1345 * This function takes a capability which will be used to determine whether
1346 * or not a page is included in the menu.
1348 * The function which is hooked in to handle the output of the page must check
1349 * that the user has the required capability as well.
1351 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1352 * @param string $menu_title The text to be used for the menu.
1353 * @param string $capability The capability required for this menu to be displayed to the user.
1354 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1355 * @param callable $function The function to be called to output the content for this page.
1356 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1358 function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1359 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1363 * Add submenu page to the Pages main menu.
1365 * This function takes a capability which will be used to determine whether
1366 * or not a page is included in the menu.
1368 * The function which is hooked in to handle the output of the page must check
1369 * that the user has the required capability as well.
1371 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1372 * @param string $menu_title The text to be used for the menu.
1373 * @param string $capability The capability required for this menu to be displayed to the user.
1374 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1375 * @param callable $function The function to be called to output the content for this page.
1376 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1378 function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1379 return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function );
1383 * Add submenu page to the Comments main menu.
1385 * This function takes a capability which will be used to determine whether
1386 * or not a page is included in the menu.
1388 * The function which is hooked in to handle the output of the page must check
1389 * that the user has the required capability as well.
1391 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
1392 * @param string $menu_title The text to be used for the menu.
1393 * @param string $capability The capability required for this menu to be displayed to the user.
1394 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
1395 * @param callable $function The function to be called to output the content for this page.
1396 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
1398 function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1399 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1403 * Remove a top-level admin menu.
1407 * @global array $menu
1409 * @param string $menu_slug The slug of the menu.
1410 * @return array|bool The removed menu on success, false if not found.
1412 function remove_menu_page( $menu_slug ) {
1415 foreach ( $menu as $i => $item ) {
1416 if ( $menu_slug == $item[2] ) {
1426 * Remove an admin submenu.
1430 * @global array $submenu
1432 * @param string $menu_slug The slug for the parent menu.
1433 * @param string $submenu_slug The slug of the submenu.
1434 * @return array|bool The removed submenu on success, false if not found.
1436 function remove_submenu_page( $menu_slug, $submenu_slug ) {
1439 if ( !isset( $submenu[$menu_slug] ) )
1442 foreach ( $submenu[$menu_slug] as $i => $item ) {
1443 if ( $submenu_slug == $item[2] ) {
1444 unset( $submenu[$menu_slug][$i] );
1453 * Get the url to access a particular menu page based on the slug it was registered with.
1455 * If the slug hasn't been registered properly no url will be returned
1459 * @global array $_parent_pages
1461 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1462 * @param bool $echo Whether or not to echo the url - default is true
1463 * @return string the url
1465 function menu_page_url($menu_slug, $echo = true) {
1466 global $_parent_pages;
1468 if ( isset( $_parent_pages[$menu_slug] ) ) {
1469 $parent_slug = $_parent_pages[$menu_slug];
1470 if ( $parent_slug && ! isset( $_parent_pages[$parent_slug] ) ) {
1471 $url = admin_url( add_query_arg( 'page', $menu_slug, $parent_slug ) );
1473 $url = admin_url( 'admin.php?page=' . $menu_slug );
1479 $url = esc_url($url);
1488 // Pluggable Menu Support -- Private
1492 * @global string $parent_file
1493 * @global array $menu
1494 * @global array $submenu
1495 * @global string $pagenow
1496 * @global string $typenow
1497 * @global string $plugin_page
1498 * @global array $_wp_real_parent_file
1499 * @global array $_wp_menu_nopriv
1500 * @global array $_wp_submenu_nopriv
1502 function get_admin_page_parent( $parent = '' ) {
1503 global $parent_file, $menu, $submenu, $pagenow, $typenow,
1504 $plugin_page, $_wp_real_parent_file, $_wp_menu_nopriv, $_wp_submenu_nopriv;
1506 if ( !empty ( $parent ) && 'admin.php' != $parent ) {
1507 if ( isset( $_wp_real_parent_file[$parent] ) )
1508 $parent = $_wp_real_parent_file[$parent];
1512 if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
1513 foreach ( (array)$menu as $parent_menu ) {
1514 if ( $parent_menu[2] == $plugin_page ) {
1515 $parent_file = $plugin_page;
1516 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1517 $parent_file = $_wp_real_parent_file[$parent_file];
1518 return $parent_file;
1521 if ( isset( $_wp_menu_nopriv[$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;
1529 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
1530 $parent_file = $pagenow;
1531 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1532 $parent_file = $_wp_real_parent_file[$parent_file];
1533 return $parent_file;
1536 foreach (array_keys( (array)$submenu ) as $parent) {
1537 foreach ( $submenu[$parent] as $submenu_array ) {
1538 if ( isset( $_wp_real_parent_file[$parent] ) )
1539 $parent = $_wp_real_parent_file[$parent];
1540 if ( !empty($typenow) && ($submenu_array[2] == "$pagenow?post_type=$typenow") ) {
1541 $parent_file = $parent;
1543 } elseif ( $submenu_array[2] == $pagenow && empty($typenow) && ( empty($parent_file) || false === strpos($parent_file, '?') ) ) {
1544 $parent_file = $parent;
1546 } elseif ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
1547 $parent_file = $parent;
1553 if ( empty($parent_file) )
1560 * @global string $title
1561 * @global array $menu
1562 * @global array $submenu
1563 * @global string $pagenow
1564 * @global string $plugin_page
1565 * @global string $typenow
1567 function get_admin_page_title() {
1568 global $title, $menu, $submenu, $pagenow, $plugin_page, $typenow;
1570 if ( ! empty ( $title ) )
1573 $hook = get_plugin_page_hook( $plugin_page, $pagenow );
1575 $parent = $parent1 = get_admin_page_parent();
1577 if ( empty ( $parent) ) {
1578 foreach ( (array)$menu as $menu_array ) {
1579 if ( isset( $menu_array[3] ) ) {
1580 if ( $menu_array[2] == $pagenow ) {
1581 $title = $menu_array[3];
1582 return $menu_array[3];
1583 } elseif ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
1584 $title = $menu_array[3];
1585 return $menu_array[3];
1588 $title = $menu_array[0];
1593 foreach ( array_keys( $submenu ) as $parent ) {
1594 foreach ( $submenu[$parent] as $submenu_array ) {
1595 if ( isset( $plugin_page ) &&
1596 ( $plugin_page == $submenu_array[2] ) &&
1598 ( $parent == $pagenow ) ||
1599 ( $parent == $plugin_page ) ||
1600 ( $plugin_page == $hook ) ||
1601 ( $pagenow == 'admin.php' && $parent1 != $submenu_array[2] ) ||
1602 ( !empty($typenow) && $parent == $pagenow . '?post_type=' . $typenow)
1605 $title = $submenu_array[3];
1606 return $submenu_array[3];
1609 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
1612 if ( isset( $submenu_array[3] ) ) {
1613 $title = $submenu_array[3];
1614 return $submenu_array[3];
1616 $title = $submenu_array[0];
1621 if ( empty ( $title ) ) {
1622 foreach ( $menu as $menu_array ) {
1623 if ( isset( $plugin_page ) &&
1624 ( $plugin_page == $menu_array[2] ) &&
1625 ( $pagenow == 'admin.php' ) &&
1626 ( $parent1 == $menu_array[2] ) )
1628 $title = $menu_array[3];
1629 return $menu_array[3];
1641 * @param string $plugin_page
1642 * @param string $parent_page
1643 * @return string|null
1645 function get_plugin_page_hook( $plugin_page, $parent_page ) {
1646 $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
1647 if ( has_action($hook) )
1655 * @global array $admin_page_hooks
1656 * @param string $plugin_page
1657 * @param string $parent_page
1659 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
1660 global $admin_page_hooks;
1662 $parent = get_admin_page_parent( $parent_page );
1664 $page_type = 'admin';
1665 if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
1666 if ( isset( $admin_page_hooks[$plugin_page] ) ) {
1667 $page_type = 'toplevel';
1668 } elseif ( isset( $admin_page_hooks[$parent] )) {
1669 $page_type = $admin_page_hooks[$parent];
1671 } elseif ( isset( $admin_page_hooks[$parent] ) ) {
1672 $page_type = $admin_page_hooks[$parent];
1675 $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
1677 return $page_type . '_page_' . $plugin_name;
1682 * @global string $pagenow
1683 * @global array $menu
1684 * @global array $submenu
1685 * @global array $_wp_menu_nopriv
1686 * @global array $_wp_submenu_nopriv
1687 * @global string $plugin_page
1688 * @global array $_registered_pages
1690 function user_can_access_admin_page() {
1691 global $pagenow, $menu, $submenu, $_wp_menu_nopriv, $_wp_submenu_nopriv,
1692 $plugin_page, $_registered_pages;
1694 $parent = get_admin_page_parent();
1696 if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
1699 if ( isset( $plugin_page ) ) {
1700 if ( isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
1703 $hookname = get_plugin_page_hookname($plugin_page, $parent);
1705 if ( !isset($_registered_pages[$hookname]) )
1709 if ( empty( $parent) ) {
1710 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
1712 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
1714 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
1716 if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1718 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
1719 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
1721 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
1727 if ( isset( $plugin_page ) && ( $plugin_page == $parent ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1730 if ( isset( $submenu[$parent] ) ) {
1731 foreach ( $submenu[$parent] as $submenu_array ) {
1732 if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
1733 if ( current_user_can( $submenu_array[1] ))
1737 } elseif ( $submenu_array[2] == $pagenow ) {
1738 if ( current_user_can( $submenu_array[1] ))
1746 foreach ( $menu as $menu_array ) {
1747 if ( $menu_array[2] == $parent) {
1748 if ( current_user_can( $menu_array[1] ))
1758 /* Whitelist functions */
1761 * Register a setting and its sanitization callback
1765 * @global array $new_whitelist_options
1767 * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
1768 * Default whitelisted option key names include "general," "discussion," and "reading," among others.
1769 * @param string $option_name The name of an option to sanitize and save.
1770 * @param callable $sanitize_callback A callback function that sanitizes the option's value.
1772 function register_setting( $option_group, $option_name, $sanitize_callback = '' ) {
1773 global $new_whitelist_options;
1775 if ( 'misc' == $option_group ) {
1776 _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
1777 $option_group = 'general';
1780 if ( 'privacy' == $option_group ) {
1781 _deprecated_argument( __FUNCTION__, '3.5', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
1782 $option_group = 'reading';
1785 $new_whitelist_options[ $option_group ][] = $option_name;
1786 if ( $sanitize_callback != '' )
1787 add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1791 * Unregister a setting
1795 * @global array $new_whitelist_options
1797 * @param string $option_group
1798 * @param string $option_name
1799 * @param callable $sanitize_callback
1801 function unregister_setting( $option_group, $option_name, $sanitize_callback = '' ) {
1802 global $new_whitelist_options;
1804 if ( 'misc' == $option_group ) {
1805 _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
1806 $option_group = 'general';
1809 if ( 'privacy' == $option_group ) {
1810 _deprecated_argument( __FUNCTION__, '3.5', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
1811 $option_group = 'reading';
1814 $pos = array_search( $option_name, (array) $new_whitelist_options[ $option_group ] );
1815 if ( $pos !== false )
1816 unset( $new_whitelist_options[ $option_group ][ $pos ] );
1817 if ( $sanitize_callback != '' )
1818 remove_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1822 * Refreshes the value of the options whitelist available via the 'whitelist_options' filter.
1826 * @global array $new_whitelist_options
1828 * @param array $options
1831 function option_update_filter( $options ) {
1832 global $new_whitelist_options;
1834 if ( is_array( $new_whitelist_options ) )
1835 $options = add_option_whitelist( $new_whitelist_options, $options );
1841 * Adds an array of options to the options whitelist.
1845 * @global array $whitelist_options
1847 * @param array $new_options
1848 * @param string|array $options
1851 function add_option_whitelist( $new_options, $options = '' ) {
1852 if ( $options == '' )
1853 global $whitelist_options;
1855 $whitelist_options = $options;
1857 foreach ( $new_options as $page => $keys ) {
1858 foreach ( $keys as $key ) {
1859 if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
1860 $whitelist_options[ $page ] = array();
1861 $whitelist_options[ $page ][] = $key;
1863 $pos = array_search( $key, $whitelist_options[ $page ] );
1864 if ( $pos === false )
1865 $whitelist_options[ $page ][] = $key;
1870 return $whitelist_options;
1874 * Removes a list of options from the options whitelist.
1878 * @global array $whitelist_options
1880 * @param array $del_options
1881 * @param string|array $options
1884 function remove_option_whitelist( $del_options, $options = '' ) {
1885 if ( $options == '' )
1886 global $whitelist_options;
1888 $whitelist_options = $options;
1890 foreach ( $del_options as $page => $keys ) {
1891 foreach ( $keys as $key ) {
1892 if ( isset($whitelist_options[ $page ]) && is_array($whitelist_options[ $page ]) ) {
1893 $pos = array_search( $key, $whitelist_options[ $page ] );
1894 if ( $pos !== false )
1895 unset( $whitelist_options[ $page ][ $pos ] );
1900 return $whitelist_options;
1904 * Output nonce, action, and option_page fields for a settings page.
1908 * @param string $option_group A settings group name. This should match the group name used in register_setting().
1910 function settings_fields($option_group) {
1911 echo "<input type='hidden' name='option_page' value='" . esc_attr($option_group) . "' />";
1912 echo '<input type="hidden" name="action" value="update" />';
1913 wp_nonce_field("$option_group-options");
1917 * Clears the Plugins cache used by get_plugins() and by default, the Plugin Update cache.
1921 * @param bool $clear_update_cache Whether to clear the Plugin updates cache
1923 function wp_clean_plugins_cache( $clear_update_cache = true ) {
1924 if ( $clear_update_cache )
1925 delete_site_transient( 'update_plugins' );
1926 wp_cache_delete( 'plugins', 'plugins' );
1930 * @param string $plugin
1932 function plugin_sandbox_scrape( $plugin ) {
1933 wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
1934 include( WP_PLUGIN_DIR . '/' . $plugin );