3 * WordPress Plugin Administration API
6 * @subpackage Administration
10 * Parse 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.
19 * Plugin Name: Name of Plugin
20 * Plugin URI: Link to plugin information
21 * Description: Plugin Description
22 * Author: Plugin author's name
23 * Author URI: Link to the author's web site
24 * Version: Must be set in the plugin for WordPress 2.3+
25 * Text Domain: Optional. Unique identifier, should be same as the one used in
26 * plugin_text_domain()
27 * Domain Path: Optional. Only useful if the translations are located in a
28 * folder above the plugin's base path. For example, if .mo files are
29 * located in the locale folder then Domain Path will be "/locale/" and
30 * must have the first slash. Defaults to the base folder the plugin is
32 * Network: Optional. Specify "Network: true" to require that a plugin is activated
33 * across all sites in an installation. This will prevent a plugin from being
34 * activated on a single site when Multisite is enabled.
35 * * / # Remove the space to close comment
38 * Plugin data returned array contains the following:
39 * 'Name' - Name of the plugin, must be unique.
40 * 'Title' - Title of the plugin and the link to the plugin's web site.
41 * 'Description' - Description of what the plugin does and/or notes
43 * 'Author' - The author's name
44 * 'AuthorURI' - The authors web site address.
45 * 'Version' - The plugin version number.
46 * 'PluginURI' - Plugin web site address.
47 * 'TextDomain' - Plugin's text domain for localization.
48 * 'DomainPath' - Plugin's relative directory path to .mo files.
49 * 'Network' - Boolean. Whether the plugin can only be activated network wide.
51 * Some users have issues with opening large files and manipulating the contents
52 * for want is usually the first 1kiB or 2kiB. This function stops pulling in
53 * the plugin contents when it has all of the required plugin data.
55 * The first 8kiB of the file will be pulled in and if the plugin data is not
56 * within that first 8kiB, then the plugin author should correct their plugin
57 * and move the plugin data headers to the top.
59 * The plugin file is assumed to have permissions to allow for scripts to read
60 * the file. This is not checked however and the file is only opened for
63 * @link http://trac.wordpress.org/ticket/5651 Previous Optimizations.
64 * @link http://trac.wordpress.org/ticket/7372 Further and better Optimizations.
67 * @param string $plugin_file Path to the plugin file
68 * @param bool $markup Optional. If the returned data should have HTML markup applied. Defaults to true.
69 * @param bool $translate Optional. If the returned data should be translated. Defaults to true.
70 * @return array See above for description.
72 function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
74 $default_headers = array(
75 'Name' => 'Plugin Name',
76 'PluginURI' => 'Plugin URI',
77 'Version' => 'Version',
78 'Description' => 'Description',
80 'AuthorURI' => 'Author URI',
81 'TextDomain' => 'Text Domain',
82 'DomainPath' => 'Domain Path',
83 'Network' => 'Network',
84 // Site Wide Only is deprecated in favor of Network.
85 '_sitewide' => 'Site Wide Only',
88 $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
90 // Site Wide Only is the old header for Network
91 if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) {
92 _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The <code>%1$s</code> plugin header is deprecated. Use <code>%2$s</code> instead.' ), 'Site Wide Only: true', 'Network: true' ) );
93 $plugin_data['Network'] = $plugin_data['_sitewide'];
95 $plugin_data['Network'] = ( 'true' == strtolower( $plugin_data['Network'] ) );
96 unset( $plugin_data['_sitewide'] );
98 if ( $markup || $translate ) {
99 $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
101 $plugin_data['Title'] = $plugin_data['Name'];
102 $plugin_data['AuthorName'] = $plugin_data['Author'];
109 * Sanitizes plugin data, optionally adds markup, optionally translates.
113 * @see get_plugin_data()
115 function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) {
119 if ( $textdomain = $plugin_data['TextDomain'] ) {
120 if ( $plugin_data['DomainPath'] )
121 load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] );
123 load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) );
124 } elseif ( in_array( basename( $plugin_file ), array( 'hello.php', 'akismet.php' ) ) ) {
125 $textdomain = 'default';
128 foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field )
129 $plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain );
134 $allowed_tags = $allowed_tags_in_links = array(
135 'abbr' => array( 'title' => true ),
136 'acronym' => array( 'title' => true ),
141 $allowed_tags['a'] = array( 'href' => true, 'title' => true );
143 // Name is marked up inside <a> tags. Don't allow these.
144 // Author is too, but some plugins have used <a> here (omitting Author URI).
145 $plugin_data['Name'] = wp_kses( $plugin_data['Name'], $allowed_tags_in_links );
146 $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $allowed_tags );
148 $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags );
149 $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $allowed_tags );
151 $plugin_data['PluginURI'] = esc_url( $plugin_data['PluginURI'] );
152 $plugin_data['AuthorURI'] = esc_url( $plugin_data['AuthorURI'] );
154 $plugin_data['Title'] = $plugin_data['Name'];
155 $plugin_data['AuthorName'] = $plugin_data['Author'];
159 if ( $plugin_data['PluginURI'] && $plugin_data['Name'] )
160 $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . esc_attr__( 'Visit plugin homepage' ) . '">' . $plugin_data['Name'] . '</a>';
162 if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] )
163 $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . esc_attr__( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>';
165 $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
167 if ( $plugin_data['Author'] )
168 $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s.'), $plugin_data['Author'] ) . '</cite>';
175 * Get a list of a plugin's files.
179 * @param string $plugin Plugin ID
180 * @return array List of files relative to the plugin root.
182 function get_plugin_files($plugin) {
183 $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
184 $dir = dirname($plugin_file);
185 $plugin_files = array($plugin);
186 if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) {
187 $plugins_dir = @ opendir( $dir );
188 if ( $plugins_dir ) {
189 while (($file = readdir( $plugins_dir ) ) !== false ) {
190 if ( substr($file, 0, 1) == '.' )
192 if ( is_dir( $dir . '/' . $file ) ) {
193 $plugins_subdir = @ opendir( $dir . '/' . $file );
194 if ( $plugins_subdir ) {
195 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
196 if ( substr($subfile, 0, 1) == '.' )
198 $plugin_files[] = plugin_basename("$dir/$file/$subfile");
200 @closedir( $plugins_subdir );
203 if ( plugin_basename("$dir/$file") != $plugin )
204 $plugin_files[] = plugin_basename("$dir/$file");
207 @closedir( $plugins_dir );
211 return $plugin_files;
215 * Check the plugins directory and retrieve all plugin files with plugin data.
217 * WordPress only supports plugin files in the base plugins directory
218 * (wp-content/plugins) and in one directory above the plugins directory
219 * (wp-content/plugins/my-plugin). The file it looks for has the plugin data and
220 * must be found in those two locations. It is recommended that do keep your
221 * plugin files in directories.
223 * The file with the plugin data is the file that will be included and therefore
224 * needs to have the main execution for the plugin. This does not mean
225 * everything must be contained in the file and it is recommended that the file
226 * be split for maintainability. Keep everything in one file for extreme
227 * optimization purposes.
231 * @param string $plugin_folder Optional. Relative path to single plugin folder.
232 * @return array Key is the plugin file path and the value is an array of the plugin data.
234 function get_plugins($plugin_folder = '') {
236 if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
237 $cache_plugins = array();
239 if ( isset($cache_plugins[ $plugin_folder ]) )
240 return $cache_plugins[ $plugin_folder ];
242 $wp_plugins = array ();
243 $plugin_root = WP_PLUGIN_DIR;
244 if ( !empty($plugin_folder) )
245 $plugin_root .= $plugin_folder;
247 // Files in wp-content/plugins directory
248 $plugins_dir = @ opendir( $plugin_root);
249 $plugin_files = array();
250 if ( $plugins_dir ) {
251 while (($file = readdir( $plugins_dir ) ) !== false ) {
252 if ( substr($file, 0, 1) == '.' )
254 if ( is_dir( $plugin_root.'/'.$file ) ) {
255 $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
256 if ( $plugins_subdir ) {
257 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
258 if ( substr($subfile, 0, 1) == '.' )
260 if ( substr($subfile, -4) == '.php' )
261 $plugin_files[] = "$file/$subfile";
263 closedir( $plugins_subdir );
266 if ( substr($file, -4) == '.php' )
267 $plugin_files[] = $file;
270 closedir( $plugins_dir );
273 if ( empty($plugin_files) )
276 foreach ( $plugin_files as $plugin_file ) {
277 if ( !is_readable( "$plugin_root/$plugin_file" ) )
280 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
282 if ( empty ( $plugin_data['Name'] ) )
285 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
288 uasort( $wp_plugins, '_sort_uname_callback' );
290 $cache_plugins[ $plugin_folder ] = $wp_plugins;
291 wp_cache_set('plugins', $cache_plugins, 'plugins');
297 * Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data.
299 * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins).
302 * @return array Key is the mu-plugin file path and the value is an array of the mu-plugin data.
304 function get_mu_plugins() {
305 $wp_plugins = array();
306 // Files in wp-content/mu-plugins directory
307 $plugin_files = array();
309 if ( ! is_dir( WPMU_PLUGIN_DIR ) )
311 if ( $plugins_dir = @ opendir( WPMU_PLUGIN_DIR ) ) {
312 while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
313 if ( substr( $file, -4 ) == '.php' )
314 $plugin_files[] = $file;
320 @closedir( $plugins_dir );
322 if ( empty($plugin_files) )
325 foreach ( $plugin_files as $plugin_file ) {
326 if ( !is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) )
329 $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
331 if ( empty ( $plugin_data['Name'] ) )
332 $plugin_data['Name'] = $plugin_file;
334 $wp_plugins[ $plugin_file ] = $plugin_data;
337 if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php') <= 30 ) // silence is golden
338 unset( $wp_plugins['index.php'] );
340 uasort( $wp_plugins, '_sort_uname_callback' );
346 * Callback to sort array by a 'Name' key.
351 function _sort_uname_callback( $a, $b ) {
352 return strnatcasecmp( $a['Name'], $b['Name'] );
356 * Check the wp-content directory and retrieve all drop-ins with any plugin data.
359 * @return array Key is the file path and the value is an array of the plugin data.
361 function get_dropins() {
363 $plugin_files = array();
365 $_dropins = _get_dropins();
367 // These exist in the wp-content directory
368 if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) {
369 while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
370 if ( isset( $_dropins[ $file ] ) )
371 $plugin_files[] = $file;
377 @closedir( $plugins_dir );
379 if ( empty($plugin_files) )
382 foreach ( $plugin_files as $plugin_file ) {
383 if ( !is_readable( WP_CONTENT_DIR . "/$plugin_file" ) )
385 $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
386 if ( empty( $plugin_data['Name'] ) )
387 $plugin_data['Name'] = $plugin_file;
388 $dropins[ $plugin_file ] = $plugin_data;
391 uksort( $dropins, 'strnatcasecmp' );
397 * Returns drop-ins that WordPress uses.
399 * Includes Multisite drop-ins only when is_multisite()
402 * @return array Key is file name. The value is an array, with the first value the
403 * purpose of the drop-in and the second value the name of the constant that must be
404 * true for the drop-in to be used, or true if no constant is required.
406 function _get_dropins() {
408 'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE
409 'db.php' => array( __( 'Custom database class.' ), true ), // auto on load
410 'db-error.php' => array( __( 'Custom database error message.' ), true ), // auto on error
411 'install.php' => array( __( 'Custom install script.' ), true ), // auto on install
412 'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // auto on maintenance
413 'object-cache.php' => array( __( 'External object cache.' ), true ), // auto on load
416 if ( is_multisite() ) {
417 $dropins['sunrise.php' ] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE
418 $dropins['blog-deleted.php' ] = array( __( 'Custom site deleted message.' ), true ); // auto on deleted blog
419 $dropins['blog-inactive.php' ] = array( __( 'Custom site inactive message.' ), true ); // auto on inactive blog
420 $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // auto on archived or spammed blog
427 * Check whether the plugin is active by checking the active_plugins list.
431 * @param string $plugin Base plugin path from plugins directory.
432 * @return bool True, if in the active plugins list. False, not in the list.
434 function is_plugin_active( $plugin ) {
435 return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
439 * Check whether the plugin is inactive.
441 * Reverse of is_plugin_active(). Used as a callback.
444 * @see is_plugin_active()
446 * @param string $plugin Base plugin path from plugins directory.
447 * @return bool True if inactive. False if active.
449 function is_plugin_inactive( $plugin ) {
450 return ! is_plugin_active( $plugin );
454 * Check whether the plugin is active for the entire network.
458 * @param string $plugin Base plugin path from plugins directory.
459 * @return bool True, if active for the network, otherwise false.
461 function is_plugin_active_for_network( $plugin ) {
462 if ( !is_multisite() )
465 $plugins = get_site_option( 'active_sitewide_plugins');
466 if ( isset($plugins[$plugin]) )
473 * Checks for "Network: true" in the plugin header to see if this should
474 * be activated only as a network wide plugin. The plugin would also work
475 * when Multisite is not enabled.
477 * Checks for "Site Wide Only: true" for backwards compatibility.
481 * @param string $plugin Plugin to check
482 * @return bool True if plugin is network only, false otherwise.
484 function is_network_only_plugin( $plugin ) {
485 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
487 return $plugin_data['Network'];
492 * Attempts activation of plugin in a "sandbox" and redirects on success.
494 * A plugin that is already activated will not attempt to be activated again.
496 * The way it works is by setting the redirection to the error before trying to
497 * include the plugin file. If the plugin fails, then the redirection will not
498 * be overwritten with the success message. Also, the options will not be
499 * updated and the activation hook will not be called on plugin error.
501 * It should be noted that in no way the below code will actually prevent errors
502 * within the file. The code should not be used elsewhere to replicate the
503 * "sandbox", which uses redirection to work.
506 * If any errors are found or text is outputted, then it will be captured to
507 * ensure that the success redirection will update the error redirection.
511 * @param string $plugin Plugin path to main plugin file with plugin data.
512 * @param string $redirect Optional. URL to redirect to.
513 * @param bool $network_wide Whether to enable the plugin for all sites in the
514 * network or just the current site. Multisite only. Default is false.
515 * @param bool $silent Prevent calling activation hooks. Optional, default is false.
516 * @return WP_Error|null WP_Error on invalid file or null on success.
518 function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
519 $plugin = plugin_basename( trim( $plugin ) );
521 if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) {
522 $network_wide = true;
523 $current = get_site_option( 'active_sitewide_plugins', array() );
524 $_GET['networkwide'] = 1; // Back compat for plugins looking for this value.
526 $current = get_option( 'active_plugins', array() );
529 $valid = validate_plugin($plugin);
530 if ( is_wp_error($valid) )
533 if ( !in_array($plugin, $current) ) {
534 if ( !empty($redirect) )
535 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
537 include_once(WP_PLUGIN_DIR . '/' . $plugin);
540 do_action( 'activate_plugin', $plugin, $network_wide );
541 do_action( 'activate_' . $plugin, $network_wide );
544 if ( $network_wide ) {
545 $current[$plugin] = time();
546 update_site_option( 'active_sitewide_plugins', $current );
548 $current[] = $plugin;
550 update_option('active_plugins', $current);
554 do_action( 'activated_plugin', $plugin, $network_wide );
557 if ( ob_get_length() > 0 ) {
558 $output = ob_get_clean();
559 return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
568 * Deactivate a single plugin or multiple plugins.
570 * The deactivation hook is disabled by the plugin upgrader by using the $silent
575 * @param string|array $plugins Single plugin or list of plugins to deactivate.
576 * @param bool $silent Prevent calling deactivation hooks. Default is false.
577 * @param mixed $network_wide Whether to deactivate the plugin for all sites in the network.
578 * A value of null (the default) will deactivate plugins for both the site and the network.
580 function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) {
581 if ( is_multisite() )
582 $network_current = get_site_option( 'active_sitewide_plugins', array() );
583 $current = get_option( 'active_plugins', array() );
584 $do_blog = $do_network = false;
586 foreach ( (array) $plugins as $plugin ) {
587 $plugin = plugin_basename( trim( $plugin ) );
588 if ( ! is_plugin_active($plugin) )
591 $network_deactivating = false !== $network_wide && is_plugin_active_for_network( $plugin );
594 do_action( 'deactivate_plugin', $plugin, $network_deactivating );
596 if ( false !== $network_wide ) {
597 if ( is_plugin_active_for_network( $plugin ) ) {
599 unset( $network_current[ $plugin ] );
600 } elseif ( $network_wide ) {
605 if ( true !== $network_wide ) {
606 $key = array_search( $plugin, $current );
607 if ( false !== $key ) {
609 unset( $current[ $key ] );
614 do_action( 'deactivate_' . $plugin, $network_deactivating );
615 do_action( 'deactivated_plugin', $plugin, $network_deactivating );
620 update_option('active_plugins', $current);
622 update_site_option( 'active_sitewide_plugins', $network_current );
626 * Activate multiple plugins.
628 * When WP_Error is returned, it does not mean that one of the plugins had
629 * errors. It means that one or more of the plugins file path was invalid.
631 * The execution will be halted as soon as one of the plugins has an error.
635 * @param string|array $plugins
636 * @param string $redirect Redirect to page after successful activation.
637 * @param bool $network_wide Whether to enable the plugin for all sites in the network.
638 * @param bool $silent Prevent calling activation hooks. Default is false.
639 * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
641 function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
642 if ( !is_array($plugins) )
643 $plugins = array($plugins);
646 foreach ( $plugins as $plugin ) {
647 if ( !empty($redirect) )
648 $redirect = add_query_arg('plugin', $plugin, $redirect);
649 $result = activate_plugin($plugin, $redirect, $network_wide, $silent);
650 if ( is_wp_error($result) )
651 $errors[$plugin] = $result;
654 if ( !empty($errors) )
655 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
661 * Remove directory and files of a plugin for a single or list of plugin(s).
663 * If the plugins parameter list is empty, false will be returned. True when
668 * @param array $plugins List of plugin
669 * @param string $redirect Redirect to page when complete.
672 function delete_plugins($plugins, $redirect = '' ) {
673 global $wp_filesystem;
675 if ( empty($plugins) )
679 foreach( $plugins as $plugin )
680 $checked[] = 'checked[]=' . $plugin;
683 $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-plugins');
684 if ( false === ($credentials = request_filesystem_credentials($url)) ) {
685 $data = ob_get_contents();
687 if ( ! empty($data) ){
688 include_once( ABSPATH . 'wp-admin/admin-header.php');
690 include( ABSPATH . 'wp-admin/admin-footer.php');
696 if ( ! WP_Filesystem($credentials) ) {
697 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
698 $data = ob_get_contents();
700 if ( ! empty($data) ){
701 include_once( ABSPATH . 'wp-admin/admin-header.php');
703 include( ABSPATH . 'wp-admin/admin-footer.php');
709 if ( ! is_object($wp_filesystem) )
710 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
712 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
713 return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
715 //Get the base plugin folder
716 $plugins_dir = $wp_filesystem->wp_plugins_dir();
717 if ( empty($plugins_dir) )
718 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
720 $plugins_dir = trailingslashit( $plugins_dir );
724 foreach( $plugins as $plugin_file ) {
725 // Run Uninstall hook
726 if ( is_uninstallable_plugin( $plugin_file ) )
727 uninstall_plugin($plugin_file);
729 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
730 // If plugin is in its own directory, recursively delete the directory.
731 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
732 $deleted = $wp_filesystem->delete($this_plugin_dir, true);
734 $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
737 $errors[] = $plugin_file;
740 if ( ! empty($errors) )
741 return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s.'), implode(', ', $errors)) );
743 // Force refresh of plugin update information
744 if ( $current = get_site_transient('update_plugins') ) {
745 unset( $current->response[ $plugin_file ] );
746 set_site_transient('update_plugins', $current);
753 * Validate active plugins
755 * Validate all active plugins, deactivates invalid and
756 * returns an array of deactivated ones.
759 * @return array invalid plugins, plugin as key, error as value
761 function validate_active_plugins() {
762 $plugins = get_option( 'active_plugins', array() );
763 // validate vartype: array
764 if ( ! is_array( $plugins ) ) {
765 update_option( 'active_plugins', array() );
769 if ( is_multisite() && is_super_admin() ) {
770 $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
771 $plugins = array_merge( $plugins, array_keys( $network_plugins ) );
774 if ( empty( $plugins ) )
779 // invalid plugins get deactivated
780 foreach ( $plugins as $plugin ) {
781 $result = validate_plugin( $plugin );
782 if ( is_wp_error( $result ) ) {
783 $invalid[$plugin] = $result;
784 deactivate_plugins( $plugin, true );
791 * Validate the plugin path.
793 * Checks that the file exists and {@link validate_file() is valid file}.
797 * @param string $plugin Plugin Path
798 * @return WP_Error|int 0 on success, WP_Error on failure.
800 function validate_plugin($plugin) {
801 if ( validate_file($plugin) )
802 return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
803 if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
804 return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
806 $installed_plugins = get_plugins();
807 if ( ! isset($installed_plugins[$plugin]) )
808 return new WP_Error('no_plugin_header', __('The plugin does not have a valid header.'));
813 * Whether the plugin can be uninstalled.
817 * @param string $plugin Plugin path to check.
818 * @return bool Whether plugin can be uninstalled.
820 function is_uninstallable_plugin($plugin) {
821 $file = plugin_basename($plugin);
823 $uninstallable_plugins = (array) get_option('uninstall_plugins');
824 if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
831 * Uninstall a single plugin.
833 * Calls the uninstall hook, if it is available.
837 * @param string $plugin Relative plugin path from Plugin Directory.
839 function uninstall_plugin($plugin) {
840 $file = plugin_basename($plugin);
842 $uninstallable_plugins = (array) get_option('uninstall_plugins');
843 if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
844 if ( isset( $uninstallable_plugins[$file] ) ) {
845 unset($uninstallable_plugins[$file]);
846 update_option('uninstall_plugins', $uninstallable_plugins);
848 unset($uninstallable_plugins);
850 define('WP_UNINSTALL_PLUGIN', $file);
851 include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
856 if ( isset( $uninstallable_plugins[$file] ) ) {
857 $callable = $uninstallable_plugins[$file];
858 unset($uninstallable_plugins[$file]);
859 update_option('uninstall_plugins', $uninstallable_plugins);
860 unset($uninstallable_plugins);
862 include WP_PLUGIN_DIR . '/' . $file;
864 add_action( 'uninstall_' . $file, $callable );
865 do_action( 'uninstall_' . $file );
874 * Add a top level menu page
876 * This function takes a capability which will be used to determine whether
877 * or not a page is included in the menu.
879 * The function which is hooked in to handle the output of the page must check
880 * that the user has the required capability as well.
882 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
883 * @param string $menu_title The text to be used for the menu
884 * @param string $capability The capability required for this menu to be displayed to the user.
885 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
886 * @param callback $function The function to be called to output the content for this page.
887 * @param string $icon_url The url to the icon to be used for this menu. Using 'none' would leave div.wp-menu-image empty
888 * so an icon can be added as background with CSS.
889 * @param int $position The position in the menu order this one should appear
891 * @return string The resulting page's hook_suffix
893 function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) {
894 global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
896 $menu_slug = plugin_basename( $menu_slug );
898 $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title );
900 $hookname = get_plugin_page_hookname( $menu_slug, '' );
902 if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) )
903 add_action( $hookname, $function );
905 if ( empty($icon_url) ) {
907 $icon_class = 'menu-icon-generic ';
909 $icon_url = set_url_scheme( $icon_url );
913 $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url );
915 if ( null === $position )
918 $menu[$position] = $new_menu;
920 $_registered_pages[$hookname] = true;
922 // No parent as top level
923 $_parent_pages[$menu_slug] = false;
929 * Add a top level menu page in the 'objects' section
931 * This function takes a capability which will be used to determine whether
932 * or not a page is included in the menu.
934 * The function which is hooked in to handle the output of the page must check
935 * that the user has the required capability as well.
937 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
938 * @param string $menu_title The text to be used for the menu
939 * @param string $capability The capability required for this menu to be displayed to the user.
940 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
941 * @param callback $function The function to be called to output the content for this page.
942 * @param string $icon_url The url to the icon to be used for this menu
944 * @return string The resulting page's hook_suffix
946 function add_object_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
947 global $_wp_last_object_menu;
949 $_wp_last_object_menu++;
951 return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_object_menu);
955 * Add a top level menu page in the 'utility' section
957 * This function takes a capability which will be used to determine whether
958 * or not a page is included in the menu.
960 * The function which is hooked in to handle the output of the page must check
961 * that the user has the required capability as well.
963 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
964 * @param string $menu_title The text to be used for the menu
965 * @param string $capability The capability required for this menu to be displayed to the user.
966 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
967 * @param callback $function The function to be called to output the content for this page.
968 * @param string $icon_url The url to the icon to be used for this menu
970 * @return string The resulting page's hook_suffix
972 function add_utility_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
973 global $_wp_last_utility_menu;
975 $_wp_last_utility_menu++;
977 return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_utility_menu);
981 * Add a sub menu page
983 * This function takes a capability which will be used to determine whether
984 * or not a page is included in the menu.
986 * The function which is hooked in to handle the output of the page must check
987 * that the user has the required capability as well.
989 * @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page)
990 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
991 * @param string $menu_title The text to be used for the menu
992 * @param string $capability The capability required for this menu to be displayed to the user.
993 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
994 * @param callback $function The function to be called to output the content for this page.
996 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
998 function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1001 global $_wp_real_parent_file;
1002 global $_wp_submenu_nopriv;
1003 global $_registered_pages;
1004 global $_parent_pages;
1006 $menu_slug = plugin_basename( $menu_slug );
1007 $parent_slug = plugin_basename( $parent_slug);
1009 if ( isset( $_wp_real_parent_file[$parent_slug] ) )
1010 $parent_slug = $_wp_real_parent_file[$parent_slug];
1012 if ( !current_user_can( $capability ) ) {
1013 $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true;
1017 // If the parent doesn't already have a submenu, add a link to the parent
1018 // as the first item in the submenu. If the submenu file is the same as the
1019 // parent file someone is trying to link back to the parent manually. In
1020 // this case, don't automatically add a link back to avoid duplication.
1021 if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) {
1022 foreach ( (array)$menu as $parent_menu ) {
1023 if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) )
1024 $submenu[$parent_slug][] = $parent_menu;
1028 $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title );
1030 $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);
1031 if (!empty ( $function ) && !empty ( $hookname ))
1032 add_action( $hookname, $function );
1034 $_registered_pages[$hookname] = true;
1035 // backwards-compatibility for plugins using add_management page. See wp-admin/admin.php for redirect from edit.php to tools.php
1036 if ( 'tools.php' == $parent_slug )
1037 $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true;
1039 // No parent as top level
1040 $_parent_pages[$menu_slug] = $parent_slug;
1046 * Add sub menu page to the tools main menu.
1048 * This function takes a capability which will be used to determine whether
1049 * or not a page is included in the menu.
1051 * The function which is hooked in to handle the output of the page must check
1052 * that the user has the required capability as well.
1054 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1055 * @param string $menu_title The text to be used for the menu
1056 * @param string $capability The capability required for this menu to be displayed to the user.
1057 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1058 * @param callback $function The function to be called to output the content for this page.
1060 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1062 function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1063 return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1067 * Add sub menu page to the options main menu.
1069 * This function takes a capability which will be used to determine whether
1070 * or not a page is included in the menu.
1072 * The function which is hooked in to handle the output of the page must check
1073 * that the user has the required capability as well.
1075 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1076 * @param string $menu_title The text to be used for the menu
1077 * @param string $capability The capability required for this menu to be displayed to the user.
1078 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1079 * @param callback $function The function to be called to output the content for this page.
1081 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1083 function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1084 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1088 * Add sub menu page to the themes main menu.
1090 * This function takes a capability which will be used to determine whether
1091 * or not a page is included in the menu.
1093 * The function which is hooked in to handle the output of the page must check
1094 * that the user has the required capability as well.
1096 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1097 * @param string $menu_title The text to be used for the menu
1098 * @param string $capability The capability required for this menu to be displayed to the user.
1099 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1100 * @param callback $function The function to be called to output the content for this page.
1102 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1104 function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1105 return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1109 * Add sub menu page to the plugins main menu.
1111 * This function takes a capability which will be used to determine whether
1112 * or not a page is included in the menu.
1114 * The function which is hooked in to handle the output of the page must check
1115 * that the user has the required capability as well.
1117 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1118 * @param string $menu_title The text to be used for the menu
1119 * @param string $capability The capability required for this menu to be displayed to the user.
1120 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1121 * @param callback $function The function to be called to output the content for this page.
1123 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1125 function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1126 return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1130 * Add sub menu page to the Users/Profile main menu.
1132 * This function takes a capability which will be used to determine whether
1133 * or not a page is included in the menu.
1135 * The function which is hooked in to handle the output of the page must check
1136 * that the user has the required capability as well.
1138 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1139 * @param string $menu_title The text to be used for the menu
1140 * @param string $capability The capability required for this menu to be displayed to the user.
1141 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1142 * @param callback $function The function to be called to output the content for this page.
1144 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1146 function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1147 if ( current_user_can('edit_users') )
1148 $parent = 'users.php';
1150 $parent = 'profile.php';
1151 return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
1154 * Add sub menu page to the Dashboard main menu.
1156 * This function takes a capability which will be used to determine whether
1157 * or not a page is included in the menu.
1159 * The function which is hooked in to handle the output of the page must check
1160 * that the user has the required capability as well.
1162 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1163 * @param string $menu_title The text to be used for the menu
1164 * @param string $capability The capability required for this menu to be displayed to the user.
1165 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1166 * @param callback $function The function to be called to output the content for this page.
1168 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1170 function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1171 return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1175 * Add sub menu page to the posts main menu.
1177 * This function takes a capability which will be used to determine whether
1178 * or not a page is included in the menu.
1180 * The function which is hooked in to handle the output of the page must check
1181 * that the user has the required capability as well.
1183 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1184 * @param string $menu_title The text to be used for the menu
1185 * @param string $capability The capability required for this menu to be displayed to the user.
1186 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1187 * @param callback $function The function to be called to output the content for this page.
1189 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1191 function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1192 return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1196 * Add sub menu page to the media main menu.
1198 * This function takes a capability which will be used to determine whether
1199 * or not a page is included in the menu.
1201 * The function which is hooked in to handle the output of the page must check
1202 * that the user has the required capability as well.
1204 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1205 * @param string $menu_title The text to be used for the menu
1206 * @param string $capability The capability required for this menu to be displayed to the user.
1207 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1208 * @param callback $function The function to be called to output the content for this page.
1210 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1212 function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1213 return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1217 * Add sub menu page to the links main menu.
1219 * This function takes a capability which will be used to determine whether
1220 * or not a page is included in the menu.
1222 * The function which is hooked in to handle the output of the page must check
1223 * that the user has the required capability as well.
1225 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1226 * @param string $menu_title The text to be used for the menu
1227 * @param string $capability The capability required for this menu to be displayed to the user.
1228 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1229 * @param callback $function The function to be called to output the content for this page.
1231 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1233 function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1234 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1238 * Add sub menu page to the pages main menu.
1240 * This function takes a capability which will be used to determine whether
1241 * or not a page is included in the menu.
1243 * The function which is hooked in to handle the output of the page must check
1244 * that the user has the required capability as well.
1246 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1247 * @param string $menu_title The text to be used for the menu
1248 * @param string $capability The capability required for this menu to be displayed to the user.
1249 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1250 * @param callback $function The function to be called to output the content for this page.
1252 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1254 function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1255 return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function );
1259 * Add sub menu page to the comments main menu.
1261 * This function takes a capability which will be used to determine whether
1262 * or not a page is included in the menu.
1264 * The function which is hooked in to handle the output of the page must check
1265 * that the user has the required capability as well.
1267 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1268 * @param string $menu_title The text to be used for the menu
1269 * @param string $capability The capability required for this menu to be displayed to the user.
1270 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1271 * @param callback $function The function to be called to output the content for this page.
1273 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1275 function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1276 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1280 * Remove a top level admin menu
1284 * @param string $menu_slug The slug of the menu
1285 * @return array|bool The removed menu on success, False if not found
1287 function remove_menu_page( $menu_slug ) {
1290 foreach ( $menu as $i => $item ) {
1291 if ( $menu_slug == $item[2] ) {
1301 * Remove an admin submenu
1305 * @param string $menu_slug The slug for the parent menu
1306 * @param string $submenu_slug The slug of the submenu
1307 * @return array|bool The removed submenu on success, False if not found
1309 function remove_submenu_page( $menu_slug, $submenu_slug ) {
1312 if ( !isset( $submenu[$menu_slug] ) )
1315 foreach ( $submenu[$menu_slug] as $i => $item ) {
1316 if ( $submenu_slug == $item[2] ) {
1317 unset( $submenu[$menu_slug][$i] );
1326 * Get the url to access a particular menu page based on the slug it was registered with.
1328 * If the slug hasn't been registered properly no url will be returned
1332 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1333 * @param bool $echo Whether or not to echo the url - default is true
1334 * @return string the url
1336 function menu_page_url($menu_slug, $echo = true) {
1337 global $_parent_pages;
1339 if ( isset( $_parent_pages[$menu_slug] ) ) {
1340 $parent_slug = $_parent_pages[$menu_slug];
1341 if ( $parent_slug && ! isset( $_parent_pages[$parent_slug] ) ) {
1342 $url = admin_url( add_query_arg( 'page', $menu_slug, $parent_slug ) );
1344 $url = admin_url( 'admin.php?page=' . $menu_slug );
1350 $url = esc_url($url);
1359 // Pluggable Menu Support -- Private
1362 function get_admin_page_parent( $parent = '' ) {
1363 global $parent_file;
1368 global $plugin_page;
1369 global $_wp_real_parent_file;
1370 global $_wp_menu_nopriv;
1371 global $_wp_submenu_nopriv;
1373 if ( !empty ( $parent ) && 'admin.php' != $parent ) {
1374 if ( isset( $_wp_real_parent_file[$parent] ) )
1375 $parent = $_wp_real_parent_file[$parent];
1380 if ( !empty ( $parent_file ) ) {
1381 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1382 $parent_file = $_wp_real_parent_file[$parent_file];
1384 return $parent_file;
1388 if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
1389 foreach ( (array)$menu as $parent_menu ) {
1390 if ( $parent_menu[2] == $plugin_page ) {
1391 $parent_file = $plugin_page;
1392 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1393 $parent_file = $_wp_real_parent_file[$parent_file];
1394 return $parent_file;
1397 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
1398 $parent_file = $plugin_page;
1399 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1400 $parent_file = $_wp_real_parent_file[$parent_file];
1401 return $parent_file;
1405 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
1406 $parent_file = $pagenow;
1407 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1408 $parent_file = $_wp_real_parent_file[$parent_file];
1409 return $parent_file;
1412 foreach (array_keys( (array)$submenu ) as $parent) {
1413 foreach ( $submenu[$parent] as $submenu_array ) {
1414 if ( isset( $_wp_real_parent_file[$parent] ) )
1415 $parent = $_wp_real_parent_file[$parent];
1416 if ( !empty($typenow) && ($submenu_array[2] == "$pagenow?post_type=$typenow") ) {
1417 $parent_file = $parent;
1419 } elseif ( $submenu_array[2] == $pagenow && empty($typenow) && ( empty($parent_file) || false === strpos($parent_file, '?') ) ) {
1420 $parent_file = $parent;
1423 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
1424 $parent_file = $parent;
1430 if ( empty($parent_file) )
1435 function get_admin_page_title() {
1440 global $plugin_page;
1443 if ( ! empty ( $title ) )
1446 $hook = get_plugin_page_hook( $plugin_page, $pagenow );
1448 $parent = $parent1 = get_admin_page_parent();
1450 if ( empty ( $parent) ) {
1451 foreach ( (array)$menu as $menu_array ) {
1452 if ( isset( $menu_array[3] ) ) {
1453 if ( $menu_array[2] == $pagenow ) {
1454 $title = $menu_array[3];
1455 return $menu_array[3];
1457 if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
1458 $title = $menu_array[3];
1459 return $menu_array[3];
1462 $title = $menu_array[0];
1467 foreach ( array_keys( $submenu ) as $parent ) {
1468 foreach ( $submenu[$parent] as $submenu_array ) {
1469 if ( isset( $plugin_page ) &&
1470 ( $plugin_page == $submenu_array[2] ) &&
1472 ( $parent == $pagenow ) ||
1473 ( $parent == $plugin_page ) ||
1474 ( $plugin_page == $hook ) ||
1475 ( $pagenow == 'admin.php' && $parent1 != $submenu_array[2] ) ||
1476 ( !empty($typenow) && $parent == $pagenow . '?post_type=' . $typenow)
1479 $title = $submenu_array[3];
1480 return $submenu_array[3];
1483 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
1486 if ( isset( $submenu_array[3] ) ) {
1487 $title = $submenu_array[3];
1488 return $submenu_array[3];
1490 $title = $submenu_array[0];
1495 if ( empty ( $title ) ) {
1496 foreach ( $menu as $menu_array ) {
1497 if ( isset( $plugin_page ) &&
1498 ( $plugin_page == $menu_array[2] ) &&
1499 ( $pagenow == 'admin.php' ) &&
1500 ( $parent1 == $menu_array[2] ) )
1502 $title = $menu_array[3];
1503 return $menu_array[3];
1512 function get_plugin_page_hook( $plugin_page, $parent_page ) {
1513 $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
1514 if ( has_action($hook) )
1520 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
1521 global $admin_page_hooks;
1523 $parent = get_admin_page_parent( $parent_page );
1525 $page_type = 'admin';
1526 if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
1527 if ( isset( $admin_page_hooks[$plugin_page] ) )
1528 $page_type = 'toplevel';
1530 if ( isset( $admin_page_hooks[$parent] ))
1531 $page_type = $admin_page_hooks[$parent];
1532 } else if ( isset( $admin_page_hooks[$parent] ) ) {
1533 $page_type = $admin_page_hooks[$parent];
1536 $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
1538 return $page_type . '_page_' . $plugin_name;
1541 function user_can_access_admin_page() {
1545 global $_wp_menu_nopriv;
1546 global $_wp_submenu_nopriv;
1547 global $plugin_page;
1548 global $_registered_pages;
1550 $parent = get_admin_page_parent();
1552 if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
1555 if ( isset( $plugin_page ) ) {
1556 if ( isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
1559 $hookname = get_plugin_page_hookname($plugin_page, $parent);
1561 if ( !isset($_registered_pages[$hookname]) )
1565 if ( empty( $parent) ) {
1566 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
1568 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
1570 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
1572 if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1574 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
1575 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
1577 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
1583 if ( isset( $plugin_page ) && ( $plugin_page == $parent ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1586 if ( isset( $submenu[$parent] ) ) {
1587 foreach ( $submenu[$parent] as $submenu_array ) {
1588 if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
1589 if ( current_user_can( $submenu_array[1] ))
1593 } else if ( $submenu_array[2] == $pagenow ) {
1594 if ( current_user_can( $submenu_array[1] ))
1602 foreach ( $menu as $menu_array ) {
1603 if ( $menu_array[2] == $parent) {
1604 if ( current_user_can( $menu_array[1] ))
1614 /* Whitelist functions */
1617 * Register a setting and its sanitization callback
1621 * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
1622 * Default whitelisted option key names include "general," "discussion," and "reading," among others.
1623 * @param string $option_name The name of an option to sanitize and save.
1624 * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
1627 function register_setting( $option_group, $option_name, $sanitize_callback = '' ) {
1628 global $new_whitelist_options;
1630 if ( 'misc' == $option_group ) {
1631 _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
1632 $option_group = 'general';
1635 if ( 'privacy' == $option_group ) {
1636 _deprecated_argument( __FUNCTION__, '3.5', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
1637 $option_group = 'reading';
1640 $new_whitelist_options[ $option_group ][] = $option_name;
1641 if ( $sanitize_callback != '' )
1642 add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1646 * Unregister a setting
1650 * @param unknown_type $option_group
1651 * @param unknown_type $option_name
1652 * @param unknown_type $sanitize_callback
1655 function unregister_setting( $option_group, $option_name, $sanitize_callback = '' ) {
1656 global $new_whitelist_options;
1658 if ( 'misc' == $option_group ) {
1659 _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
1660 $option_group = 'general';
1663 if ( 'privacy' == $option_group ) {
1664 _deprecated_argument( __FUNCTION__, '3.5', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
1665 $option_group = 'reading';
1668 $pos = array_search( $option_name, (array) $new_whitelist_options );
1669 if ( $pos !== false )
1670 unset( $new_whitelist_options[ $option_group ][ $pos ] );
1671 if ( $sanitize_callback != '' )
1672 remove_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1676 * {@internal Missing Short Description}}
1680 * @param unknown_type $options
1683 function option_update_filter( $options ) {
1684 global $new_whitelist_options;
1686 if ( is_array( $new_whitelist_options ) )
1687 $options = add_option_whitelist( $new_whitelist_options, $options );
1691 add_filter( 'whitelist_options', 'option_update_filter' );
1694 * {@internal Missing Short Description}}
1698 * @param unknown_type $new_options
1699 * @param unknown_type $options
1702 function add_option_whitelist( $new_options, $options = '' ) {
1703 if ( $options == '' )
1704 global $whitelist_options;
1706 $whitelist_options = $options;
1708 foreach ( $new_options as $page => $keys ) {
1709 foreach ( $keys as $key ) {
1710 if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
1711 $whitelist_options[ $page ] = array();
1712 $whitelist_options[ $page ][] = $key;
1714 $pos = array_search( $key, $whitelist_options[ $page ] );
1715 if ( $pos === false )
1716 $whitelist_options[ $page ][] = $key;
1721 return $whitelist_options;
1725 * {@internal Missing Short Description}}
1729 * @param unknown_type $del_options
1730 * @param unknown_type $options
1733 function remove_option_whitelist( $del_options, $options = '' ) {
1734 if ( $options == '' )
1735 global $whitelist_options;
1737 $whitelist_options = $options;
1739 foreach ( $del_options as $page => $keys ) {
1740 foreach ( $keys as $key ) {
1741 if ( isset($whitelist_options[ $page ]) && is_array($whitelist_options[ $page ]) ) {
1742 $pos = array_search( $key, $whitelist_options[ $page ] );
1743 if ( $pos !== false )
1744 unset( $whitelist_options[ $page ][ $pos ] );
1749 return $whitelist_options;
1753 * Output nonce, action, and option_page fields for a settings page.
1757 * @param string $option_group A settings group name. This should match the group name used in register_setting().
1759 function settings_fields($option_group) {
1760 echo "<input type='hidden' name='option_page' value='" . esc_attr($option_group) . "' />";
1761 echo '<input type="hidden" name="action" value="update" />';
1762 wp_nonce_field("$option_group-options");