wp_customize_url()
云策文档标注
概述
wp_customize_url() 函数用于生成加载 WordPress 自定义工具(Customizer)的 URL。它接受一个可选的主题样式表参数,并返回经过编码和清理的 URL。
关键要点
- 函数返回一个指向 admin_url('customize.php') 的 URL,用于加载 Customizer。
- 可选参数 $stylesheet 允许指定要自定义的主题,默认为当前活动主题。
- 如果提供了 $stylesheet 参数,函数会使用 add_query_arg() 添加 'theme' 查询参数,并自动进行 urlencode 编码。
- 返回的 URL 通过 esc_url() 进行清理以确保安全性。
代码示例
function wp_customize_url( $stylesheet = '' ) {
$url = admin_url( 'customize.php' );
if ( $stylesheet ) {
$url = add_query_arg( 'theme', urlencode( $stylesheet ), $url );
}
return esc_url( $url );
}注意事项
- 该函数自 WordPress 3.4.0 版本引入。
- 相关函数包括 esc_url()、add_query_arg() 和 admin_url(),用于 URL 处理和生成。
- 在多个核心功能中被使用,如 AJAX 主题安装、工具栏菜单和主题列表生成。
原文内容
Returns a URL to load the Customizer.
Parameters
$stylesheetstringoptional-
Theme to customize. Defaults to active theme.
The theme’s stylesheet will be urlencoded if necessary.
Source
function wp_customize_url( $stylesheet = '' ) {
$url = admin_url( 'customize.php' );
if ( $stylesheet ) {
$url = add_query_arg( 'theme', urlencode( $stylesheet ), $url );
}
return esc_url( $url );
}
Changelog
| Version | Description |
|---|---|
| 3.4.0 | Introduced. |