钩子文档

admin_url

💡 云策文档标注

概述

admin_url 是一个 WordPress 过滤器,用于修改管理区域 URL。它允许开发者基于参数自定义 URL,常用于添加查询参数或调整路径。

关键要点

  • 过滤器名称:admin_url,用于过滤管理区域 URL。
  • 参数:$url(完整 URL)、$path(相对路径)、$blog_id(站点 ID)、$scheme(协议方案)。
  • 应用场景:基于用户角色或访问页面动态修改管理 URL,例如添加自定义查询参数。

代码示例

function wpdocs_advanced_custom_admin_url( $url, $path ) {
    // Check if the current user is an administrator
    if ( current_user_can( 'administrator' ) ) {
        // Check if the user is accessing the plugins page
        if ( strpos( $path, 'plugins.php' ) !== false ) {
            // Add a different custom query parameter
            $url = add_query_arg( 'plugins_param', 'value', $url );
        } else {
            // Add the default custom query parameter
            $url = add_query_arg( 'custom_param', 'value', $url );
        }
    }

    return $url;
}
add_filter( 'admin_url', 'wpdocs_advanced_custom_admin_url', 10, 2 );

📄 原文内容

Filters the admin area URL.

Parameters

$urlstring
The complete admin area URL including scheme and path.
$pathstring
Path relative to the admin area URL. Blank string if no path is specified.
$blog_idint|null
Site ID, or null for the current site.
$schemestring|null
The scheme to use. Accepts 'http', 'https', 'admin', or null. Default 'admin', which obeys force_ssl_admin() and is_ssl() .

Source

return apply_filters( 'admin_url', $url, $path, $blog_id, $scheme );

Changelog

Version Description
5.8.0 The $scheme parameter was added.
2.8.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    function wpdocs_advanced_custom_admin_url( $url, $path ) {
        // Check if the current user is an administrator
        if ( current_user_can( 'administrator' ) ) {
            // Check if the user is accessing the plugins page
            if ( strpos( $path, 'plugins.php' ) !== false ) {
                // Add a different custom query parameter
                $url = add_query_arg( 'plugins_param', 'value', $url );
            } else {
                // Add the default custom query parameter
                $url = add_query_arg( 'custom_param', 'value', $url );
            }
        }
    
        return $url;
    }
    add_filter( 'admin_url', 'wpdocs_advanced_custom_admin_url', 10, 2 );