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 If the returned data should have HTML markup applied
69 * @param bool $translate If the returned data should be translated
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 ( empty( $plugin_data['Network'] ) && ! empty( $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 //For backward compatibility by default Title is the same as Name.
99 $plugin_data['Title'] = $plugin_data['Name'];
101 if ( $markup || $translate )
102 $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
104 $plugin_data['AuthorName'] = $plugin_data['Author'];
109 function _get_plugin_data_markup_translate($plugin_file, $plugin_data, $markup = true, $translate = true) {
112 if ( $translate && ! empty($plugin_data['TextDomain']) ) {
113 if ( ! empty( $plugin_data['DomainPath'] ) )
114 load_plugin_textdomain($plugin_data['TextDomain'], false, dirname($plugin_file). $plugin_data['DomainPath']);
116 load_plugin_textdomain($plugin_data['TextDomain'], false, dirname($plugin_file));
118 foreach ( array('Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version') as $field )
119 $plugin_data[ $field ] = translate($plugin_data[ $field ], $plugin_data['TextDomain']);
122 $plugins_allowedtags = array(
123 'a' => array( 'href' => array(), 'title' => array() ),
124 'abbr' => array( 'title' => array() ),
125 'acronym' => array( 'title' => array() ),
131 $plugin_data['AuthorName'] = $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $plugins_allowedtags );
135 if ( ! empty($plugin_data['PluginURI']) && ! empty($plugin_data['Name']) )
136 $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . esc_attr__( 'Visit plugin homepage' ) . '">' . $plugin_data['Name'] . '</a>';
138 $plugin_data['Title'] = $plugin_data['Name'];
140 if ( ! empty($plugin_data['AuthorURI']) && ! empty($plugin_data['Author']) )
141 $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . esc_attr__( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>';
143 $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
144 if ( ! empty($plugin_data['Author']) )
145 $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s'), $plugin_data['Author'] ) . '.</cite>';
148 // Sanitize all displayed data. Author and AuthorName sanitized above.
149 $plugin_data['Title'] = wp_kses( $plugin_data['Title'], $plugins_allowedtags );
150 $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $plugins_allowedtags );
151 $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $plugins_allowedtags );
152 $plugin_data['Name'] = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
158 * Get a list of a plugin's files.
162 * @param string $plugin Plugin ID
163 * @return array List of files relative to the plugin root.
165 function get_plugin_files($plugin) {
166 $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
167 $dir = dirname($plugin_file);
168 $plugin_files = array($plugin);
169 if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) {
170 $plugins_dir = @ opendir( $dir );
171 if ( $plugins_dir ) {
172 while (($file = readdir( $plugins_dir ) ) !== false ) {
173 if ( substr($file, 0, 1) == '.' )
175 if ( is_dir( $dir . '/' . $file ) ) {
176 $plugins_subdir = @ opendir( $dir . '/' . $file );
177 if ( $plugins_subdir ) {
178 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
179 if ( substr($subfile, 0, 1) == '.' )
181 $plugin_files[] = plugin_basename("$dir/$file/$subfile");
183 @closedir( $plugins_subdir );
186 if ( plugin_basename("$dir/$file") != $plugin )
187 $plugin_files[] = plugin_basename("$dir/$file");
190 @closedir( $plugins_dir );
194 return $plugin_files;
198 * Check the plugins directory and retrieve all plugin files with plugin data.
200 * WordPress only supports plugin files in the base plugins directory
201 * (wp-content/plugins) and in one directory above the plugins directory
202 * (wp-content/plugins/my-plugin). The file it looks for has the plugin data and
203 * must be found in those two locations. It is recommended that do keep your
204 * plugin files in directories.
206 * The file with the plugin data is the file that will be included and therefore
207 * needs to have the main execution for the plugin. This does not mean
208 * everything must be contained in the file and it is recommended that the file
209 * be split for maintainability. Keep everything in one file for extreme
210 * optimization purposes.
214 * @param string $plugin_folder Optional. Relative path to single plugin folder.
215 * @return array Key is the plugin file path and the value is an array of the plugin data.
217 function get_plugins($plugin_folder = '') {
219 if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
220 $cache_plugins = array();
222 if ( isset($cache_plugins[ $plugin_folder ]) )
223 return $cache_plugins[ $plugin_folder ];
225 $wp_plugins = array ();
226 $plugin_root = WP_PLUGIN_DIR;
227 if ( !empty($plugin_folder) )
228 $plugin_root .= $plugin_folder;
230 // Files in wp-content/plugins directory
231 $plugins_dir = @ opendir( $plugin_root);
232 $plugin_files = array();
233 if ( $plugins_dir ) {
234 while (($file = readdir( $plugins_dir ) ) !== false ) {
235 if ( substr($file, 0, 1) == '.' )
237 if ( is_dir( $plugin_root.'/'.$file ) ) {
238 $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
239 if ( $plugins_subdir ) {
240 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
241 if ( substr($subfile, 0, 1) == '.' )
243 if ( substr($subfile, -4) == '.php' )
244 $plugin_files[] = "$file/$subfile";
248 if ( substr($file, -4) == '.php' )
249 $plugin_files[] = $file;
256 @closedir( $plugins_dir );
257 @closedir( $plugins_subdir );
259 if ( empty($plugin_files) )
262 foreach ( $plugin_files as $plugin_file ) {
263 if ( !is_readable( "$plugin_root/$plugin_file" ) )
266 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
268 if ( empty ( $plugin_data['Name'] ) )
271 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
274 uasort( $wp_plugins, '_sort_uname_callback' );
276 $cache_plugins[ $plugin_folder ] = $wp_plugins;
277 wp_cache_set('plugins', $cache_plugins, 'plugins');
283 * Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data.
285 * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins).
288 * @return array Key is the mu-plugin file path and the value is an array of the mu-plugin data.
290 function get_mu_plugins() {
291 $wp_plugins = array();
292 // Files in wp-content/mu-plugins directory
293 $plugin_files = array();
295 if ( ! is_dir( WPMU_PLUGIN_DIR ) )
297 if ( $plugins_dir = @ opendir( WPMU_PLUGIN_DIR ) ) {
298 while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
299 if ( substr( $file, -4 ) == '.php' )
300 $plugin_files[] = $file;
306 @closedir( $plugins_dir );
308 if ( empty($plugin_files) )
311 foreach ( $plugin_files as $plugin_file ) {
312 if ( !is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) )
315 $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
317 if ( empty ( $plugin_data['Name'] ) )
318 $plugin_data['Name'] = $plugin_file;
320 $wp_plugins[ $plugin_file ] = $plugin_data;
323 if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php') <= 30 ) // silence is golden
324 unset( $wp_plugins['index.php'] );
326 uasort( $wp_plugins, '_sort_uname_callback' );
332 * Callback to sort array by a 'Name' key.
337 function _sort_uname_callback( $a, $b ) {
338 return strnatcasecmp( $a['Name'], $b['Name'] );
342 * Check the wp-content directory and retrieve all drop-ins with any plugin data.
345 * @return array Key is the file path and the value is an array of the plugin data.
347 function get_dropins() {
349 $plugin_files = array();
351 $_dropins = _get_dropins();
353 // These exist in the wp-content directory
354 if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) {
355 while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
356 if ( isset( $_dropins[ $file ] ) )
357 $plugin_files[] = $file;
363 @closedir( $plugins_dir );
365 if ( empty($plugin_files) )
368 foreach ( $plugin_files as $plugin_file ) {
369 if ( !is_readable( WP_CONTENT_DIR . "/$plugin_file" ) )
371 $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
372 if ( empty( $plugin_data['Name'] ) )
373 $plugin_data['Name'] = $plugin_file;
374 $dropins[ $plugin_file ] = $plugin_data;
377 uksort( $dropins, 'strnatcasecmp' );
383 * Returns drop-ins that WordPress uses.
385 * Includes Multisite drop-ins only when is_multisite()
388 * @return array Key is file name. The value is an array, with the first value the
389 * purpose of the drop-in and the second value the name of the constant that must be
390 * true for the drop-in to be used, or true if no constant is required.
392 function _get_dropins() {
394 'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE
395 'db.php' => array( __( 'Custom database class.' ), true ), // auto on load
396 'db-error.php' => array( __( 'Custom database error message.' ), true ), // auto on error
397 'install.php' => array( __( 'Custom install script.' ), true ), // auto on install
398 'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // auto on maintenance
399 'object-cache.php' => array( __( 'External object cache.' ), true ), // auto on load
402 if ( is_multisite() ) {
403 $dropins['sunrise.php' ] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE
404 $dropins['blog-deleted.php' ] = array( __( 'Custom site deleted message.' ), true ); // auto on deleted blog
405 $dropins['blog-inactive.php' ] = array( __( 'Custom site inactive message.' ), true ); // auto on inactive blog
406 $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // auto on archived or spammed blog
413 * Check whether the plugin is active by checking the active_plugins list.
417 * @param string $plugin Base plugin path from plugins directory.
418 * @return bool True, if in the active plugins list. False, not in the list.
420 function is_plugin_active( $plugin ) {
421 return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
425 * Check whether the plugin is inactive.
427 * Reverse of is_plugin_active(). Used as a callback.
430 * @see is_plugin_active()
432 * @param string $plugin Base plugin path from plugins directory.
433 * @return bool True if inactive. False if active.
435 function is_plugin_inactive( $plugin ) {
436 return ! is_plugin_active( $plugin );
440 * Check whether the plugin is active for the entire network.
444 * @param string $plugin Base plugin path from plugins directory.
445 * @return bool True, if active for the network, otherwise false.
447 function is_plugin_active_for_network( $plugin ) {
448 if ( !is_multisite() )
451 $plugins = get_site_option( 'active_sitewide_plugins');
452 if ( isset($plugins[$plugin]) )
459 * Checks for "Network: true" in the plugin header to see if this should
460 * be activated only as a network wide plugin. The plugin would also work
461 * when Multisite is not enabled.
463 * Checks for "Site Wide Only: true" for backwards compatibility.
467 * @param string $plugin Plugin to check
468 * @return bool True if plugin is network only, false otherwise.
470 function is_network_only_plugin( $plugin ) {
471 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
473 return $plugin_data['Network'];
478 * Attempts activation of plugin in a "sandbox" and redirects on success.
480 * A plugin that is already activated will not attempt to be activated again.
482 * The way it works is by setting the redirection to the error before trying to
483 * include the plugin file. If the plugin fails, then the redirection will not
484 * be overwritten with the success message. Also, the options will not be
485 * updated and the activation hook will not be called on plugin error.
487 * It should be noted that in no way the below code will actually prevent errors
488 * within the file. The code should not be used elsewhere to replicate the
489 * "sandbox", which uses redirection to work.
492 * If any errors are found or text is outputted, then it will be captured to
493 * ensure that the success redirection will update the error redirection.
497 * @param string $plugin Plugin path to main plugin file with plugin data.
498 * @param string $redirect Optional. URL to redirect to.
499 * @param bool $network_wide Whether to enable the plugin for all sites in the
500 * network or just the current site. Multisite only. Default is false.
501 * @param bool $silent Prevent calling activation hooks. Optional, default is false.
502 * @return WP_Error|null WP_Error on invalid file or null on success.
504 function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
505 $plugin = plugin_basename( trim( $plugin ) );
507 if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) {
508 $network_wide = true;
509 $current = get_site_option( 'active_sitewide_plugins', array() );
511 $current = get_option( 'active_plugins', array() );
514 $valid = validate_plugin($plugin);
515 if ( is_wp_error($valid) )
518 if ( !in_array($plugin, $current) ) {
519 if ( !empty($redirect) )
520 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
522 include_once(WP_PLUGIN_DIR . '/' . $plugin);
525 do_action( 'activate_plugin', $plugin, $network_wide );
526 do_action( 'activate_' . $plugin, $network_wide );
529 if ( $network_wide ) {
530 $current[$plugin] = time();
531 update_site_option( 'active_sitewide_plugins', $current );
533 $current[] = $plugin;
535 update_option('active_plugins', $current);
539 do_action( 'activated_plugin', $plugin, $network_wide );
542 if ( ob_get_length() > 0 ) {
543 $output = ob_get_clean();
544 return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
553 * Deactivate a single plugin or multiple plugins.
555 * The deactivation hook is disabled by the plugin upgrader by using the $silent
560 * @param string|array $plugins Single plugin or list of plugins to deactivate.
561 * @param bool $silent Prevent calling deactivation hooks. Default is false.
563 function deactivate_plugins( $plugins, $silent = false ) {
564 if ( is_multisite() )
565 $network_current = get_site_option( 'active_sitewide_plugins', array() );
566 $current = get_option( 'active_plugins', array() );
567 $do_blog = $do_network = false;
569 foreach ( (array) $plugins as $plugin ) {
570 $plugin = plugin_basename( trim( $plugin ) );
571 if ( ! is_plugin_active($plugin) )
574 $network_wide = is_plugin_active_for_network( $plugin );
577 do_action( 'deactivate_plugin', $plugin, $network_wide );
579 if ( $network_wide ) {
581 unset( $network_current[ $plugin ] );
583 $key = array_search( $plugin, $current );
584 if ( false !== $key ) {
586 array_splice( $current, $key, 1 );
591 do_action( 'deactivate_' . $plugin, $network_wide );
592 do_action( 'deactivated_plugin', $plugin, $network_wide );
597 update_option('active_plugins', $current);
599 update_site_option( 'active_sitewide_plugins', $network_current );
603 * Activate multiple plugins.
605 * When WP_Error is returned, it does not mean that one of the plugins had
606 * errors. It means that one or more of the plugins file path was invalid.
608 * The execution will be halted as soon as one of the plugins has an error.
612 * @param string|array $plugins
613 * @param string $redirect Redirect to page after successful activation.
614 * @param bool $network_wide Whether to enable the plugin for all sites in the network.
615 * @param bool $silent Prevent calling activation hooks. Default is false.
616 * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
618 function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
619 if ( !is_array($plugins) )
620 $plugins = array($plugins);
623 foreach ( $plugins as $plugin ) {
624 if ( !empty($redirect) )
625 $redirect = add_query_arg('plugin', $plugin, $redirect);
626 $result = activate_plugin($plugin, $redirect, $network_wide, $silent);
627 if ( is_wp_error($result) )
628 $errors[$plugin] = $result;
631 if ( !empty($errors) )
632 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
638 * Remove directory and files of a plugin for a single or list of plugin(s).
640 * If the plugins parameter list is empty, false will be returned. True when
645 * @param array $plugins List of plugin
646 * @param string $redirect Redirect to page when complete.
649 function delete_plugins($plugins, $redirect = '' ) {
650 global $wp_filesystem;
652 if ( empty($plugins) )
656 foreach( $plugins as $plugin )
657 $checked[] = 'checked[]=' . $plugin;
660 $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-plugins');
661 if ( false === ($credentials = request_filesystem_credentials($url)) ) {
662 $data = ob_get_contents();
664 if ( ! empty($data) ){
665 include_once( ABSPATH . 'wp-admin/admin-header.php');
667 include( ABSPATH . 'wp-admin/admin-footer.php');
673 if ( ! WP_Filesystem($credentials) ) {
674 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
675 $data = ob_get_contents();
677 if ( ! empty($data) ){
678 include_once( ABSPATH . 'wp-admin/admin-header.php');
680 include( ABSPATH . 'wp-admin/admin-footer.php');
686 if ( ! is_object($wp_filesystem) )
687 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
689 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
690 return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
692 //Get the base plugin folder
693 $plugins_dir = $wp_filesystem->wp_plugins_dir();
694 if ( empty($plugins_dir) )
695 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
697 $plugins_dir = trailingslashit( $plugins_dir );
701 foreach( $plugins as $plugin_file ) {
702 // Run Uninstall hook
703 if ( is_uninstallable_plugin( $plugin_file ) )
704 uninstall_plugin($plugin_file);
706 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
707 // If plugin is in its own directory, recursively delete the directory.
708 if ( strpos($plugin_file, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
709 $deleted = $wp_filesystem->delete($this_plugin_dir, true);
711 $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
714 $errors[] = $plugin_file;
717 if ( ! empty($errors) )
718 return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s.'), implode(', ', $errors)) );
720 // Force refresh of plugin update information
721 if ( $current = get_site_transient('update_plugins') ) {
722 unset( $current->response[ $plugin_file ] );
723 set_site_transient('update_plugins', $current);
730 * Validate active plugins
732 * Validate all active plugins, deactivates invalid and
733 * returns an array of deactivated ones.
736 * @return array invalid plugins, plugin as key, error as value
738 function validate_active_plugins() {
739 $plugins = get_option( 'active_plugins', array() );
740 // validate vartype: array
741 if ( ! is_array( $plugins ) ) {
742 update_option( 'active_plugins', array() );
746 if ( is_multisite() && is_super_admin() ) {
747 $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
748 $plugins = array_merge( $plugins, array_keys( $network_plugins ) );
751 if ( empty( $plugins ) )
756 // invalid plugins get deactivated
757 foreach ( $plugins as $plugin ) {
758 $result = validate_plugin( $plugin );
759 if ( is_wp_error( $result ) ) {
760 $invalid[$plugin] = $result;
761 deactivate_plugins( $plugin, true );
768 * Validate the plugin path.
770 * Checks that the file exists and {@link validate_file() is valid file}.
774 * @param string $plugin Plugin Path
775 * @return WP_Error|int 0 on success, WP_Error on failure.
777 function validate_plugin($plugin) {
778 if ( validate_file($plugin) )
779 return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
780 if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
781 return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
783 $installed_plugins = get_plugins();
784 if ( ! isset($installed_plugins[$plugin]) )
785 return new WP_Error('no_plugin_header', __('The plugin does not have a valid header.'));
790 * Whether the plugin can be uninstalled.
794 * @param string $plugin Plugin path to check.
795 * @return bool Whether plugin can be uninstalled.
797 function is_uninstallable_plugin($plugin) {
798 $file = plugin_basename($plugin);
800 $uninstallable_plugins = (array) get_option('uninstall_plugins');
801 if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
808 * Uninstall a single plugin.
810 * Calls the uninstall hook, if it is available.
814 * @param string $plugin Relative plugin path from Plugin Directory.
816 function uninstall_plugin($plugin) {
817 $file = plugin_basename($plugin);
819 $uninstallable_plugins = (array) get_option('uninstall_plugins');
820 if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
821 if ( isset( $uninstallable_plugins[$file] ) ) {
822 unset($uninstallable_plugins[$file]);
823 update_option('uninstall_plugins', $uninstallable_plugins);
825 unset($uninstallable_plugins);
827 define('WP_UNINSTALL_PLUGIN', $file);
828 include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
833 if ( isset( $uninstallable_plugins[$file] ) ) {
834 $callable = $uninstallable_plugins[$file];
835 unset($uninstallable_plugins[$file]);
836 update_option('uninstall_plugins', $uninstallable_plugins);
837 unset($uninstallable_plugins);
839 include WP_PLUGIN_DIR . '/' . $file;
841 add_action( 'uninstall_' . $file, $callable );
842 do_action( 'uninstall_' . $file );
851 * Add a top level menu page
853 * This function takes a capability which will be used to determine whether
854 * or not a page is included in the menu.
856 * The function which is hooked in to handle the output of the page must check
857 * that the user has the required capability as well.
859 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
860 * @param string $menu_title The text to be used for the menu
861 * @param string $capability The capability required for this menu to be displayed to the user.
862 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
863 * @param callback $function The function to be called to output the content for this page.
864 * @param string $icon_url The url to the icon to be used for this menu
865 * @param int $position The position in the menu order this one should appear
867 * @return string The resulting page's hook_suffix
869 function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = NULL ) {
870 global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
872 $menu_slug = plugin_basename( $menu_slug );
874 $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title );
876 $hookname = get_plugin_page_hookname( $menu_slug, '' );
878 if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) )
879 add_action( $hookname, $function );
881 if ( empty($icon_url) )
882 $icon_url = esc_url( admin_url( 'images/generic.png' ) );
883 elseif ( is_ssl() && 0 === strpos($icon_url, 'http://') )
884 $icon_url = 'https://' . substr($icon_url, 7);
886 $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
888 if ( null === $position )
891 $menu[$position] = $new_menu;
893 $_registered_pages[$hookname] = true;
895 // No parent as top level
896 $_parent_pages[$menu_slug] = false;
902 * Add a top level menu page in the 'objects' section
904 * This function takes a capability which will be used to determine whether
905 * or not a page is included in the menu.
907 * The function which is hooked in to handle the output of the page must check
908 * that the user has the required capability as well.
910 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
911 * @param string $menu_title The text to be used for the menu
912 * @param string $capability The capability required for this menu to be displayed to the user.
913 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
914 * @param callback $function The function to be called to output the content for this page.
915 * @param string $icon_url The url to the icon to be used for this menu
917 * @return string The resulting page's hook_suffix
919 function add_object_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
920 global $_wp_last_object_menu;
922 $_wp_last_object_menu++;
924 return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_object_menu);
928 * Add a top level menu page in the 'utility' section
930 * This function takes a capability which will be used to determine whether
931 * or not a page is included in the menu.
933 * The function which is hooked in to handle the output of the page must check
934 * that the user has the required capability as well.
936 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
937 * @param string $menu_title The text to be used for the menu
938 * @param string $capability The capability required for this menu to be displayed to the user.
939 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
940 * @param callback $function The function to be called to output the content for this page.
941 * @param string $icon_url The url to the icon to be used for this menu
943 * @return string The resulting page's hook_suffix
945 function add_utility_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
946 global $_wp_last_utility_menu;
948 $_wp_last_utility_menu++;
950 return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_utility_menu);
954 * Add a sub menu page
956 * This function takes a capability which will be used to determine whether
957 * or not a page is included in the menu.
959 * The function which is hooked in to handle the output of the page must check
960 * that the user has the required capability as well.
962 * @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page)
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.
969 * @return string The resulting page's hook_suffix
971 function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
974 global $_wp_real_parent_file;
975 global $_wp_submenu_nopriv;
976 global $_registered_pages;
977 global $_parent_pages;
979 $menu_slug = plugin_basename( $menu_slug );
980 $parent_slug = plugin_basename( $parent_slug);
982 if ( isset( $_wp_real_parent_file[$parent_slug] ) )
983 $parent_slug = $_wp_real_parent_file[$parent_slug];
985 if ( !current_user_can( $capability ) ) {
986 $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true;
990 // If the parent doesn't already have a submenu, add a link to the parent
991 // as the first item in the submenu. If the submenu file is the same as the
992 // parent file someone is trying to link back to the parent manually. In
993 // this case, don't automatically add a link back to avoid duplication.
994 if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) {
995 foreach ( (array)$menu as $parent_menu ) {
996 if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) )
997 $submenu[$parent_slug][] = $parent_menu;
1001 $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title );
1003 $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);
1004 if (!empty ( $function ) && !empty ( $hookname ))
1005 add_action( $hookname, $function );
1007 $_registered_pages[$hookname] = true;
1008 // backwards-compatibility for plugins using add_management page. See wp-admin/admin.php for redirect from edit.php to tools.php
1009 if ( 'tools.php' == $parent_slug )
1010 $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true;
1012 // No parent as top level
1013 $_parent_pages[$menu_slug] = $parent_slug;
1019 * Add sub menu page to the tools main menu.
1021 * This function takes a capability which will be used to determine whether
1022 * or not a page is included in the menu.
1024 * The function which is hooked in to handle the output of the page must check
1025 * that the user has the required capability as well.
1027 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1028 * @param string $menu_title The text to be used for the menu
1029 * @param string $capability The capability required for this menu to be displayed to the user.
1030 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1031 * @param callback $function The function to be called to output the content for this page.
1033 * @return string The resulting page's hook_suffix
1035 function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1036 return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1040 * Add sub menu page to the options main menu.
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 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1049 * @param string $menu_title The text to be used for the menu
1050 * @param string $capability The capability required for this menu to be displayed to the user.
1051 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1052 * @param callback $function The function to be called to output the content for this page.
1054 * @return string The resulting page's hook_suffix
1056 function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1057 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1061 * Add sub menu page to the themes main menu.
1063 * This function takes a capability which will be used to determine whether
1064 * or not a page is included in the menu.
1066 * The function which is hooked in to handle the output of the page must check
1067 * that the user has the required capability as well.
1069 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1070 * @param string $menu_title The text to be used for the menu
1071 * @param string $capability The capability required for this menu to be displayed to the user.
1072 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1073 * @param callback $function The function to be called to output the content for this page.
1075 * @return string The resulting page's hook_suffix
1077 function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1078 return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1082 * Add sub menu page to the plugins main menu.
1084 * This function takes a capability which will be used to determine whether
1085 * or not a page is included in the menu.
1087 * The function which is hooked in to handle the output of the page must check
1088 * that the user has the required capability as well.
1090 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1091 * @param string $menu_title The text to be used for the menu
1092 * @param string $capability The capability required for this menu to be displayed to the user.
1093 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1094 * @param callback $function The function to be called to output the content for this page.
1096 * @return string The resulting page's hook_suffix
1098 function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1099 return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1103 * Add sub menu page to the Users/Profile main menu.
1105 * This function takes a capability which will be used to determine whether
1106 * or not a page is included in the menu.
1108 * The function which is hooked in to handle the output of the page must check
1109 * that the user has the required capability as well.
1111 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1112 * @param string $menu_title The text to be used for the menu
1113 * @param string $capability The capability required for this menu to be displayed to the user.
1114 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1115 * @param callback $function The function to be called to output the content for this page.
1117 * @return string The resulting page's hook_suffix
1119 function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1120 if ( current_user_can('edit_users') )
1121 $parent = 'users.php';
1123 $parent = 'profile.php';
1124 return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
1127 * Add sub menu page to the Dashboard main menu.
1129 * This function takes a capability which will be used to determine whether
1130 * or not a page is included in the menu.
1132 * The function which is hooked in to handle the output of the page must check
1133 * that the user has the required capability as well.
1135 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1136 * @param string $menu_title The text to be used for the menu
1137 * @param string $capability The capability required for this menu to be displayed to the user.
1138 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1139 * @param callback $function The function to be called to output the content for this page.
1141 * @return string The resulting page's hook_suffix
1143 function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1144 return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1148 * Add sub menu page to the posts main menu.
1150 * This function takes a capability which will be used to determine whether
1151 * or not a page is included in the menu.
1153 * The function which is hooked in to handle the output of the page must check
1154 * that the user has the required capability as well.
1156 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1157 * @param string $menu_title The text to be used for the menu
1158 * @param string $capability The capability required for this menu to be displayed to the user.
1159 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1160 * @param callback $function The function to be called to output the content for this page.
1162 * @return string The resulting page's hook_suffix
1164 function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1165 return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1169 * Add sub menu page to the media main menu.
1171 * This function takes a capability which will be used to determine whether
1172 * or not a page is included in the menu.
1174 * The function which is hooked in to handle the output of the page must check
1175 * that the user has the required capability as well.
1177 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1178 * @param string $menu_title The text to be used for the menu
1179 * @param string $capability The capability required for this menu to be displayed to the user.
1180 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1181 * @param callback $function The function to be called to output the content for this page.
1183 * @return string The resulting page's hook_suffix
1185 function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1186 return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1190 * Add sub menu page to the links main menu.
1192 * This function takes a capability which will be used to determine whether
1193 * or not a page is included in the menu.
1195 * The function which is hooked in to handle the output of the page must check
1196 * that the user has the required capability as well.
1198 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1199 * @param string $menu_title The text to be used for the menu
1200 * @param string $capability The capability required for this menu to be displayed to the user.
1201 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1202 * @param callback $function The function to be called to output the content for this page.
1204 * @return string The resulting page's hook_suffix
1206 function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1207 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1211 * Add sub menu page to the pages main menu.
1213 * This function takes a capability which will be used to determine whether
1214 * or not a page is included in the menu.
1216 * The function which is hooked in to handle the output of the page must check
1217 * that the user has the required capability as well.
1219 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1220 * @param string $menu_title The text to be used for the menu
1221 * @param string $capability The capability required for this menu to be displayed to the user.
1222 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1223 * @param callback $function The function to be called to output the content for this page.
1225 * @return string The resulting page's hook_suffix
1227 function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1228 return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function );
1232 * Add sub menu page to the comments main menu.
1234 * This function takes a capability which will be used to determine whether
1235 * or not a page is included in the menu.
1237 * The function which is hooked in to handle the output of the page must check
1238 * that the user has the required capability as well.
1240 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1241 * @param string $menu_title The text to be used for the menu
1242 * @param string $capability The capability required for this menu to be displayed to the user.
1243 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1244 * @param callback $function The function to be called to output the content for this page.
1246 * @return string The resulting page's hook_suffix
1248 function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1249 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1254 * Remove a top level admin menu
1258 * @param string $menu_slug The slug of the menu
1259 * @return array|bool The removed menu on success, False if not found
1261 function remove_menu_page( $menu_slug ) {
1264 foreach ( $menu as $i => $item ) {
1265 if ( $menu_slug == $item[2] ) {
1275 * Remove an admin submenu
1279 * @param string $menu_slug The slug for the parent menu
1280 * @param string $submenu_slug The slug of the submenu
1281 * @return array|bool The removed submenu on success, False if not found
1283 function remove_submenu_page( $menu_slug, $submenu_slug ) {
1286 if ( !isset( $submenu[$menu_slug] ) )
1289 foreach ( $submenu[$menu_slug] as $i => $item ) {
1290 if ( $submenu_slug == $item[2] ) {
1291 unset( $submenu[$menu_slug][$i] );
1300 * Get the url to access a particular menu page based on the slug it was registered with.
1302 * If the slug hasn't been registered properly no url will be returned
1306 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1307 * @param bool $echo Whether or not to echo the url - default is true
1308 * @return string the url
1310 function menu_page_url($menu_slug, $echo = true) {
1311 global $_parent_pages;
1313 if ( isset( $_parent_pages[$menu_slug] ) ) {
1314 $parent_slug = $_parent_pages[$menu_slug];
1315 if ( $parent_slug && ! isset( $_parent_pages[$parent_slug] ) ) {
1316 $url = admin_url( add_query_arg( 'page', $menu_slug, $parent_slug ) );
1318 $url = admin_url( 'admin.php?page=' . $menu_slug );
1324 $url = esc_url($url);
1333 // Pluggable Menu Support -- Private
1336 function get_admin_page_parent( $parent = '' ) {
1337 global $parent_file;
1342 global $plugin_page;
1343 global $_wp_real_parent_file;
1344 global $_wp_menu_nopriv;
1345 global $_wp_submenu_nopriv;
1347 if ( !empty ( $parent ) && 'admin.php' != $parent ) {
1348 if ( isset( $_wp_real_parent_file[$parent] ) )
1349 $parent = $_wp_real_parent_file[$parent];
1354 if ( !empty ( $parent_file ) ) {
1355 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1356 $parent_file = $_wp_real_parent_file[$parent_file];
1358 return $parent_file;
1362 if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
1363 foreach ( (array)$menu as $parent_menu ) {
1364 if ( $parent_menu[2] == $plugin_page ) {
1365 $parent_file = $plugin_page;
1366 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1367 $parent_file = $_wp_real_parent_file[$parent_file];
1368 return $parent_file;
1371 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
1372 $parent_file = $plugin_page;
1373 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1374 $parent_file = $_wp_real_parent_file[$parent_file];
1375 return $parent_file;
1379 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
1380 $parent_file = $pagenow;
1381 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1382 $parent_file = $_wp_real_parent_file[$parent_file];
1383 return $parent_file;
1386 foreach (array_keys( (array)$submenu ) as $parent) {
1387 foreach ( $submenu[$parent] as $submenu_array ) {
1388 if ( isset( $_wp_real_parent_file[$parent] ) )
1389 $parent = $_wp_real_parent_file[$parent];
1390 if ( !empty($typenow) && ($submenu_array[2] == "$pagenow?post_type=$typenow") ) {
1391 $parent_file = $parent;
1393 } elseif ( $submenu_array[2] == $pagenow && empty($typenow) && ( empty($parent_file) || false === strpos($parent_file, '?') ) ) {
1394 $parent_file = $parent;
1397 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
1398 $parent_file = $parent;
1404 if ( empty($parent_file) )
1409 function get_admin_page_title() {
1414 global $plugin_page;
1417 if ( ! empty ( $title ) )
1420 $hook = get_plugin_page_hook( $plugin_page, $pagenow );
1422 $parent = $parent1 = get_admin_page_parent();
1424 if ( empty ( $parent) ) {
1425 foreach ( (array)$menu as $menu_array ) {
1426 if ( isset( $menu_array[3] ) ) {
1427 if ( $menu_array[2] == $pagenow ) {
1428 $title = $menu_array[3];
1429 return $menu_array[3];
1431 if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
1432 $title = $menu_array[3];
1433 return $menu_array[3];
1436 $title = $menu_array[0];
1441 foreach ( array_keys( $submenu ) as $parent ) {
1442 foreach ( $submenu[$parent] as $submenu_array ) {
1443 if ( isset( $plugin_page ) &&
1444 ( $plugin_page == $submenu_array[2] ) &&
1446 ( $parent == $pagenow ) ||
1447 ( $parent == $plugin_page ) ||
1448 ( $plugin_page == $hook ) ||
1449 ( $pagenow == 'admin.php' && $parent1 != $submenu_array[2] ) ||
1450 ( !empty($typenow) && $parent == $pagenow . '?post_type=' . $typenow)
1453 $title = $submenu_array[3];
1454 return $submenu_array[3];
1457 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
1460 if ( isset( $submenu_array[3] ) ) {
1461 $title = $submenu_array[3];
1462 return $submenu_array[3];
1464 $title = $submenu_array[0];
1469 if ( empty ( $title ) ) {
1470 foreach ( $menu as $menu_array ) {
1471 if ( isset( $plugin_page ) &&
1472 ( $plugin_page == $menu_array[2] ) &&
1473 ( $pagenow == 'admin.php' ) &&
1474 ( $parent1 == $menu_array[2] ) )
1476 $title = $menu_array[3];
1477 return $menu_array[3];
1486 function get_plugin_page_hook( $plugin_page, $parent_page ) {
1487 $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
1488 if ( has_action($hook) )
1494 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
1495 global $admin_page_hooks;
1497 $parent = get_admin_page_parent( $parent_page );
1499 $page_type = 'admin';
1500 if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
1501 if ( isset( $admin_page_hooks[$plugin_page] ) )
1502 $page_type = 'toplevel';
1504 if ( isset( $admin_page_hooks[$parent] ))
1505 $page_type = $admin_page_hooks[$parent];
1506 } else if ( isset( $admin_page_hooks[$parent] ) ) {
1507 $page_type = $admin_page_hooks[$parent];
1510 $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
1512 return $page_type . '_page_' . $plugin_name;
1515 function user_can_access_admin_page() {
1519 global $_wp_menu_nopriv;
1520 global $_wp_submenu_nopriv;
1521 global $plugin_page;
1522 global $_registered_pages;
1524 $parent = get_admin_page_parent();
1526 if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
1529 if ( isset( $plugin_page ) ) {
1530 if ( isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
1533 $hookname = get_plugin_page_hookname($plugin_page, $parent);
1535 if ( !isset($_registered_pages[$hookname]) )
1539 if ( empty( $parent) ) {
1540 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
1542 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
1544 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
1546 if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1548 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
1549 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
1551 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
1557 if ( isset( $plugin_page ) && ( $plugin_page == $parent ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1560 if ( isset( $submenu[$parent] ) ) {
1561 foreach ( $submenu[$parent] as $submenu_array ) {
1562 if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
1563 if ( current_user_can( $submenu_array[1] ))
1567 } else if ( $submenu_array[2] == $pagenow ) {
1568 if ( current_user_can( $submenu_array[1] ))
1576 foreach ( $menu as $menu_array ) {
1577 if ( $menu_array[2] == $parent) {
1578 if ( current_user_can( $menu_array[1] ))
1588 /* Whitelist functions */
1591 * Register a setting and its sanitization callback
1595 * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
1596 * Default whitelisted option key names include "general," "discussion," and "reading," among others.
1597 * @param string $option_name The name of an option to sanitize and save.
1598 * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
1601 function register_setting( $option_group, $option_name, $sanitize_callback = '' ) {
1602 global $new_whitelist_options;
1604 if ( 'misc' == $option_group ) {
1605 _deprecated_argument( __FUNCTION__, '3.0', __( 'The miscellaneous options group has been removed. Use another settings group.' ) );
1606 $option_group = 'general';
1609 $new_whitelist_options[ $option_group ][] = $option_name;
1610 if ( $sanitize_callback != '' )
1611 add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1615 * Unregister a setting
1619 * @param unknown_type $option_group
1620 * @param unknown_type $option_name
1621 * @param unknown_type $sanitize_callback
1624 function unregister_setting( $option_group, $option_name, $sanitize_callback = '' ) {
1625 global $new_whitelist_options;
1627 if ( 'misc' == $option_group ) {
1628 _deprecated_argument( __FUNCTION__, '3.0', __( 'The miscellaneous options group has been removed. Use another settings group.' ) );
1629 $option_group = 'general';
1632 $pos = array_search( $option_name, (array) $new_whitelist_options );
1633 if ( $pos !== false )
1634 unset( $new_whitelist_options[ $option_group ][ $pos ] );
1635 if ( $sanitize_callback != '' )
1636 remove_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1640 * {@internal Missing Short Description}}
1644 * @param unknown_type $options
1647 function option_update_filter( $options ) {
1648 global $new_whitelist_options;
1650 if ( is_array( $new_whitelist_options ) )
1651 $options = add_option_whitelist( $new_whitelist_options, $options );
1655 add_filter( 'whitelist_options', 'option_update_filter' );
1658 * {@internal Missing Short Description}}
1662 * @param unknown_type $new_options
1663 * @param unknown_type $options
1666 function add_option_whitelist( $new_options, $options = '' ) {
1667 if ( $options == '' )
1668 global $whitelist_options;
1670 $whitelist_options = $options;
1672 foreach ( $new_options as $page => $keys ) {
1673 foreach ( $keys as $key ) {
1674 if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
1675 $whitelist_options[ $page ] = array();
1676 $whitelist_options[ $page ][] = $key;
1678 $pos = array_search( $key, $whitelist_options[ $page ] );
1679 if ( $pos === false )
1680 $whitelist_options[ $page ][] = $key;
1685 return $whitelist_options;
1689 * {@internal Missing Short Description}}
1693 * @param unknown_type $del_options
1694 * @param unknown_type $options
1697 function remove_option_whitelist( $del_options, $options = '' ) {
1698 if ( $options == '' )
1699 global $whitelist_options;
1701 $whitelist_options = $options;
1703 foreach ( $del_options as $page => $keys ) {
1704 foreach ( $keys as $key ) {
1705 if ( isset($whitelist_options[ $page ]) && is_array($whitelist_options[ $page ]) ) {
1706 $pos = array_search( $key, $whitelist_options[ $page ] );
1707 if ( $pos !== false )
1708 unset( $whitelist_options[ $page ][ $pos ] );
1713 return $whitelist_options;
1717 * Output nonce, action, and option_page fields for a settings page.
1721 * @param string $option_group A settings group name. This should match the group name used in register_setting().
1723 function settings_fields($option_group) {
1724 echo "<input type='hidden' name='option_page' value='" . esc_attr($option_group) . "' />";
1725 echo '<input type="hidden" name="action" value="update" />';
1726 wp_nonce_field("$option_group-options");