admin_footer
云策文档标注
概述
admin_footer 是一个 WordPress 管理后台专用的 Hook,用于在默认页脚脚本之前输出脚本或数据。它仅适用于管理界面,不能在前端使用。
关键要点
- admin_footer 是一个 Action Hook,在 admin-footer.php 页面的 </body> 标签关闭后、admin_print_footer_scripts 动作调用前触发。
- 此 Hook 仅用于管理后台,不能在前端添加任何内容。
- 使用 add_action('admin_footer', 'your_function') 来挂载自定义函数,函数应直接输出数据而非返回。
- 可以通过 add_action 的第三个参数设置优先级,数字越低优先级越高。
代码示例
// 示例:在管理页脚输出 HTML
function my_admin_footer_function() {
echo '<div>' . __( 'This will be inserted at the bottom of admin page', 'textdomain' ) . '</div>';
}
add_action('admin_footer', 'my_admin_footer_function');
// 示例:添加 JavaScript 函数
function my_admin_footer_js() {
echo '<script>alert("This will trigger an alert on the admin page.");</script>';
}
add_action('admin_footer', 'my_admin_footer_js');
// 示例:设置优先级
function my_admin_footer_priority() {
echo '<div>This will be inserted at the bottom of admin page</div>';
}
add_action('admin_footer', 'my_admin_footer_priority', 5); // 优先级为 5注意事项
- 函数应使用 echo 输出数据,而不是 return,否则内容不会显示。
- 确保仅在管理后台使用此 Hook,避免影响前端性能或功能。
原文内容
Prints scripts or data before the default footer scripts.
Parameters
$datastring-
The data to print.
Skip to note 5 content
rmdundon
Actually, you are not supposed to return data, but rather echo it!
This example should work:
function my_admin_footer_function() { echo '<p>' . __( 'This will be inserted at the bottom of admin page', 'textdomain' ) . '</p>'; } add_action('admin_footer', 'my_admin_footer_function');Skip to note 6 content
Steven Lin
Example migrated from Codex:
Add HTML in the footer of your admin page
This will be inserted at the bottom of admin page</p>'; return $data; } ?>Skip to note 7 content
Steven Lin
Example migrated from Codex:
Adding a JS function
alert("This will trigger an alert on the admin page.")</script>'; return $data; } ?>Skip to note 8 content
Steven Lin
Example migrated from Codex:
Set priority of your action
You can set the priority of your action by passing the third parameter. Lower the number to higher the priority.
This will be inserted at the bottom of admin page</p>'; return $data; } ?>