admin_print_footer_scripts
云策文档标注
概述
admin_print_footer_scripts 是一个 WordPress 动作钩子,用于在管理后台页脚打印已排队的脚本和数据。开发者可以利用此钩子添加自定义 JavaScript 代码来修改仪表盘页面。
关键要点
- admin_print_footer_scripts 是一个 do_action 钩子,在管理后台页脚触发,用于输出脚本和数据。
- 此钩子常用于插件或主题开发中,以在特定管理页面注入自定义 JavaScript 代码。
- 使用时应结合页面检查(如检查 post_type 或 get_current_screen())来确保代码仅在目标页面执行,避免影响其他页面。
代码示例
function admin_footer_js(){
// 检查是否为自定义文章类型页面
if(!isset($_GET['post_type']) || false === strpos($_GET['post_type'],'my-cpt') ){
return false;
}
// 使用 get_current_screen() 进一步验证页面
$screen = get_current_screen();
if ( 'edit' != $screen->base && '' != $screen->action ){
return;
}
// 在此处添加自定义 JavaScript 代码
echo "";
}
add_action( 'admin_print_footer_scripts', 'admin_footer_js' );注意事项
- 在使用 get_current_screen() 前,建议先进行基本页面检查,以避免在某些管理页面上出现致命错误。
- 确保代码仅在需要修改的页面执行,以提高性能和安全性。
原文内容
Prints any scripts and data queued for the footer.
Source
do_action( 'admin_print_footer_scripts' );
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
Skip to note 2 content
Aurovrata Venet
You can use this hook to insert footer javascript to alter dashboard pages,
function admin_footer_js(){ //you should check to make sure this is the page your want to alter first, check if this your custom post edtit page // for example .wp-admin/edit.php?post_type=my-cpt would be your custom post table list page. if(!isset($_GET['post_type']) || false === strpos($_GET['post_type'],'my-cpt') ){ return false; } //get_current_screen() can result in fatal error on some admin pages, hence I use it after a basic check above $screen = get_current_screen(); if ( 'edit' != $screen->base && '' != $screen->action ){ return; } //now we can sure this is our page. echo "<script type='text/javascript'>n"; echo 'var pluginUrl = ' . wp_json_encode( WP_PLUGIN_URL . '/my_plugin/' ) . ';'; echo "n</script>"; } add_action( 'admin_print_footer_scripts', 'admin_footer_js' );