admin_url()
概述
admin_url() 函数用于获取当前站点的管理区域 URL,支持附加路径和指定协议方案。它是 WordPress 核心函数,常用于生成后台链接。
关键要点
- 函数返回当前站点的管理区域 URL,默认基于 force_ssl_admin() 和 is_ssl() 确定协议。
- 参数 $path 可选,用于附加相对路径到管理 URL,如 'edit-tags.php?taxonomy=category'。
- 参数 $scheme 可选,可指定 'admin'(默认)、'http' 或 'https' 来强制协议。
- 函数内部调用 get_admin_url(),简化了获取管理 URL 的过程。
- 在 WordPress 2.6.0 版本中引入,广泛用于插件和主题开发中生成后台链接。
代码示例
// 基本用法:获取管理区域 URL
echo admin_url(); // 输出如 http://example.com/wp-admin/
// 附加路径并强制 HTTPS
echo admin_url('edit-tags.php?taxonomy=category', 'https'); // 输出如 https://example.com/wp-admin/edit-tags.php?taxonomy=category
// 路径中的前导斜杠会被自动移除,不影响输出
admin_url('test'); // 返回 http://example.com/wp-admin/test
admin_url('/test'); // 同样返回 http://example.com/wp-admin/test注意事项
- 使用 $scheme 参数时,'admin' 值会根据站点 SSL 设置自动选择协议,而 'http' 或 'https' 会强制使用指定协议。
- 路径参数 $path 应使用相对路径,避免包含域名,以确保 URL 正确构建。
- 该函数常用于生成后台页面链接,如编辑文章或分类页面,结合 add_query_arg() 可进一步定制查询参数。
Retrieves the URL to the admin area for the current site.
Parameters
$pathstringoptional-
Path relative to the admin URL. Default empty.
$schemestringoptional-
The scheme to use. Default is
'admin', which obeys force_ssl_admin() and is_ssl() .
'http'or'https'can be passed to force those schemes.
Return
string Admin URL link with optional path appended.
Source
function admin_url( $path = '', $scheme = 'admin' ) {
return get_admin_url( null, $path, $scheme );
}
Related
| Uses | Description |
|---|---|
get_admin_url()wp-includes/link-template.php |
Retrieves the URL to the admin area for a given site. |
| Used by | Description |
|---|---|
WP_Site_Health::get_test_search_engine_visibility()wp-admin/includes/class-wp-site-health.php |
Tests whether search engine indexing is enabled. |
WP_Plugin_Dependencies::check_plugin_dependencies_during_ajax()wp-includes/class-wp-plugin-dependencies.php |
Checks plugin dependencies after a plugin is installed via AJAX. |
WP_Plugin_Dependencies::display_admin_notice_for_unmet_dependencies()wp-includes/class-wp-plugin-dependencies.php |
Displays an admin notice if dependencies are not installed. |
WP_Widget_Media::get_l10n_defaults()wp-includes/widgets/class-wp-widget-media.php |
Returns the default localized strings used by the widget. |
wp_admin_bar_edit_site_menu()wp-includes/admin-bar.php |
Adds the “Edit Site” link to the Toolbar. |
get_default_block_editor_settings()wp-includes/block-editor.php |
Returns the default block editor settings. |
deactivated_plugins_notice()wp-admin/includes/plugin.php |
Renders an admin notice when a plugin was deactivated during an update. |
rest_add_application_passwords_to_index()wp-includes/rest-api.php |
Adds Application Passwords info to the REST API index. |
WP_Site_Health::get_test_authorization_header()wp-admin/includes/class-wp-site-health.php |
Tests if the Authorization header has the expected values. |
WP_Automatic_Updater::send_plugin_theme_email()wp-admin/includes/class-wp-automatic-updater.php |
Sends an email upon the completion or failure of a plugin or theme background update. |
wp_dashboard_site_health()wp-admin/includes/dashboard.php |
Displays the Site Health Status widget. |
WP_Site_Health::wp_cron_scheduled_check()wp-admin/includes/class-wp-site-health.php |
Runs the scheduled event to check and update the latest site health status for the website. |
WP_Privacy_Requests_Table::get_admin_url()wp-admin/includes/class-wp-privacy-requests-table.php |
Normalizes the admin URL to the current page (by request_type). |
WP_Privacy_Data_Removal_Requests_List_Table::column_email()wp-admin/includes/class-wp-privacy-data-removal-requests-list-table.php |
Outputs the Actions column. |
WP_Privacy_Data_Removal_Requests_List_Table::column_next_steps()wp-admin/includes/class-wp-privacy-data-removal-requests-list-table.php |
Outputs the Next steps column. |
WP_Privacy_Data_Export_Requests_List_Table::column_email()wp-admin/includes/class-wp-privacy-data-export-requests-list-table.php |
Actions column. |
WP_Privacy_Data_Export_Requests_List_Table::column_next_steps()wp-admin/includes/class-wp-privacy-data-export-requests-list-table.php |
Displays the next steps column. |
WP_Recovery_Mode::handle_exit_recovery_mode()wp-includes/class-wp-recovery-mode.php |
Handles a request to exit Recovery Mode. |
paused_themes_notice()wp-admin/includes/theme.php |
Renders an admin notice in case some themes have been paused due to errors. |
WP_Site_Health::get_test_wordpress_version()wp-admin/includes/class-wp-site-health.php |
Tests for WordPress version and outputs it. |
WP_Site_Health::get_test_plugin_version()wp-admin/includes/class-wp-site-health.php |
Tests if plugins are outdated, or unnecessary. |
WP_Site_Health::get_test_theme_version()wp-admin/includes/class-wp-site-health.php |
Tests if themes are outdated, or unnecessary. |
WP_Site_Health::get_test_https_status()wp-admin/includes/class-wp-site-health.php |
Tests if the site is serving content over HTTPS. |
paused_plugins_notice()wp-admin/includes/plugin.php |
Renders an admin notice in case some plugins have been paused due to errors. |
validate_plugin_requirements()wp-admin/includes/plugin.php |
Validates the plugin requirements for WordPress version and PHP version. |
wp_default_packages_inline_scripts()wp-includes/script-loader.php |
Adds inline scripts required for the WordPress JavaScript packages. |
the_block_editor_meta_boxes()wp-admin/includes/post.php |
Renders the meta boxes forms. |
_wp_privacy_send_request_confirmation_notification()wp-includes/user.php |
Notifies the site administrator via email when a request is confirmed. |
WP_Privacy_Policy_Content::notice()wp-admin/includes/class-wp-privacy-policy-content.php |
Adds a notice with a link to the guide when editing the privacy policy page. |
WP_Privacy_Policy_Content::policy_text_changed_notice()wp-admin/includes/class-wp-privacy-policy-content.php |
Outputs a warning when some privacy info has changed. |
wp_load_press_this()wp-admin/press-this.php |
|
wp_edit_theme_plugin_file()wp-admin/includes/file.php |
Attempts to edit a file for a theme or plugin. |
WP_Widget_Media_Audio::__construct()wp-includes/widgets/class-wp-widget-media-audio.php |
Constructor. |
WP_Widget_Media_Video::__construct()wp-includes/widgets/class-wp-widget-media-video.php |
Constructor. |
WP_Widget_Media_Image::__construct()wp-includes/widgets/class-wp-widget-media-image.php |
Constructor. |
wp_print_community_events_markup()wp-admin/includes/dashboard.php |
Prints the markup for the Community Events section of the Events and News Dashboard widget. |
WP_Customize_Manager::is_cross_domain()wp-includes/class-wp-customize-manager.php |
Determines whether the admin and the frontend are on different domains. |
wp_ajax_install_theme()wp-admin/includes/ajax-actions.php |
Handles installing a theme via AJAX. |
wp_ajax_install_plugin()wp-admin/includes/ajax-actions.php |
Handles installing a plugin via AJAX. |
WP_Customize_Manager::get_return_url()wp-includes/class-wp-customize-manager.php |
Gets URL to link the user to when closing the Customizer. |
WP_Customize_Manager::customize_pane_settings()wp-includes/class-wp-customize-manager.php |
Prints JavaScript settings for parent window. |
WP_Posts_List_Table::handle_row_actions()wp-admin/includes/class-wp-posts-list-table.php |
Generates and displays row action links. |
network_step1()wp-admin/includes/network.php |
Prints step 1 for Network installation process. |
WP_Automatic_Updater::send_email()wp-admin/includes/class-wp-automatic-updater.php |
Sends an email upon the completion or failure of a background core update. |
wp_prepare_themes_for_js()wp-admin/includes/theme.php |
Prepares themes for JavaScript. |
get_theme_update_available()wp-admin/includes/theme.php |
Retrieves the update link if there is a theme update available. |
WP_Plugins_List_Table::no_items()wp-admin/includes/class-wp-plugins-list-table.php |
|
Theme_Upgrader_Skin::after()wp-admin/includes/class-theme-upgrader-skin.php |
Performs an action following a single theme update. |
Plugin_Installer_Skin::after()wp-admin/includes/class-plugin-installer-skin.php |
Performs an action following a plugin install. |
Theme_Installer_Skin::after()wp-admin/includes/class-theme-installer-skin.php |
Performs an action following a single theme install. |
WP_List_Table::comments_bubble()wp-admin/includes/class-wp-list-table.php |
Displays a comment count bubble. |
wp_image_editor()wp-admin/includes/image-edit.php |
Loads the WP image-editing interface. |
wp_welcome_panel()wp-admin/includes/dashboard.php |
Displays a welcome panel to introduce users to WordPress. |
wp_dashboard_quota()wp-admin/includes/dashboard.php |
Displays file upload quota on dashboard. |
wp_dashboard_quick_press()wp-admin/includes/dashboard.php |
Displays the Quick Draft widget. |
wp_dashboard_recent_drafts()wp-admin/includes/dashboard.php |
Show recent drafts of the user on the dashboard. |
menu_page_url()wp-admin/includes/plugin.php |
Gets the URL to access a particular menu page based on the slug it was registered with. |
wp_install_defaults()wp-admin/includes/upgrade.php |
Creates the initial content for a newly-installed site. |
iframe_header()wp-admin/includes/template.php |
Generic Iframe header for use with Thickbox. |
WP_Themes_List_Table::no_items()wp-admin/includes/class-wp-themes-list-table.php |
|
media_upload_type_form()wp-admin/includes/media.php |
Outputs the legacy media upload form for a given media type. |
media_upload_type_url_form()wp-admin/includes/media.php |
Outputs the legacy media upload form for external media. |
media_upload_gallery_form()wp-admin/includes/media.php |
Adds gallery form to upload iframe. |
media_upload_library_form()wp-admin/includes/media.php |
Outputs the legacy media upload form for the media library. |
media_upload_max_image_resize()wp-admin/includes/media.php |
Displays the checkbox to scale images. |
media_upload_form()wp-admin/includes/media.php |
Outputs the legacy media upload form. |
wp_iframe()wp-admin/includes/media.php |
Outputs the iframe to display the media upload page. |
get_upload_iframe_src()wp-admin/includes/media.php |
Retrieves the upload iframe source URL. |
_admin_notice_post_locked()wp-admin/includes/post.php |
Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post. |
wp_ajax_query_themes()wp-admin/includes/ajax-actions.php |
Handles getting themes from themes_api() via AJAX. |
wp_prepare_revisions_for_js()wp-admin/includes/revision.php |
Prepare revisions for JavaScript. |
update_core()wp-admin/includes/update-core.php |
Upgrades the core of WordPress. |
post_submit_meta_box()wp-admin/includes/meta-boxes.php |
Displays post submit form fields. |
WP_Comments_List_Table::column_author()wp-admin/includes/class-wp-comments-list-table.php |
|
WP_Comments_List_Table::get_views()wp-admin/includes/class-wp-comments-list-table.php |
|
Walker_Nav_Menu_Edit::start_el()wp-admin/includes/class-walker-nav-menu-edit.php |
Start the element output. |
Custom_Image_Header::step_1()wp-admin/includes/class-custom-image-header.php |
Displays first step of custom header image page. |
Custom_Image_Header::help()wp-admin/includes/class-custom-image-header.php |
Adds contextual help. |
Custom_Background::admin_page()wp-admin/includes/class-custom-background.php |
Displays the custom background page. |
wp_customize_url()wp-includes/theme.php |
Returns a URL to load the Customizer. |
wp_customize_support_script()wp-includes/theme.php |
Prints a script to check whether or not the Customizer is supported, and apply either the no-customize-support or customize-support class to the body. |
_wp_customize_loader_settings()wp-includes/theme.php |
Adds settings for the customize-loader script. |
wp_safe_redirect()wp-includes/pluggable.php |
Performs a safe (local) redirect, using wp_redirect() . |
wp_notify_postauthor()wp-includes/pluggable.php |
Notifies an author (and/or others) of a comment/trackback/pingback on a post. |
wp_notify_moderator()wp-includes/pluggable.php |
Notifies the moderator of the site about a new comment that is awaiting approval. |
check_admin_referer()wp-includes/pluggable.php |
Ensures intent by verifying that a user was referred from another admin page with the correct security nonce. |
wp_heartbeat_settings()wp-includes/general-template.php |
Default settings for heartbeat. |
register_admin_color_schemes()wp-includes/general-template.php |
Registers the default admin color schemes. |
wp_admin_css_uri()wp-includes/general-template.php |
Displays the URL of a WordPress admin CSS file. |
wp_register()wp-includes/general-template.php |
Displays the Registration or Admin link. |
wp_admin_bar_dashboard_view_site_menu()wp-includes/deprecated.php |
Add the “Dashboard”/”Visit Site” menu. |
WP_Nav_Menu_Widget::form()wp-includes/widgets/class-wp-nav-menu-widget.php |
Outputs the settings form for the Navigation Menu widget. |
WP_Embed::maybe_run_ajax_cache()wp-includes/class-wp-embed.php |
If a post/page was saved, then output JavaScript to make an Ajax request that will call WP_Embed::cache_oembed(). |
self_admin_url()wp-includes/link-template.php |
Retrieves the URL to the admin area for either the current site or the network depending on context. |
get_dashboard_url()wp-includes/link-template.php |
Retrieves the URL to the user’s dashboard. |
network_admin_url()wp-includes/link-template.php |
Retrieves the URL to the admin area for the network. |
get_edit_post_link()wp-includes/link-template.php |
Retrieves the edit post link for post. |
get_delete_post_link()wp-includes/link-template.php |
Retrieves the delete posts link for post. |
get_edit_comment_link()wp-includes/link-template.php |
Retrieves the edit comment link. |
get_edit_bookmark_link()wp-includes/link-template.php |
Displays the edit bookmark link. |
get_edit_term_link()wp-includes/link-template.php |
Retrieves the URL for editing a given term. |
get_allowed_http_origins()wp-includes/http.php |
Retrieves list of allowed HTTP origins. |
wp_admin_bar_site_menu()wp-includes/admin-bar.php |
Adds the “Site Name” menu. |
wp_admin_bar_my_sites_menu()wp-includes/admin-bar.php |
Adds the “My Sites/[Site Name]” menu and all submenus. |
wp_admin_bar_new_content_menu()wp-includes/admin-bar.php |
Adds “Add New” menu. |
wp_admin_bar_comments_menu()wp-includes/admin-bar.php |
Adds edit comments link with awaiting moderation count bubble. |
wp_admin_bar_appearance_menu()wp-includes/admin-bar.php |
Adds appearance submenu items to the “Site Name” menu. |
wp_user_settings()wp-includes/option.php |
Saves and restores user interface settings stored in a cookie. |
wp_plupload_default_settings()wp-includes/media.php |
Prints default Plupload arguments. |
wp_enqueue_media()wp-includes/media.php |
Enqueues all scripts, styles, settings, and templates necessary to use all media JS APIs. |
wp_redirect_admin_locations()wp-includes/canonical.php |
Redirects a variety of shorthand URLs to the admin. |
maybe_add_existing_user_to_blog()wp-includes/ms-functions.php |
Adds a new user to a blog by visiting /newbloguser/{key}/. |
wp_default_scripts()wp-includes/script-loader.php |
Registers all WordPress scripts. |
_WP_Editors::editor_js()wp-includes/class-wp-editor.php |
Print (output) the TinyMCE configuration and initialization scripts. |
wp_print_media_templates()wp-includes/media-template.php |
Prints the templates used in the media manager. |
Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |
Skip to note 7 content
Codex
Examples
Output: http://example.com/wp-admin/ (or https protocol when appropriate)
// Generate URL to admin's "Categories" page, and force httpsOutput: https://example.com/wp-admin/edit-tags.php?taxonomy=category
Skip to note 8 content
Matt Calvert
If you are looking for the post edit url for admin end and you have the post id (suppose
$post_id) with you, then you can use the following code for getting the url.$post_id = 1731; // For example $post_url = add_query_arg( array( 'post' => $post_id, 'action' => 'edit', ), admin_url( 'post.php' ) ); // returns <a href="http://example.com/wp-admin/post.php?post=1731&action=edit" rel="nofollow ugc">http://example.com/wp-admin/post.php?post=1731&action;=edit</a>Skip to note 9 content
Codex
Default Usage
Skip to note 10 content
Kowsar Hossain
The presence of a leading slash in the path doesn’t affect the output, as it will be internally removed. Thus, both calls to
admin_url()will yield the same result:admin_url( 'test' ); // returns <a href="http://example.com/wp-admin/test" rel="nofollow ugc">http://example.com/wp-admin/test</a> admin_url( '/test' ); // returns <a href="http://example.com/wp-admin/test" rel="nofollow ugc">http://example.com/wp-admin/test</a>Skip to note 11 content
israelakinola
Logout Users based on specific capabilities and role
if ( ! current_user_can( 'edit_posts' ) && ! is_admin() ) { $redirect_url = site_url(); } else { $redirect_url = '/wp-login.php'; } function wpdocs_redirect_after_logout() { global $redirect_url; wp_safe_redirect( $redirect_url ); exit; } add_action( 'wp_logout', 'wpdocs_redirect_after_logout' );Skip to note 12 content
Codex
Usage