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";
246 closedir( $plugins_subdir );
249 if ( substr($file, -4) == '.php' )
250 $plugin_files[] = $file;
253 closedir( $plugins_dir );
256 if ( empty($plugin_files) )
259 foreach ( $plugin_files as $plugin_file ) {
260 if ( !is_readable( "$plugin_root/$plugin_file" ) )
263 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
265 if ( empty ( $plugin_data['Name'] ) )
268 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
271 uasort( $wp_plugins, '_sort_uname_callback' );
273 $cache_plugins[ $plugin_folder ] = $wp_plugins;
274 wp_cache_set('plugins', $cache_plugins, 'plugins');
280 * Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data.
282 * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins).
285 * @return array Key is the mu-plugin file path and the value is an array of the mu-plugin data.
287 function get_mu_plugins() {
288 $wp_plugins = array();
289 // Files in wp-content/mu-plugins directory
290 $plugin_files = array();
292 if ( ! is_dir( WPMU_PLUGIN_DIR ) )
294 if ( $plugins_dir = @ opendir( WPMU_PLUGIN_DIR ) ) {
295 while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
296 if ( substr( $file, -4 ) == '.php' )
297 $plugin_files[] = $file;
303 @closedir( $plugins_dir );
305 if ( empty($plugin_files) )
308 foreach ( $plugin_files as $plugin_file ) {
309 if ( !is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) )
312 $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
314 if ( empty ( $plugin_data['Name'] ) )
315 $plugin_data['Name'] = $plugin_file;
317 $wp_plugins[ $plugin_file ] = $plugin_data;
320 if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php') <= 30 ) // silence is golden
321 unset( $wp_plugins['index.php'] );
323 uasort( $wp_plugins, '_sort_uname_callback' );
329 * Callback to sort array by a 'Name' key.
334 function _sort_uname_callback( $a, $b ) {
335 return strnatcasecmp( $a['Name'], $b['Name'] );
339 * Check the wp-content directory and retrieve all drop-ins with any plugin data.
342 * @return array Key is the file path and the value is an array of the plugin data.
344 function get_dropins() {
346 $plugin_files = array();
348 $_dropins = _get_dropins();
350 // These exist in the wp-content directory
351 if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) {
352 while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
353 if ( isset( $_dropins[ $file ] ) )
354 $plugin_files[] = $file;
360 @closedir( $plugins_dir );
362 if ( empty($plugin_files) )
365 foreach ( $plugin_files as $plugin_file ) {
366 if ( !is_readable( WP_CONTENT_DIR . "/$plugin_file" ) )
368 $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
369 if ( empty( $plugin_data['Name'] ) )
370 $plugin_data['Name'] = $plugin_file;
371 $dropins[ $plugin_file ] = $plugin_data;
374 uksort( $dropins, 'strnatcasecmp' );
380 * Returns drop-ins that WordPress uses.
382 * Includes Multisite drop-ins only when is_multisite()
385 * @return array Key is file name. The value is an array, with the first value the
386 * purpose of the drop-in and the second value the name of the constant that must be
387 * true for the drop-in to be used, or true if no constant is required.
389 function _get_dropins() {
391 'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE
392 'db.php' => array( __( 'Custom database class.' ), true ), // auto on load
393 'db-error.php' => array( __( 'Custom database error message.' ), true ), // auto on error
394 'install.php' => array( __( 'Custom install script.' ), true ), // auto on install
395 'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // auto on maintenance
396 'object-cache.php' => array( __( 'External object cache.' ), true ), // auto on load
399 if ( is_multisite() ) {
400 $dropins['sunrise.php' ] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE
401 $dropins['blog-deleted.php' ] = array( __( 'Custom site deleted message.' ), true ); // auto on deleted blog
402 $dropins['blog-inactive.php' ] = array( __( 'Custom site inactive message.' ), true ); // auto on inactive blog
403 $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // auto on archived or spammed blog
410 * Check whether the plugin is active by checking the active_plugins list.
414 * @param string $plugin Base plugin path from plugins directory.
415 * @return bool True, if in the active plugins list. False, not in the list.
417 function is_plugin_active( $plugin ) {
418 return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
422 * Check whether the plugin is inactive.
424 * Reverse of is_plugin_active(). Used as a callback.
427 * @see is_plugin_active()
429 * @param string $plugin Base plugin path from plugins directory.
430 * @return bool True if inactive. False if active.
432 function is_plugin_inactive( $plugin ) {
433 return ! is_plugin_active( $plugin );
437 * Check whether the plugin is active for the entire network.
441 * @param string $plugin Base plugin path from plugins directory.
442 * @return bool True, if active for the network, otherwise false.
444 function is_plugin_active_for_network( $plugin ) {
445 if ( !is_multisite() )
448 $plugins = get_site_option( 'active_sitewide_plugins');
449 if ( isset($plugins[$plugin]) )
456 * Checks for "Network: true" in the plugin header to see if this should
457 * be activated only as a network wide plugin. The plugin would also work
458 * when Multisite is not enabled.
460 * Checks for "Site Wide Only: true" for backwards compatibility.
464 * @param string $plugin Plugin to check
465 * @return bool True if plugin is network only, false otherwise.
467 function is_network_only_plugin( $plugin ) {
468 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
470 return $plugin_data['Network'];
475 * Attempts activation of plugin in a "sandbox" and redirects on success.
477 * A plugin that is already activated will not attempt to be activated again.
479 * The way it works is by setting the redirection to the error before trying to
480 * include the plugin file. If the plugin fails, then the redirection will not
481 * be overwritten with the success message. Also, the options will not be
482 * updated and the activation hook will not be called on plugin error.
484 * It should be noted that in no way the below code will actually prevent errors
485 * within the file. The code should not be used elsewhere to replicate the
486 * "sandbox", which uses redirection to work.
489 * If any errors are found or text is outputted, then it will be captured to
490 * ensure that the success redirection will update the error redirection.
494 * @param string $plugin Plugin path to main plugin file with plugin data.
495 * @param string $redirect Optional. URL to redirect to.
496 * @param bool $network_wide Whether to enable the plugin for all sites in the
497 * network or just the current site. Multisite only. Default is false.
498 * @param bool $silent Prevent calling activation hooks. Optional, default is false.
499 * @return WP_Error|null WP_Error on invalid file or null on success.
501 function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
502 $plugin = plugin_basename( trim( $plugin ) );
504 if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) {
505 $network_wide = true;
506 $current = get_site_option( 'active_sitewide_plugins', array() );
508 $current = get_option( 'active_plugins', array() );
511 $valid = validate_plugin($plugin);
512 if ( is_wp_error($valid) )
515 if ( !in_array($plugin, $current) ) {
516 if ( !empty($redirect) )
517 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
519 include_once(WP_PLUGIN_DIR . '/' . $plugin);
522 do_action( 'activate_plugin', $plugin, $network_wide );
523 do_action( 'activate_' . $plugin, $network_wide );
526 if ( $network_wide ) {
527 $current[$plugin] = time();
528 update_site_option( 'active_sitewide_plugins', $current );
530 $current[] = $plugin;
532 update_option('active_plugins', $current);
536 do_action( 'activated_plugin', $plugin, $network_wide );
539 if ( ob_get_length() > 0 ) {
540 $output = ob_get_clean();
541 return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
550 * Deactivate a single plugin or multiple plugins.
552 * The deactivation hook is disabled by the plugin upgrader by using the $silent
557 * @param string|array $plugins Single plugin or list of plugins to deactivate.
558 * @param bool $silent Prevent calling deactivation hooks. Default is false.
560 function deactivate_plugins( $plugins, $silent = false ) {
561 if ( is_multisite() )
562 $network_current = get_site_option( 'active_sitewide_plugins', array() );
563 $current = get_option( 'active_plugins', array() );
564 $do_blog = $do_network = false;
566 foreach ( (array) $plugins as $plugin ) {
567 $plugin = plugin_basename( trim( $plugin ) );
568 if ( ! is_plugin_active($plugin) )
571 $network_wide = is_plugin_active_for_network( $plugin );
574 do_action( 'deactivate_plugin', $plugin, $network_wide );
576 if ( $network_wide ) {
578 unset( $network_current[ $plugin ] );
580 $key = array_search( $plugin, $current );
581 if ( false !== $key ) {
583 array_splice( $current, $key, 1 );
588 do_action( 'deactivate_' . $plugin, $network_wide );
589 do_action( 'deactivated_plugin', $plugin, $network_wide );
594 update_option('active_plugins', $current);
596 update_site_option( 'active_sitewide_plugins', $network_current );
600 * Activate multiple plugins.
602 * When WP_Error is returned, it does not mean that one of the plugins had
603 * errors. It means that one or more of the plugins file path was invalid.
605 * The execution will be halted as soon as one of the plugins has an error.
609 * @param string|array $plugins
610 * @param string $redirect Redirect to page after successful activation.
611 * @param bool $network_wide Whether to enable the plugin for all sites in the network.
612 * @param bool $silent Prevent calling activation hooks. Default is false.
613 * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
615 function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
616 if ( !is_array($plugins) )
617 $plugins = array($plugins);
620 foreach ( $plugins as $plugin ) {
621 if ( !empty($redirect) )
622 $redirect = add_query_arg('plugin', $plugin, $redirect);
623 $result = activate_plugin($plugin, $redirect, $network_wide, $silent);
624 if ( is_wp_error($result) )
625 $errors[$plugin] = $result;
628 if ( !empty($errors) )
629 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
635 * Remove directory and files of a plugin for a single or list of plugin(s).
637 * If the plugins parameter list is empty, false will be returned. True when
642 * @param array $plugins List of plugin
643 * @param string $redirect Redirect to page when complete.
646 function delete_plugins($plugins, $redirect = '' ) {
647 global $wp_filesystem;
649 if ( empty($plugins) )
653 foreach( $plugins as $plugin )
654 $checked[] = 'checked[]=' . $plugin;
657 $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-plugins');
658 if ( false === ($credentials = request_filesystem_credentials($url)) ) {
659 $data = ob_get_contents();
661 if ( ! empty($data) ){
662 include_once( ABSPATH . 'wp-admin/admin-header.php');
664 include( ABSPATH . 'wp-admin/admin-footer.php');
670 if ( ! WP_Filesystem($credentials) ) {
671 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
672 $data = ob_get_contents();
674 if ( ! empty($data) ){
675 include_once( ABSPATH . 'wp-admin/admin-header.php');
677 include( ABSPATH . 'wp-admin/admin-footer.php');
683 if ( ! is_object($wp_filesystem) )
684 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
686 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
687 return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
689 //Get the base plugin folder
690 $plugins_dir = $wp_filesystem->wp_plugins_dir();
691 if ( empty($plugins_dir) )
692 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
694 $plugins_dir = trailingslashit( $plugins_dir );
698 foreach( $plugins as $plugin_file ) {
699 // Run Uninstall hook
700 if ( is_uninstallable_plugin( $plugin_file ) )
701 uninstall_plugin($plugin_file);
703 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
704 // If plugin is in its own directory, recursively delete the directory.
705 if ( strpos($plugin_file, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory separator AND that its not the root plugin folder
706 $deleted = $wp_filesystem->delete($this_plugin_dir, true);
708 $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
711 $errors[] = $plugin_file;
714 if ( ! empty($errors) )
715 return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s.'), implode(', ', $errors)) );
717 // Force refresh of plugin update information
718 if ( $current = get_site_transient('update_plugins') ) {
719 unset( $current->response[ $plugin_file ] );
720 set_site_transient('update_plugins', $current);
727 * Validate active plugins
729 * Validate all active plugins, deactivates invalid and
730 * returns an array of deactivated ones.
733 * @return array invalid plugins, plugin as key, error as value
735 function validate_active_plugins() {
736 $plugins = get_option( 'active_plugins', array() );
737 // validate vartype: array
738 if ( ! is_array( $plugins ) ) {
739 update_option( 'active_plugins', array() );
743 if ( is_multisite() && is_super_admin() ) {
744 $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
745 $plugins = array_merge( $plugins, array_keys( $network_plugins ) );
748 if ( empty( $plugins ) )
753 // invalid plugins get deactivated
754 foreach ( $plugins as $plugin ) {
755 $result = validate_plugin( $plugin );
756 if ( is_wp_error( $result ) ) {
757 $invalid[$plugin] = $result;
758 deactivate_plugins( $plugin, true );
765 * Validate the plugin path.
767 * Checks that the file exists and {@link validate_file() is valid file}.
771 * @param string $plugin Plugin Path
772 * @return WP_Error|int 0 on success, WP_Error on failure.
774 function validate_plugin($plugin) {
775 if ( validate_file($plugin) )
776 return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
777 if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
778 return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
780 $installed_plugins = get_plugins();
781 if ( ! isset($installed_plugins[$plugin]) )
782 return new WP_Error('no_plugin_header', __('The plugin does not have a valid header.'));
787 * Whether the plugin can be uninstalled.
791 * @param string $plugin Plugin path to check.
792 * @return bool Whether plugin can be uninstalled.
794 function is_uninstallable_plugin($plugin) {
795 $file = plugin_basename($plugin);
797 $uninstallable_plugins = (array) get_option('uninstall_plugins');
798 if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
805 * Uninstall a single plugin.
807 * Calls the uninstall hook, if it is available.
811 * @param string $plugin Relative plugin path from Plugin Directory.
813 function uninstall_plugin($plugin) {
814 $file = plugin_basename($plugin);
816 $uninstallable_plugins = (array) get_option('uninstall_plugins');
817 if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
818 if ( isset( $uninstallable_plugins[$file] ) ) {
819 unset($uninstallable_plugins[$file]);
820 update_option('uninstall_plugins', $uninstallable_plugins);
822 unset($uninstallable_plugins);
824 define('WP_UNINSTALL_PLUGIN', $file);
825 include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
830 if ( isset( $uninstallable_plugins[$file] ) ) {
831 $callable = $uninstallable_plugins[$file];
832 unset($uninstallable_plugins[$file]);
833 update_option('uninstall_plugins', $uninstallable_plugins);
834 unset($uninstallable_plugins);
836 include WP_PLUGIN_DIR . '/' . $file;
838 add_action( 'uninstall_' . $file, $callable );
839 do_action( 'uninstall_' . $file );
848 * Add a top level menu page
850 * This function takes a capability which will be used to determine whether
851 * or not a page is included in the menu.
853 * The function which is hooked in to handle the output of the page must check
854 * that the user has the required capability as well.
856 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
857 * @param string $menu_title The text to be used for the menu
858 * @param string $capability The capability required for this menu to be displayed to the user.
859 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
860 * @param callback $function The function to be called to output the content for this page.
861 * @param string $icon_url The url to the icon to be used for this menu
862 * @param int $position The position in the menu order this one should appear
864 * @return string The resulting page's hook_suffix
866 function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = NULL ) {
867 global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
869 $menu_slug = plugin_basename( $menu_slug );
871 $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title );
873 $hookname = get_plugin_page_hookname( $menu_slug, '' );
875 if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) )
876 add_action( $hookname, $function );
878 if ( empty($icon_url) )
879 $icon_url = esc_url( admin_url( 'images/generic.png' ) );
880 elseif ( is_ssl() && 0 === strpos($icon_url, 'http://') )
881 $icon_url = 'https://' . substr($icon_url, 7);
883 $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
885 if ( null === $position )
888 $menu[$position] = $new_menu;
890 $_registered_pages[$hookname] = true;
892 // No parent as top level
893 $_parent_pages[$menu_slug] = false;
899 * Add a top level menu page in the 'objects' section
901 * This function takes a capability which will be used to determine whether
902 * or not a page is included in the menu.
904 * The function which is hooked in to handle the output of the page must check
905 * that the user has the required capability as well.
907 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
908 * @param string $menu_title The text to be used for the menu
909 * @param string $capability The capability required for this menu to be displayed to the user.
910 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
911 * @param callback $function The function to be called to output the content for this page.
912 * @param string $icon_url The url to the icon to be used for this menu
914 * @return string The resulting page's hook_suffix
916 function add_object_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
917 global $_wp_last_object_menu;
919 $_wp_last_object_menu++;
921 return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_object_menu);
925 * Add a top level menu page in the 'utility' section
927 * This function takes a capability which will be used to determine whether
928 * or not a page is included in the menu.
930 * The function which is hooked in to handle the output of the page must check
931 * that the user has the required capability as well.
933 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
934 * @param string $menu_title The text to be used for the menu
935 * @param string $capability The capability required for this menu to be displayed to the user.
936 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
937 * @param callback $function The function to be called to output the content for this page.
938 * @param string $icon_url The url to the icon to be used for this menu
940 * @return string The resulting page's hook_suffix
942 function add_utility_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
943 global $_wp_last_utility_menu;
945 $_wp_last_utility_menu++;
947 return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_utility_menu);
951 * Add a sub menu page
953 * This function takes a capability which will be used to determine whether
954 * or not a page is included in the menu.
956 * The function which is hooked in to handle the output of the page must check
957 * that the user has the required capability as well.
959 * @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page)
960 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
961 * @param string $menu_title The text to be used for the menu
962 * @param string $capability The capability required for this menu to be displayed to the user.
963 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
964 * @param callback $function The function to be called to output the content for this page.
966 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
968 function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
971 global $_wp_real_parent_file;
972 global $_wp_submenu_nopriv;
973 global $_registered_pages;
974 global $_parent_pages;
976 $menu_slug = plugin_basename( $menu_slug );
977 $parent_slug = plugin_basename( $parent_slug);
979 if ( isset( $_wp_real_parent_file[$parent_slug] ) )
980 $parent_slug = $_wp_real_parent_file[$parent_slug];
982 if ( !current_user_can( $capability ) ) {
983 $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true;
987 // If the parent doesn't already have a submenu, add a link to the parent
988 // as the first item in the submenu. If the submenu file is the same as the
989 // parent file someone is trying to link back to the parent manually. In
990 // this case, don't automatically add a link back to avoid duplication.
991 if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) {
992 foreach ( (array)$menu as $parent_menu ) {
993 if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) )
994 $submenu[$parent_slug][] = $parent_menu;
998 $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title );
1000 $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);
1001 if (!empty ( $function ) && !empty ( $hookname ))
1002 add_action( $hookname, $function );
1004 $_registered_pages[$hookname] = true;
1005 // backwards-compatibility for plugins using add_management page. See wp-admin/admin.php for redirect from edit.php to tools.php
1006 if ( 'tools.php' == $parent_slug )
1007 $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true;
1009 // No parent as top level
1010 $_parent_pages[$menu_slug] = $parent_slug;
1016 * Add sub menu page to the tools main menu.
1018 * This function takes a capability which will be used to determine whether
1019 * or not a page is included in the menu.
1021 * The function which is hooked in to handle the output of the page must check
1022 * that the user has the required capability as well.
1024 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1025 * @param string $menu_title The text to be used for the menu
1026 * @param string $capability The capability required for this menu to be displayed to the user.
1027 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1028 * @param callback $function The function to be called to output the content for this page.
1030 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1032 function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1033 return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1037 * Add sub menu page to the options main menu.
1039 * This function takes a capability which will be used to determine whether
1040 * or not a page is included in the menu.
1042 * The function which is hooked in to handle the output of the page must check
1043 * that the user has the required capability as well.
1045 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1046 * @param string $menu_title The text to be used for the menu
1047 * @param string $capability The capability required for this menu to be displayed to the user.
1048 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1049 * @param callback $function The function to be called to output the content for this page.
1051 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1053 function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1054 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1058 * Add sub menu page to the themes main menu.
1060 * This function takes a capability which will be used to determine whether
1061 * or not a page is included in the menu.
1063 * The function which is hooked in to handle the output of the page must check
1064 * that the user has the required capability as well.
1066 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1067 * @param string $menu_title The text to be used for the menu
1068 * @param string $capability The capability required for this menu to be displayed to the user.
1069 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1070 * @param callback $function The function to be called to output the content for this page.
1072 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1074 function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1075 return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1079 * Add sub menu page to the plugins main menu.
1081 * This function takes a capability which will be used to determine whether
1082 * or not a page is included in the menu.
1084 * The function which is hooked in to handle the output of the page must check
1085 * that the user has the required capability as well.
1087 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1088 * @param string $menu_title The text to be used for the menu
1089 * @param string $capability The capability required for this menu to be displayed to the user.
1090 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1091 * @param callback $function The function to be called to output the content for this page.
1093 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1095 function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1096 return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1100 * Add sub menu page to the Users/Profile main menu.
1102 * This function takes a capability which will be used to determine whether
1103 * or not a page is included in the menu.
1105 * The function which is hooked in to handle the output of the page must check
1106 * that the user has the required capability as well.
1108 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1109 * @param string $menu_title The text to be used for the menu
1110 * @param string $capability The capability required for this menu to be displayed to the user.
1111 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1112 * @param callback $function The function to be called to output the content for this page.
1114 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1116 function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1117 if ( current_user_can('edit_users') )
1118 $parent = 'users.php';
1120 $parent = 'profile.php';
1121 return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
1124 * Add sub menu page to the Dashboard main menu.
1126 * This function takes a capability which will be used to determine whether
1127 * or not a page is included in the menu.
1129 * The function which is hooked in to handle the output of the page must check
1130 * that the user has the required capability as well.
1132 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1133 * @param string $menu_title The text to be used for the menu
1134 * @param string $capability The capability required for this menu to be displayed to the user.
1135 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1136 * @param callback $function The function to be called to output the content for this page.
1138 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1140 function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1141 return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1145 * Add sub menu page to the posts main menu.
1147 * This function takes a capability which will be used to determine whether
1148 * or not a page is included in the menu.
1150 * The function which is hooked in to handle the output of the page must check
1151 * that the user has the required capability as well.
1153 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1154 * @param string $menu_title The text to be used for the menu
1155 * @param string $capability The capability required for this menu to be displayed to the user.
1156 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1157 * @param callback $function The function to be called to output the content for this page.
1159 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1161 function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1162 return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1166 * Add sub menu page to the media main menu.
1168 * This function takes a capability which will be used to determine whether
1169 * or not a page is included in the menu.
1171 * The function which is hooked in to handle the output of the page must check
1172 * that the user has the required capability as well.
1174 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1175 * @param string $menu_title The text to be used for the menu
1176 * @param string $capability The capability required for this menu to be displayed to the user.
1177 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1178 * @param callback $function The function to be called to output the content for this page.
1180 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1182 function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1183 return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1187 * Add sub menu page to the links main menu.
1189 * This function takes a capability which will be used to determine whether
1190 * or not a page is included in the menu.
1192 * The function which is hooked in to handle the output of the page must check
1193 * that the user has the required capability as well.
1195 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1196 * @param string $menu_title The text to be used for the menu
1197 * @param string $capability The capability required for this menu to be displayed to the user.
1198 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1199 * @param callback $function The function to be called to output the content for this page.
1201 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1203 function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1204 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1208 * Add sub menu page to the pages main menu.
1210 * This function takes a capability which will be used to determine whether
1211 * or not a page is included in the menu.
1213 * The function which is hooked in to handle the output of the page must check
1214 * that the user has the required capability as well.
1216 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1217 * @param string $menu_title The text to be used for the menu
1218 * @param string $capability The capability required for this menu to be displayed to the user.
1219 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1220 * @param callback $function The function to be called to output the content for this page.
1222 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1224 function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1225 return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function );
1229 * Add sub menu page to the comments main menu.
1231 * This function takes a capability which will be used to determine whether
1232 * or not a page is included in the menu.
1234 * The function which is hooked in to handle the output of the page must check
1235 * that the user has the required capability as well.
1237 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1238 * @param string $menu_title The text to be used for the menu
1239 * @param string $capability The capability required for this menu to be displayed to the user.
1240 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1241 * @param callback $function The function to be called to output the content for this page.
1243 * @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required.
1245 function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1246 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1251 * Remove a top level admin menu
1255 * @param string $menu_slug The slug of the menu
1256 * @return array|bool The removed menu on success, False if not found
1258 function remove_menu_page( $menu_slug ) {
1261 foreach ( $menu as $i => $item ) {
1262 if ( $menu_slug == $item[2] ) {
1272 * Remove an admin submenu
1276 * @param string $menu_slug The slug for the parent menu
1277 * @param string $submenu_slug The slug of the submenu
1278 * @return array|bool The removed submenu on success, False if not found
1280 function remove_submenu_page( $menu_slug, $submenu_slug ) {
1283 if ( !isset( $submenu[$menu_slug] ) )
1286 foreach ( $submenu[$menu_slug] as $i => $item ) {
1287 if ( $submenu_slug == $item[2] ) {
1288 unset( $submenu[$menu_slug][$i] );
1297 * Get the url to access a particular menu page based on the slug it was registered with.
1299 * If the slug hasn't been registered properly no url will be returned
1303 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1304 * @param bool $echo Whether or not to echo the url - default is true
1305 * @return string the url
1307 function menu_page_url($menu_slug, $echo = true) {
1308 global $_parent_pages;
1310 if ( isset( $_parent_pages[$menu_slug] ) ) {
1311 $parent_slug = $_parent_pages[$menu_slug];
1312 if ( $parent_slug && ! isset( $_parent_pages[$parent_slug] ) ) {
1313 $url = admin_url( add_query_arg( 'page', $menu_slug, $parent_slug ) );
1315 $url = admin_url( 'admin.php?page=' . $menu_slug );
1321 $url = esc_url($url);
1330 // Pluggable Menu Support -- Private
1333 function get_admin_page_parent( $parent = '' ) {
1334 global $parent_file;
1339 global $plugin_page;
1340 global $_wp_real_parent_file;
1341 global $_wp_menu_nopriv;
1342 global $_wp_submenu_nopriv;
1344 if ( !empty ( $parent ) && 'admin.php' != $parent ) {
1345 if ( isset( $_wp_real_parent_file[$parent] ) )
1346 $parent = $_wp_real_parent_file[$parent];
1351 if ( !empty ( $parent_file ) ) {
1352 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1353 $parent_file = $_wp_real_parent_file[$parent_file];
1355 return $parent_file;
1359 if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
1360 foreach ( (array)$menu as $parent_menu ) {
1361 if ( $parent_menu[2] == $plugin_page ) {
1362 $parent_file = $plugin_page;
1363 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1364 $parent_file = $_wp_real_parent_file[$parent_file];
1365 return $parent_file;
1368 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
1369 $parent_file = $plugin_page;
1370 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1371 $parent_file = $_wp_real_parent_file[$parent_file];
1372 return $parent_file;
1376 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
1377 $parent_file = $pagenow;
1378 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1379 $parent_file = $_wp_real_parent_file[$parent_file];
1380 return $parent_file;
1383 foreach (array_keys( (array)$submenu ) as $parent) {
1384 foreach ( $submenu[$parent] as $submenu_array ) {
1385 if ( isset( $_wp_real_parent_file[$parent] ) )
1386 $parent = $_wp_real_parent_file[$parent];
1387 if ( !empty($typenow) && ($submenu_array[2] == "$pagenow?post_type=$typenow") ) {
1388 $parent_file = $parent;
1390 } elseif ( $submenu_array[2] == $pagenow && empty($typenow) && ( empty($parent_file) || false === strpos($parent_file, '?') ) ) {
1391 $parent_file = $parent;
1394 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
1395 $parent_file = $parent;
1401 if ( empty($parent_file) )
1406 function get_admin_page_title() {
1411 global $plugin_page;
1414 if ( ! empty ( $title ) )
1417 $hook = get_plugin_page_hook( $plugin_page, $pagenow );
1419 $parent = $parent1 = get_admin_page_parent();
1421 if ( empty ( $parent) ) {
1422 foreach ( (array)$menu as $menu_array ) {
1423 if ( isset( $menu_array[3] ) ) {
1424 if ( $menu_array[2] == $pagenow ) {
1425 $title = $menu_array[3];
1426 return $menu_array[3];
1428 if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
1429 $title = $menu_array[3];
1430 return $menu_array[3];
1433 $title = $menu_array[0];
1438 foreach ( array_keys( $submenu ) as $parent ) {
1439 foreach ( $submenu[$parent] as $submenu_array ) {
1440 if ( isset( $plugin_page ) &&
1441 ( $plugin_page == $submenu_array[2] ) &&
1443 ( $parent == $pagenow ) ||
1444 ( $parent == $plugin_page ) ||
1445 ( $plugin_page == $hook ) ||
1446 ( $pagenow == 'admin.php' && $parent1 != $submenu_array[2] ) ||
1447 ( !empty($typenow) && $parent == $pagenow . '?post_type=' . $typenow)
1450 $title = $submenu_array[3];
1451 return $submenu_array[3];
1454 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
1457 if ( isset( $submenu_array[3] ) ) {
1458 $title = $submenu_array[3];
1459 return $submenu_array[3];
1461 $title = $submenu_array[0];
1466 if ( empty ( $title ) ) {
1467 foreach ( $menu as $menu_array ) {
1468 if ( isset( $plugin_page ) &&
1469 ( $plugin_page == $menu_array[2] ) &&
1470 ( $pagenow == 'admin.php' ) &&
1471 ( $parent1 == $menu_array[2] ) )
1473 $title = $menu_array[3];
1474 return $menu_array[3];
1483 function get_plugin_page_hook( $plugin_page, $parent_page ) {
1484 $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
1485 if ( has_action($hook) )
1491 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
1492 global $admin_page_hooks;
1494 $parent = get_admin_page_parent( $parent_page );
1496 $page_type = 'admin';
1497 if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
1498 if ( isset( $admin_page_hooks[$plugin_page] ) )
1499 $page_type = 'toplevel';
1501 if ( isset( $admin_page_hooks[$parent] ))
1502 $page_type = $admin_page_hooks[$parent];
1503 } else if ( isset( $admin_page_hooks[$parent] ) ) {
1504 $page_type = $admin_page_hooks[$parent];
1507 $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
1509 return $page_type . '_page_' . $plugin_name;
1512 function user_can_access_admin_page() {
1516 global $_wp_menu_nopriv;
1517 global $_wp_submenu_nopriv;
1518 global $plugin_page;
1519 global $_registered_pages;
1521 $parent = get_admin_page_parent();
1523 if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
1526 if ( isset( $plugin_page ) ) {
1527 if ( isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
1530 $hookname = get_plugin_page_hookname($plugin_page, $parent);
1532 if ( !isset($_registered_pages[$hookname]) )
1536 if ( empty( $parent) ) {
1537 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
1539 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
1541 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
1543 if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1545 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
1546 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
1548 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
1554 if ( isset( $plugin_page ) && ( $plugin_page == $parent ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1557 if ( isset( $submenu[$parent] ) ) {
1558 foreach ( $submenu[$parent] as $submenu_array ) {
1559 if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
1560 if ( current_user_can( $submenu_array[1] ))
1564 } else if ( $submenu_array[2] == $pagenow ) {
1565 if ( current_user_can( $submenu_array[1] ))
1573 foreach ( $menu as $menu_array ) {
1574 if ( $menu_array[2] == $parent) {
1575 if ( current_user_can( $menu_array[1] ))
1585 /* Whitelist functions */
1588 * Register a setting and its sanitization callback
1592 * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
1593 * Default whitelisted option key names include "general," "discussion," and "reading," among others.
1594 * @param string $option_name The name of an option to sanitize and save.
1595 * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
1598 function register_setting( $option_group, $option_name, $sanitize_callback = '' ) {
1599 global $new_whitelist_options;
1601 if ( 'misc' == $option_group ) {
1602 _deprecated_argument( __FUNCTION__, '3.0', __( 'The miscellaneous options group has been removed. Use another settings group.' ) );
1603 $option_group = 'general';
1606 $new_whitelist_options[ $option_group ][] = $option_name;
1607 if ( $sanitize_callback != '' )
1608 add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1612 * Unregister a setting
1616 * @param unknown_type $option_group
1617 * @param unknown_type $option_name
1618 * @param unknown_type $sanitize_callback
1621 function unregister_setting( $option_group, $option_name, $sanitize_callback = '' ) {
1622 global $new_whitelist_options;
1624 if ( 'misc' == $option_group ) {
1625 _deprecated_argument( __FUNCTION__, '3.0', __( 'The miscellaneous options group has been removed. Use another settings group.' ) );
1626 $option_group = 'general';
1629 $pos = array_search( $option_name, (array) $new_whitelist_options );
1630 if ( $pos !== false )
1631 unset( $new_whitelist_options[ $option_group ][ $pos ] );
1632 if ( $sanitize_callback != '' )
1633 remove_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1637 * {@internal Missing Short Description}}
1641 * @param unknown_type $options
1644 function option_update_filter( $options ) {
1645 global $new_whitelist_options;
1647 if ( is_array( $new_whitelist_options ) )
1648 $options = add_option_whitelist( $new_whitelist_options, $options );
1652 add_filter( 'whitelist_options', 'option_update_filter' );
1655 * {@internal Missing Short Description}}
1659 * @param unknown_type $new_options
1660 * @param unknown_type $options
1663 function add_option_whitelist( $new_options, $options = '' ) {
1664 if ( $options == '' )
1665 global $whitelist_options;
1667 $whitelist_options = $options;
1669 foreach ( $new_options as $page => $keys ) {
1670 foreach ( $keys as $key ) {
1671 if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
1672 $whitelist_options[ $page ] = array();
1673 $whitelist_options[ $page ][] = $key;
1675 $pos = array_search( $key, $whitelist_options[ $page ] );
1676 if ( $pos === false )
1677 $whitelist_options[ $page ][] = $key;
1682 return $whitelist_options;
1686 * {@internal Missing Short Description}}
1690 * @param unknown_type $del_options
1691 * @param unknown_type $options
1694 function remove_option_whitelist( $del_options, $options = '' ) {
1695 if ( $options == '' )
1696 global $whitelist_options;
1698 $whitelist_options = $options;
1700 foreach ( $del_options as $page => $keys ) {
1701 foreach ( $keys as $key ) {
1702 if ( isset($whitelist_options[ $page ]) && is_array($whitelist_options[ $page ]) ) {
1703 $pos = array_search( $key, $whitelist_options[ $page ] );
1704 if ( $pos !== false )
1705 unset( $whitelist_options[ $page ][ $pos ] );
1710 return $whitelist_options;
1714 * Output nonce, action, and option_page fields for a settings page.
1718 * @param string $option_group A settings group name. This should match the group name used in register_setting().
1720 function settings_fields($option_group) {
1721 echo "<input type='hidden' name='option_page' value='" . esc_attr($option_group) . "' />";
1722 echo '<input type="hidden" name="action" value="update" />';
1723 wp_nonce_field("$option_group-options");