validate_plugin()
云策文档标注
概述
validate_plugin() 函数用于验证插件路径的有效性,检查主插件文件是否存在且具有有效的插件头信息。它基于 validate_file() 进行路径验证,并返回成功或 WP_Error 错误。
关键要点
- 验证插件路径相对于插件目录的字符串参数 $plugin
- 使用 validate_file() 检查路径格式,无效时返回 WP_Error('plugin_invalid')
- 检查文件是否存在,不存在时返回 WP_Error('plugin_not_found')
- 通过 get_plugins() 验证插件头信息,缺失时返回 WP_Error('no_plugin_header')
- 成功时返回整数 0,失败时返回 WP_Error 对象
代码示例
function validate_plugin( $plugin ) {
if ( validate_file( $plugin ) ) {
return new WP_Error( 'plugin_invalid', __( 'Invalid plugin path.' ) );
}
if ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) {
return new WP_Error( 'plugin_not_found', __( 'Plugin file does not exist.' ) );
}
$installed_plugins = get_plugins();
if ( ! isset( $installed_plugins[ $plugin ] ) ) {
return new WP_Error( 'no_plugin_header', __( 'The plugin does not have a valid header.' ) );
}
return 0;
}
原文内容
Validates the plugin path.
Description
Checks that the main plugin file exists and is a valid plugin. See validate_file() .
Parameters
$pluginstringrequired-
Path to the plugin file relative to the plugins directory.
Source
function validate_plugin( $plugin ) {
if ( validate_file( $plugin ) ) {
return new WP_Error( 'plugin_invalid', __( 'Invalid plugin path.' ) );
}
if ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) {
return new WP_Error( 'plugin_not_found', __( 'Plugin file does not exist.' ) );
}
$installed_plugins = get_plugins();
if ( ! isset( $installed_plugins[ $plugin ] ) ) {
return new WP_Error( 'no_plugin_header', __( 'The plugin does not have a valid header.' ) );
}
return 0;
}
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |