wp_enqueue_code_editor
云策文档标注
概述
wp_enqueue_code_editor 是一个 WordPress 动作钩子,在代码编辑器的脚本和样式被入队时触发。它允许开发者自定义代码编辑器的设置,常用于后台管理界面。
关键要点
- 这是一个动作钩子,用于在代码编辑器资源入队时执行自定义代码。
- 参数 $settings 是一个数组,用于传递代码编辑器的配置设置。
- 钩子通过 do_action( 'wp_enqueue_code_editor', $settings ) 调用。
- 与 wp_enqueue_code_editor() 函数关联,该函数用于入队代码编辑器资源。
- 自 WordPress 4.9.0 版本引入。
代码示例
add_action( 'admin_enqueue_scripts', function() {
if ( 'profile' !== get_current_screen()->id ) {
return;
}
// Enqueue code editor and settings for manipulating HTML.
$settings = wp_enqueue_code_editor( array( 'type' => 'text/html' ) );
// Bail if user disabled CodeMirror.
if ( false === $settings ) {
return;
}
wp_add_inline_script(
'code-editor',
sprintf(
'jQuery( function() { wp.codeEditor.initialize( "description", %s ); } );',
wp_json_encode( $settings )
)
);
} );注意事项
示例代码展示了如何在特定管理屏幕(如用户资料页)入队代码编辑器,并初始化编辑器。注意检查 $settings 是否为 false,以防用户禁用 CodeMirror。
原文内容
Fires when scripts and styles are enqueued for the code editor.
Parameters
$settingsarray-
Settings for the enqueued code editor.
Source
do_action( 'wp_enqueue_code_editor', $settings );
Changelog
| Version | Description |
|---|---|
| 4.9.0 | Introduced. |
Skip to note 2 content
Guido Scialfa
/* * See <a href="https://make.wordpress.org/core/2017/10/22/code-editing-improvements-in-wordpress-4-9/" rel="nofollow ugc">https://make.wordpress.org/core/2017/10/22/code-editing-improvements-in-wordpress-4-9/</a> */ add_action( 'admin_enqueue_scripts', function() { if ( 'profile' !== get_current_screen()->id ) { return; } // Enqueue code editor and settings for manipulating HTML. $settings = wp_enqueue_code_editor( array( 'type' => 'text/html' ) ); // Bail if user disabled CodeMirror. if ( false === $settings ) { return; } wp_add_inline_script( 'code-editor', sprintf( 'jQuery( function() { wp.codeEditor.initialize( "description", %s ); } );', wp_json_encode( $settings ) ) ); } );