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 );
107 function _get_plugin_data_markup_translate($plugin_file, $plugin_data, $markup = true, $translate = true) {
110 if ( $translate && ! empty($plugin_data['TextDomain']) ) {
111 if ( ! empty( $plugin_data['DomainPath'] ) )
112 load_plugin_textdomain($plugin_data['TextDomain'], false, dirname($plugin_file). $plugin_data['DomainPath']);
114 load_plugin_textdomain($plugin_data['TextDomain'], false, dirname($plugin_file));
116 foreach ( array('Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version') as $field )
117 $plugin_data[ $field ] = translate($plugin_data[ $field ], $plugin_data['TextDomain']);
122 if ( ! empty($plugin_data['PluginURI']) && ! empty($plugin_data['Name']) )
123 $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '" title="' . __( 'Visit plugin homepage' ) . '">' . $plugin_data['Name'] . '</a>';
125 $plugin_data['Title'] = $plugin_data['Name'];
127 if ( ! empty($plugin_data['AuthorURI']) && ! empty($plugin_data['Author']) )
128 $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '" title="' . __( 'Visit author homepage' ) . '">' . $plugin_data['Author'] . '</a>';
130 $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
131 if ( ! empty($plugin_data['Author']) )
132 $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s'), $plugin_data['Author'] ) . '.</cite>';
135 $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
137 // Sanitize all displayed data
138 $plugin_data['Title'] = wp_kses($plugin_data['Title'], $plugins_allowedtags);
139 $plugin_data['Version'] = wp_kses($plugin_data['Version'], $plugins_allowedtags);
140 $plugin_data['Description'] = wp_kses($plugin_data['Description'], $plugins_allowedtags);
141 $plugin_data['Author'] = wp_kses($plugin_data['Author'], $plugins_allowedtags);
147 * Get a list of a plugin's files.
151 * @param string $plugin Plugin ID
152 * @return array List of files relative to the plugin root.
154 function get_plugin_files($plugin) {
155 $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
156 $dir = dirname($plugin_file);
157 $plugin_files = array($plugin);
158 if ( is_dir($dir) && $dir != WP_PLUGIN_DIR ) {
159 $plugins_dir = @ opendir( $dir );
160 if ( $plugins_dir ) {
161 while (($file = readdir( $plugins_dir ) ) !== false ) {
162 if ( substr($file, 0, 1) == '.' )
164 if ( is_dir( $dir . '/' . $file ) ) {
165 $plugins_subdir = @ opendir( $dir . '/' . $file );
166 if ( $plugins_subdir ) {
167 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
168 if ( substr($subfile, 0, 1) == '.' )
170 $plugin_files[] = plugin_basename("$dir/$file/$subfile");
172 @closedir( $plugins_subdir );
175 if ( plugin_basename("$dir/$file") != $plugin )
176 $plugin_files[] = plugin_basename("$dir/$file");
179 @closedir( $plugins_dir );
183 return $plugin_files;
187 * Check the plugins directory and retrieve all plugin files with plugin data.
189 * WordPress only supports plugin files in the base plugins directory
190 * (wp-content/plugins) and in one directory above the plugins directory
191 * (wp-content/plugins/my-plugin). The file it looks for has the plugin data and
192 * must be found in those two locations. It is recommended that do keep your
193 * plugin files in directories.
195 * The file with the plugin data is the file that will be included and therefore
196 * needs to have the main execution for the plugin. This does not mean
197 * everything must be contained in the file and it is recommended that the file
198 * be split for maintainability. Keep everything in one file for extreme
199 * optimization purposes.
203 * @param string $plugin_folder Optional. Relative path to single plugin folder.
204 * @return array Key is the plugin file path and the value is an array of the plugin data.
206 function get_plugins($plugin_folder = '') {
208 if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
209 $cache_plugins = array();
211 if ( isset($cache_plugins[ $plugin_folder ]) )
212 return $cache_plugins[ $plugin_folder ];
214 $wp_plugins = array ();
215 $plugin_root = WP_PLUGIN_DIR;
216 if ( !empty($plugin_folder) )
217 $plugin_root .= $plugin_folder;
219 // Files in wp-content/plugins directory
220 $plugins_dir = @ opendir( $plugin_root);
221 $plugin_files = array();
222 if ( $plugins_dir ) {
223 while (($file = readdir( $plugins_dir ) ) !== false ) {
224 if ( substr($file, 0, 1) == '.' )
226 if ( is_dir( $plugin_root.'/'.$file ) ) {
227 $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
228 if ( $plugins_subdir ) {
229 while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
230 if ( substr($subfile, 0, 1) == '.' )
232 if ( substr($subfile, -4) == '.php' )
233 $plugin_files[] = "$file/$subfile";
237 if ( substr($file, -4) == '.php' )
238 $plugin_files[] = $file;
245 @closedir( $plugins_dir );
246 @closedir( $plugins_subdir );
248 if ( empty($plugin_files) )
251 foreach ( $plugin_files as $plugin_file ) {
252 if ( !is_readable( "$plugin_root/$plugin_file" ) )
255 $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
257 if ( empty ( $plugin_data['Name'] ) )
260 $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
263 uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
265 $cache_plugins[ $plugin_folder ] = $wp_plugins;
266 wp_cache_set('plugins', $cache_plugins, 'plugins');
272 * Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data.
274 * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins).
277 * @return array Key is the mu-plugin file path and the value is an array of the mu-plugin data.
279 function get_mu_plugins() {
280 $wp_plugins = array();
281 // Files in wp-content/mu-plugins directory
282 $plugin_files = array();
284 if ( ! is_dir( WPMU_PLUGIN_DIR ) )
286 if ( $plugins_dir = @ opendir( WPMU_PLUGIN_DIR ) ) {
287 while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
288 if ( substr( $file, -4 ) == '.php' )
289 $plugin_files[] = $file;
295 @closedir( $plugins_dir );
297 if ( empty($plugin_files) )
300 foreach ( $plugin_files as $plugin_file ) {
301 if ( !is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) )
304 $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
306 if ( empty ( $plugin_data['Name'] ) )
307 $plugin_data['Name'] = $plugin_file;
309 $wp_plugins[ $plugin_file ] = $plugin_data;
312 if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php') <= 30 ) // silence is golden
313 unset( $wp_plugins['index.php'] );
315 uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
321 * Check the wp-content directory and retrieve all drop-ins with any plugin data.
324 * @return array Key is the file path and the value is an array of the plugin data.
326 function get_dropins() {
328 $plugin_files = array();
330 $_dropins = _get_dropins();
332 // These exist in the wp-content directory
333 if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) {
334 while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
335 if ( isset( $_dropins[ $file ] ) )
336 $plugin_files[] = $file;
342 @closedir( $plugins_dir );
344 if ( empty($plugin_files) )
347 foreach ( $plugin_files as $plugin_file ) {
348 if ( !is_readable( WP_CONTENT_DIR . "/$plugin_file" ) )
350 $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
351 if ( empty( $plugin_data['Name'] ) )
352 $plugin_data['Name'] = $plugin_file;
353 $dropins[ $plugin_file ] = $plugin_data;
356 uksort( $dropins, create_function( '$a, $b', 'return strnatcasecmp( $a, $b );' ));
362 * Returns drop-ins that WordPress uses.
364 * Includes Multisite drop-ins only when is_multisite()
367 * @return array Key is file name. The value is an array, with the first value the
368 * purpose of the drop-in and the second value the name of the constant that must be
369 * true for the drop-in to be used, or true if no constant is required.
371 function _get_dropins() {
373 'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE
374 'db.php' => array( __( 'Custom database class.' ), true ), // auto on load
375 'db-error.php' => array( __( 'Custom database error message.' ), true ), // auto on error
376 'install.php' => array( __( 'Custom install script.' ), true ), // auto on install
377 'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // auto on maintenance
378 'object-cache.php' => array( __( 'External object cache.' ), true ), // auto on load
381 if ( is_multisite() ) {
382 $dropins['sunrise.php' ] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE
383 $dropins['blog-deleted.php' ] = array( __( 'Custom site deleted message.' ), true ); // auto on deleted blog
384 $dropins['blog-inactive.php' ] = array( __( 'Custom site inactive message.' ), true ); // auto on inactive blog
385 $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // auto on archived or spammed blog
392 * Check whether the plugin is active by checking the active_plugins list.
396 * @param string $plugin Base plugin path from plugins directory.
397 * @return bool True, if in the active plugins list. False, not in the list.
399 function is_plugin_active( $plugin ) {
400 return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
404 * Check whether the plugin is active for the entire network.
408 * @param string $plugin Base plugin path from plugins directory.
409 * @return bool True, if active for the network, otherwise false.
411 function is_plugin_active_for_network( $plugin ) {
412 if ( !is_multisite() )
415 $plugins = get_site_option( 'active_sitewide_plugins');
416 if ( isset($plugins[$plugin]) )
423 * Checks for "Network: true" in the plugin header to see if this should
424 * be activated only as a network wide plugin. The plugin would also work
425 * when Multisite is not enabled.
427 * Checks for "Site Wide Only: true" for backwards compatibility.
431 * @param $file Plugin to check
432 * $return bool True if plugin is network only, false otherwise.
434 function is_network_only_plugin( $plugin ) {
435 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
437 return $plugin_data['Network'];
442 * Attempts activation of plugin in a "sandbox" and redirects on success.
444 * A plugin that is already activated will not attempt to be activated again.
446 * The way it works is by setting the redirection to the error before trying to
447 * include the plugin file. If the plugin fails, then the redirection will not
448 * be overwritten with the success message. Also, the options will not be
449 * updated and the activation hook will not be called on plugin error.
451 * It should be noted that in no way the below code will actually prevent errors
452 * within the file. The code should not be used elsewhere to replicate the
453 * "sandbox", which uses redirection to work.
456 * If any errors are found or text is outputted, then it will be captured to
457 * ensure that the success redirection will update the error redirection.
461 * @param string $plugin Plugin path to main plugin file with plugin data.
462 * @param string $redirect Optional. URL to redirect to.
463 * @param bool $network_wide Whether to enable the plugin for all sites in the network or just the current site. Multisite only. Default is false.
464 * @return WP_Error|null WP_Error on invalid file or null on success.
466 function activate_plugin( $plugin, $redirect = '', $network_wide = false) {
467 $plugin = plugin_basename( trim( $plugin ) );
469 if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) {
470 $network_wide = true;
471 $current = get_site_option( 'active_sitewide_plugins', array() );
473 $current = get_option( 'active_plugins', array() );
476 $valid = validate_plugin($plugin);
477 if ( is_wp_error($valid) )
480 if ( !in_array($plugin, $current) ) {
481 if ( !empty($redirect) )
482 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
484 include(WP_PLUGIN_DIR . '/' . $plugin);
485 do_action( 'activate_plugin', trim( $plugin) );
486 do_action( 'activate_' . trim( $plugin ) );
487 if ( $network_wide ) {
488 $current[$plugin] = time();
489 update_site_option( 'active_sitewide_plugins', $current );
491 $current[] = $plugin;
493 update_option('active_plugins', $current);
495 do_action( 'activated_plugin', trim( $plugin) );
496 if ( ob_get_length() > 0 ) {
497 $output = ob_get_clean();
498 return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
507 * Deactivate a single plugin or multiple plugins.
509 * The deactivation hook is disabled by the plugin upgrader by using the $silent
514 * @param string|array $plugins Single plugin or list of plugins to deactivate.
515 * @param bool $silent Optional, default is false. Prevent calling deactivate hook.
517 function deactivate_plugins( $plugins, $silent = false ) {
518 $network_current = get_site_option( 'active_sitewide_plugins', array() );
519 $current = get_option( 'active_plugins', array() );
520 $do_blog = $do_network = false;
522 foreach ( (array) $plugins as $plugin ) {
523 $plugin = plugin_basename($plugin);
524 if ( ! is_plugin_active($plugin) )
527 do_action( 'deactivate_plugin', trim( $plugin ) );
529 if ( is_plugin_active_for_network($plugin) ) {
530 // Deactivate network wide
532 unset( $network_current[ $plugin ] );
534 // Deactivate for this blog only
535 $key = array_search( $plugin, (array) $current );
536 if ( false !== $key ) {
538 array_splice( $current, $key, 1 );
542 //Used by Plugin updater to internally deactivate plugin, however, not to notify plugins of the fact to prevent plugin output.
544 do_action( 'deactivate_' . trim( $plugin ) );
545 do_action( 'deactivated_plugin', trim( $plugin ) );
550 update_option('active_plugins', $current);
552 update_site_option( 'active_sitewide_plugins', $network_current );
556 * Activate multiple plugins.
558 * When WP_Error is returned, it does not mean that one of the plugins had
559 * errors. It means that one or more of the plugins file path was invalid.
561 * The execution will be halted as soon as one of the plugins has an error.
565 * @param string|array $plugins
566 * @param string $redirect Redirect to page after successful activation.
567 * @param bool $network_wide Whether to enable the plugin for all sites in the network.
568 * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
570 function activate_plugins($plugins, $redirect = '', $network_wide) {
571 if ( !is_array($plugins) )
572 $plugins = array($plugins);
575 foreach ( (array) $plugins as $plugin ) {
576 if ( !empty($redirect) )
577 $redirect = add_query_arg('plugin', $plugin, $redirect);
578 $result = activate_plugin($plugin, $redirect, $network_wide);
579 if ( is_wp_error($result) )
580 $errors[$plugin] = $result;
583 if ( !empty($errors) )
584 return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
590 * Remove directory and files of a plugin for a single or list of plugin(s).
592 * If the plugins parameter list is empty, false will be returned. True when
597 * @param array $plugins List of plugin
598 * @param string $redirect Redirect to page when complete.
601 function delete_plugins($plugins, $redirect = '' ) {
602 global $wp_filesystem;
604 if ( empty($plugins) )
608 foreach( $plugins as $plugin )
609 $checked[] = 'checked[]=' . $plugin;
612 $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-manage-plugins');
613 if ( false === ($credentials = request_filesystem_credentials($url)) ) {
614 $data = ob_get_contents();
616 if ( ! empty($data) ){
617 include_once( ABSPATH . 'wp-admin/admin-header.php');
619 include( ABSPATH . 'wp-admin/admin-footer.php');
625 if ( ! WP_Filesystem($credentials) ) {
626 request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
627 $data = ob_get_contents();
629 if ( ! empty($data) ){
630 include_once( ABSPATH . 'wp-admin/admin-header.php');
632 include( ABSPATH . 'wp-admin/admin-footer.php');
638 if ( ! is_object($wp_filesystem) )
639 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
641 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
642 return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
644 //Get the base plugin folder
645 $plugins_dir = $wp_filesystem->wp_plugins_dir();
646 if ( empty($plugins_dir) )
647 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
649 $plugins_dir = trailingslashit( $plugins_dir );
653 foreach( $plugins as $plugin_file ) {
654 // Run Uninstall hook
655 if ( is_uninstallable_plugin( $plugin_file ) )
656 uninstall_plugin($plugin_file);
658 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
659 // If plugin is in its own directory, recursively delete the directory.
660 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
661 $deleted = $wp_filesystem->delete($this_plugin_dir, true);
663 $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
666 $errors[] = $plugin_file;
669 if ( ! empty($errors) )
670 return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s.'), implode(', ', $errors)) );
672 // Force refresh of plugin update information
673 if ( $current = get_site_transient('update_plugins') ) {
674 unset( $current->response[ $plugin_file ] );
675 set_site_transient('update_plugins', $current);
682 * Validate active plugins
684 * Validate all active plugins, deactivates invalid and
685 * returns an array of deactivated ones.
688 * @return array invalid plugins, plugin as key, error as value
690 function validate_active_plugins() {
691 $plugins = get_option( 'active_plugins', array() );
692 // validate vartype: array
693 if ( ! is_array( $plugins ) ) {
694 update_option( 'active_plugins', array() );
698 if ( is_multisite() && is_super_admin() ) {
699 $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
700 $plugins = array_merge( $plugins, array_keys( $network_plugins ) );
703 if ( empty( $plugins ) )
708 // invalid plugins get deactivated
709 foreach ( $plugins as $plugin ) {
710 $result = validate_plugin( $plugin );
711 if ( is_wp_error( $result ) ) {
712 $invalid[$plugin] = $result;
713 deactivate_plugins( $plugin, true );
720 * Validate the plugin path.
722 * Checks that the file exists and {@link validate_file() is valid file}.
726 * @param string $plugin Plugin Path
727 * @return WP_Error|int 0 on success, WP_Error on failure.
729 function validate_plugin($plugin) {
730 if ( validate_file($plugin) )
731 return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
732 if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
733 return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
735 $installed_plugins = get_plugins();
736 if ( ! isset($installed_plugins[$plugin]) )
737 return new WP_Error('no_plugin_header', __('The plugin does not have a valid header.'));
742 * Whether the plugin can be uninstalled.
746 * @param string $plugin Plugin path to check.
747 * @return bool Whether plugin can be uninstalled.
749 function is_uninstallable_plugin($plugin) {
750 $file = plugin_basename($plugin);
752 $uninstallable_plugins = (array) get_option('uninstall_plugins');
753 if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
760 * Uninstall a single plugin.
762 * Calls the uninstall hook, if it is available.
766 * @param string $plugin Relative plugin path from Plugin Directory.
768 function uninstall_plugin($plugin) {
769 $file = plugin_basename($plugin);
771 $uninstallable_plugins = (array) get_option('uninstall_plugins');
772 if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
773 if ( isset( $uninstallable_plugins[$file] ) ) {
774 unset($uninstallable_plugins[$file]);
775 update_option('uninstall_plugins', $uninstallable_plugins);
777 unset($uninstallable_plugins);
779 define('WP_UNINSTALL_PLUGIN', $file);
780 include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
785 if ( isset( $uninstallable_plugins[$file] ) ) {
786 $callable = $uninstallable_plugins[$file];
787 unset($uninstallable_plugins[$file]);
788 update_option('uninstall_plugins', $uninstallable_plugins);
789 unset($uninstallable_plugins);
791 include WP_PLUGIN_DIR . '/' . $file;
793 add_action( 'uninstall_' . $file, $callable );
794 do_action( 'uninstall_' . $file );
803 * Add a top level menu page
805 * This function takes a capability which will be used to determine whether
806 * or not a page is included in the menu.
808 * The function which is hooked in to handle the output of the page must check
809 * that the user has the required capability as well.
811 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
812 * @param string $menu_title The text to be used for the menu
813 * @param string $capability The capability required for this menu to be displayed to the user.
814 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
815 * @param callback $function The function to be called to output the content for this page.
816 * @param string $icon_url The url to the icon to be used for this menu
817 * @param int $position The position in the menu order this one should appear
819 function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = NULL ) {
820 global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
822 $menu_slug = plugin_basename( $menu_slug );
824 $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title );
826 $hookname = get_plugin_page_hookname( $menu_slug, '' );
828 if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) )
829 add_action( $hookname, $function );
831 if ( empty($icon_url) )
832 $icon_url = esc_url( admin_url( 'images/generic.png' ) );
833 elseif ( is_ssl() && 0 === strpos($icon_url, 'http://') )
834 $icon_url = 'https://' . substr($icon_url, 7);
836 $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
838 if ( null === $position )
841 $menu[$position] = $new_menu;
843 $_registered_pages[$hookname] = true;
845 // No parent as top level
846 $_parent_pages[$menu_slug] = false;
852 * Add a top level menu page in the 'objects' section
854 * This function takes a capability which will be used to determine whether
855 * or not a page is included in the menu.
857 * The function which is hooked in to handle the output of the page must check
858 * that the user has the required capability as well.
860 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
861 * @param string $menu_title The text to be used for the menu
862 * @param string $capability The capability required for this menu to be displayed to the user.
863 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
864 * @param callback $function The function to be called to output the content for this page.
865 * @param string $icon_url The url to the icon to be used for this menu
867 function add_object_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
868 global $_wp_last_object_menu;
870 $_wp_last_object_menu++;
872 return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_object_menu);
876 * Add a top level menu page in the 'utility' section
878 * This function takes a capability which will be used to determine whether
879 * or not a page is included in the menu.
881 * The function which is hooked in to handle the output of the page must check
882 * that the user has the required capability as well.
884 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
885 * @param string $menu_title The text to be used for the menu
886 * @param string $capability The capability required for this menu to be displayed to the user.
887 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
888 * @param callback $function The function to be called to output the content for this page.
889 * @param string $icon_url The url to the icon to be used for this menu
891 function add_utility_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '') {
892 global $_wp_last_utility_menu;
894 $_wp_last_utility_menu++;
896 return add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $_wp_last_utility_menu);
900 * Add a sub menu page
902 * This function takes a capability which will be used to determine whether
903 * or not a page is included in the menu.
905 * The function which is hooked in to handle the output of the page must check
906 * that the user has the required capability as well.
908 * @param string $parent_slug The slug name for the parent menu (or the file name of a standard WordPress admin page)
909 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
910 * @param string $menu_title The text to be used for the menu
911 * @param string $capability The capability required for this menu to be displayed to the user.
912 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
913 * @param callback $function The function to be called to output the content for this page.
915 function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
918 global $_wp_real_parent_file;
919 global $_wp_submenu_nopriv;
920 global $_registered_pages;
921 global $_parent_pages;
923 $menu_slug = plugin_basename( $menu_slug );
924 $parent_slug = plugin_basename( $parent_slug);
926 if ( isset( $_wp_real_parent_file[$parent_slug] ) )
927 $parent_slug = $_wp_real_parent_file[$parent_slug];
929 if ( !current_user_can( $capability ) ) {
930 $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true;
934 // If the parent doesn't already have a submenu, add a link to the parent
935 // as the first item in the submenu. If the submenu file is the same as the
936 // parent file someone is trying to link back to the parent manually. In
937 // this case, don't automatically add a link back to avoid duplication.
938 if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) {
939 foreach ( (array)$menu as $parent_menu ) {
940 if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) )
941 $submenu[$parent_slug][] = $parent_menu;
945 $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title );
947 $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);
948 if (!empty ( $function ) && !empty ( $hookname ))
949 add_action( $hookname, $function );
951 $_registered_pages[$hookname] = true;
952 // backwards-compatibility for plugins using add_management page. See wp-admin/admin.php for redirect from edit.php to tools.php
953 if ( 'tools.php' == $parent_slug )
954 $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true;
956 // No parent as top level
957 $_parent_pages[$menu_slug] = $parent_slug;
963 * Add sub menu page to the tools main menu.
965 * This function takes a capability which will be used to determine whether
966 * or not a page is included in the menu.
968 * The function which is hooked in to handle the output of the page must check
969 * that the user has the required capability as well.
971 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
972 * @param string $menu_title The text to be used for the menu
973 * @param string $capability The capability required for this menu to be displayed to the user.
974 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
975 * @param callback $function The function to be called to output the content for this page.
977 function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
978 return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );
982 * Add sub menu page to the options main menu.
984 * This function takes a capability which will be used to determine whether
985 * or not a page is included in the menu.
987 * The function which is hooked in to handle the output of the page must check
988 * that the user has the required capability as well.
990 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
991 * @param string $menu_title The text to be used for the menu
992 * @param string $capability The capability required for this menu to be displayed to the user.
993 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
994 * @param callback $function The function to be called to output the content for this page.
996 function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
997 return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1001 * Add sub menu page to the themes main menu.
1003 * This function takes a capability which will be used to determine whether
1004 * or not a page is included in the menu.
1006 * The function which is hooked in to handle the output of the page must check
1007 * that the user has the required capability as well.
1009 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1010 * @param string $menu_title The text to be used for the menu
1011 * @param string $capability The capability required for this menu to be displayed to the user.
1012 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1013 * @param callback $function The function to be called to output the content for this page.
1015 function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1016 return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1020 * Add sub menu page to the plugins main menu.
1022 * This function takes a capability which will be used to determine whether
1023 * or not a page is included in the menu.
1025 * The function which is hooked in to handle the output of the page must check
1026 * that the user has the required capability as well.
1028 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1029 * @param string $menu_title The text to be used for the menu
1030 * @param string $capability The capability required for this menu to be displayed to the user.
1031 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1032 * @param callback $function The function to be called to output the content for this page.
1034 function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1035 return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1039 * Add sub menu page to the Users/Profile main menu.
1041 * This function takes a capability which will be used to determine whether
1042 * or not a page is included in the menu.
1044 * The function which is hooked in to handle the output of the page must check
1045 * that the user has the required capability as well.
1047 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1048 * @param string $menu_title The text to be used for the menu
1049 * @param string $capability The capability required for this menu to be displayed to the user.
1050 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1051 * @param callback $function The function to be called to output the content for this page.
1053 function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1054 if ( current_user_can('edit_users') )
1055 $parent = 'users.php';
1057 $parent = 'profile.php';
1058 return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
1061 * Add sub menu page to the Dashboard 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 function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1076 return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1080 * Add sub menu page to the posts main menu.
1082 * This function takes a capability which will be used to determine whether
1083 * or not a page is included in the menu.
1085 * The function which is hooked in to handle the output of the page must check
1086 * that the user has the required capability as well.
1088 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1089 * @param string $menu_title The text to be used for the menu
1090 * @param string $capability The capability required for this menu to be displayed to the user.
1091 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1092 * @param callback $function The function to be called to output the content for this page.
1094 function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1095 return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1099 * Add sub menu page to the media main menu.
1101 * This function takes a capability which will be used to determine whether
1102 * or not a page is included in the menu.
1104 * The function which is hooked in to handle the output of the page must check
1105 * that the user has the required capability as well.
1107 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1108 * @param string $menu_title The text to be used for the menu
1109 * @param string $capability The capability required for this menu to be displayed to the user.
1110 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1111 * @param callback $function The function to be called to output the content for this page.
1113 function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1114 return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1118 * Add sub menu page to the links main menu.
1120 * This function takes a capability which will be used to determine whether
1121 * or not a page is included in the menu.
1123 * The function which is hooked in to handle the output of the page must check
1124 * that the user has the required capability as well.
1126 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1127 * @param string $menu_title The text to be used for the menu
1128 * @param string $capability The capability required for this menu to be displayed to the user.
1129 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1130 * @param callback $function The function to be called to output the content for this page.
1132 function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1133 return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1137 * Add sub menu page to the pages main menu.
1139 * This function takes a capability which will be used to determine whether
1140 * or not a page is included in the menu.
1142 * The function which is hooked in to handle the output of the page must check
1143 * that the user has the required capability as well.
1145 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1146 * @param string $menu_title The text to be used for the menu
1147 * @param string $capability The capability required for this menu to be displayed to the user.
1148 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1149 * @param callback $function The function to be called to output the content for this page.
1151 function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1152 return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function );
1156 * Add sub menu page to the comments main menu.
1158 * This function takes a capability which will be used to determine whether
1159 * or not a page is included in the menu.
1161 * The function which is hooked in to handle the output of the page must check
1162 * that the user has the required capability as well.
1164 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected
1165 * @param string $menu_title The text to be used for the menu
1166 * @param string $capability The capability required for this menu to be displayed to the user.
1167 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1168 * @param callback $function The function to be called to output the content for this page.
1170 function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
1171 return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function );
1175 * Get the url to access a particular menu page based on the slug it was registered with.
1177 * If the slug hasn't been registered properly no url will be returned
1181 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
1182 * @param bool $echo Whether or not to echo the url - default is true
1183 * @return string the url
1185 function menu_page_url($menu_slug, $echo = true) {
1186 global $_parent_pages;
1188 if ( isset( $_parent_pages[$menu_slug] ) ) {
1189 if ( $_parent_pages[$menu_slug] ) {
1190 $url = admin_url( add_query_arg( 'page', $menu_slug, $_parent_pages[$menu_slug] ) );
1192 $url = admin_url('admin.php?page=' . $menu_slug);
1198 $url = esc_url($url);
1207 // Pluggable Menu Support -- Private
1210 function get_admin_page_parent( $parent = '' ) {
1211 global $parent_file;
1216 global $plugin_page;
1217 global $_wp_real_parent_file;
1218 global $_wp_menu_nopriv;
1219 global $_wp_submenu_nopriv;
1221 if ( !empty ( $parent ) && 'admin.php' != $parent ) {
1222 if ( isset( $_wp_real_parent_file[$parent] ) )
1223 $parent = $_wp_real_parent_file[$parent];
1228 if ( !empty ( $parent_file ) ) {
1229 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1230 $parent_file = $_wp_real_parent_file[$parent_file];
1232 return $parent_file;
1236 if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
1237 foreach ( (array)$menu as $parent_menu ) {
1238 if ( $parent_menu[2] == $plugin_page ) {
1239 $parent_file = $plugin_page;
1240 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1241 $parent_file = $_wp_real_parent_file[$parent_file];
1242 return $parent_file;
1245 if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
1246 $parent_file = $plugin_page;
1247 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1248 $parent_file = $_wp_real_parent_file[$parent_file];
1249 return $parent_file;
1253 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
1254 $parent_file = $pagenow;
1255 if ( isset( $_wp_real_parent_file[$parent_file] ) )
1256 $parent_file = $_wp_real_parent_file[$parent_file];
1257 return $parent_file;
1260 foreach (array_keys( (array)$submenu ) as $parent) {
1261 foreach ( $submenu[$parent] as $submenu_array ) {
1262 if ( isset( $_wp_real_parent_file[$parent] ) )
1263 $parent = $_wp_real_parent_file[$parent];
1264 if ( !empty($typenow) && ($submenu_array[2] == "$pagenow?post_type=$typenow") ) {
1265 $parent_file = $parent;
1267 } elseif ( $submenu_array[2] == $pagenow && empty($typenow) && ( empty($parent_file) || false === strpos($parent_file, '?') ) ) {
1268 $parent_file = $parent;
1271 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
1272 $parent_file = $parent;
1278 if ( empty($parent_file) )
1283 function get_admin_page_title() {
1288 global $plugin_page;
1291 if ( ! empty ( $title ) )
1294 $hook = get_plugin_page_hook( $plugin_page, $pagenow );
1296 $parent = $parent1 = get_admin_page_parent();
1298 if ( empty ( $parent) ) {
1299 foreach ( (array)$menu as $menu_array ) {
1300 if ( isset( $menu_array[3] ) ) {
1301 if ( $menu_array[2] == $pagenow ) {
1302 $title = $menu_array[3];
1303 return $menu_array[3];
1305 if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
1306 $title = $menu_array[3];
1307 return $menu_array[3];
1310 $title = $menu_array[0];
1315 foreach ( array_keys( $submenu ) as $parent ) {
1316 foreach ( $submenu[$parent] as $submenu_array ) {
1317 if ( isset( $plugin_page ) &&
1318 ( $plugin_page == $submenu_array[2] ) &&
1320 ( $parent == $pagenow ) ||
1321 ( $parent == $plugin_page ) ||
1322 ( $plugin_page == $hook ) ||
1323 ( $pagenow == 'admin.php' && $parent1 != $submenu_array[2] ) ||
1324 ( !empty($typenow) && $parent == $pagenow . '?post_type=' . $typenow)
1327 $title = $submenu_array[3];
1328 return $submenu_array[3];
1331 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
1334 if ( isset( $submenu_array[3] ) ) {
1335 $title = $submenu_array[3];
1336 return $submenu_array[3];
1338 $title = $submenu_array[0];
1343 if ( empty ( $title ) ) {
1344 foreach ( $menu as $menu_array ) {
1345 if ( isset( $plugin_page ) &&
1346 ( $plugin_page == $menu_array[2] ) &&
1347 ( $pagenow == 'admin.php' ) &&
1348 ( $parent1 == $menu_array[2] ) )
1350 $title = $menu_array[3];
1351 return $menu_array[3];
1360 function get_plugin_page_hook( $plugin_page, $parent_page ) {
1361 $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
1362 if ( has_action($hook) )
1368 function get_plugin_page_hookname( $plugin_page, $parent_page ) {
1369 global $admin_page_hooks;
1371 $parent = get_admin_page_parent( $parent_page );
1373 $page_type = 'admin';
1374 if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
1375 if ( isset( $admin_page_hooks[$plugin_page] ) )
1376 $page_type = 'toplevel';
1378 if ( isset( $admin_page_hooks[$parent] ))
1379 $page_type = $admin_page_hooks[$parent];
1380 } else if ( isset( $admin_page_hooks[$parent] ) ) {
1381 $page_type = $admin_page_hooks[$parent];
1384 $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
1386 return $page_type . '_page_' . $plugin_name;
1389 function user_can_access_admin_page() {
1393 global $_wp_menu_nopriv;
1394 global $_wp_submenu_nopriv;
1395 global $plugin_page;
1396 global $_registered_pages;
1398 $parent = get_admin_page_parent();
1400 if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
1403 if ( isset( $plugin_page ) ) {
1404 if ( isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
1407 $hookname = get_plugin_page_hookname($plugin_page, $parent);
1409 if ( !isset($_registered_pages[$hookname]) )
1413 if ( empty( $parent) ) {
1414 if ( isset( $_wp_menu_nopriv[$pagenow] ) )
1416 if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
1418 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
1420 if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1422 foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
1423 if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
1425 if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
1431 if ( isset( $plugin_page ) && ( $plugin_page == $parent ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
1434 if ( isset( $submenu[$parent] ) ) {
1435 foreach ( $submenu[$parent] as $submenu_array ) {
1436 if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
1437 if ( current_user_can( $submenu_array[1] ))
1441 } else if ( $submenu_array[2] == $pagenow ) {
1442 if ( current_user_can( $submenu_array[1] ))
1450 foreach ( $menu as $menu_array ) {
1451 if ( $menu_array[2] == $parent) {
1452 if ( current_user_can( $menu_array[1] ))
1462 /* Whitelist functions */
1465 * Register a setting and its sanitization callback
1469 * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
1470 * Default whitelisted option key names include "general," "discussion," and "reading," among others.
1471 * @param string $option_name The name of an option to sanitize and save.
1472 * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
1475 function register_setting( $option_group, $option_name, $sanitize_callback = '' ) {
1476 global $new_whitelist_options;
1478 if ( 'misc' == $option_group ) {
1479 _deprecated_argument( __FUNCTION__, '3.0', __( 'The miscellaneous options group has been removed. Use another settings group.' ) );
1480 $option_group = 'general';
1483 $new_whitelist_options[ $option_group ][] = $option_name;
1484 if ( $sanitize_callback != '' )
1485 add_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1489 * Unregister a setting
1493 * @param unknown_type $option_group
1494 * @param unknown_type $option_name
1495 * @param unknown_type $sanitize_callback
1498 function unregister_setting( $option_group, $option_name, $sanitize_callback = '' ) {
1499 global $new_whitelist_options;
1501 if ( 'misc' == $option_group ) {
1502 _deprecated_argument( __FUNCTION__, '3.0', __( 'The miscellaneous options group has been removed. Use another settings group.' ) );
1503 $option_group = 'general';
1506 $pos = array_search( $option_name, (array) $new_whitelist_options );
1507 if ( $pos !== false )
1508 unset( $new_whitelist_options[ $option_group ][ $pos ] );
1509 if ( $sanitize_callback != '' )
1510 remove_filter( "sanitize_option_{$option_name}", $sanitize_callback );
1514 * {@internal Missing Short Description}}
1518 * @param unknown_type $options
1521 function option_update_filter( $options ) {
1522 global $new_whitelist_options;
1524 if ( is_array( $new_whitelist_options ) )
1525 $options = add_option_whitelist( $new_whitelist_options, $options );
1529 add_filter( 'whitelist_options', 'option_update_filter' );
1532 * {@internal Missing Short Description}}
1536 * @param unknown_type $new_options
1537 * @param unknown_type $options
1540 function add_option_whitelist( $new_options, $options = '' ) {
1541 if ( $options == '' )
1542 global $whitelist_options;
1544 $whitelist_options = $options;
1546 foreach ( $new_options as $page => $keys ) {
1547 foreach ( $keys as $key ) {
1548 if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
1549 $whitelist_options[ $page ] = array();
1550 $whitelist_options[ $page ][] = $key;
1552 $pos = array_search( $key, $whitelist_options[ $page ] );
1553 if ( $pos === false )
1554 $whitelist_options[ $page ][] = $key;
1559 return $whitelist_options;
1563 * {@internal Missing Short Description}}
1567 * @param unknown_type $del_options
1568 * @param unknown_type $options
1571 function remove_option_whitelist( $del_options, $options = '' ) {
1572 if ( $options == '' )
1573 global $whitelist_options;
1575 $whitelist_options = $options;
1577 foreach ( $del_options as $page => $keys ) {
1578 foreach ( $keys as $key ) {
1579 if ( isset($whitelist_options[ $page ]) && is_array($whitelist_options[ $page ]) ) {
1580 $pos = array_search( $key, $whitelist_options[ $page ] );
1581 if ( $pos !== false )
1582 unset( $whitelist_options[ $page ][ $pos ] );
1587 return $whitelist_options;
1591 * Output nonce, action, and option_page fields for a settings page.
1595 * @param string $option_group A settings group name. This should match the group name used in register_setting().
1597 function settings_fields($option_group) {
1598 echo "<input type='hidden' name='option_page' value='" . esc_attr($option_group) . "' />";
1599 echo '<input type="hidden" name="action" value="update" />';
1600 wp_nonce_field("$option_group-options");