Action Hooks (动作钩子)
wp_head
在 HTML <head>
部分插入代码。
function custom_wp_head() {
echo '<meta name="author" content="Your Name">';
}
add_action('wp_head', 'custom_wp_head');
wp_footer
在页面底部插入代码。
function custom_wp_footer() {
echo '<script src="https://example.com/custom.js"></script>';
}
add_action('wp_footer', 'custom_wp_footer');
init
WordPress 初始化时触发,用于注册自定义的 post type 和 taxonomy。
function custom_init() {
register_post_type('custom_type', [
'labels' => [
'name' => 'Custom Posts',
'singular_name' => 'Custom Post',
],
'public' => true,
'has_archive' => true,
]);
}
add_action('init', 'custom_init');
wp_loaded
在所有插件和主题加载完成后触发。
function on_wp_loaded() {
error_log('WordPress 已完全加载');
}
add_action('wp_loaded', 'on_wp_loaded');
admin_menu
在后台菜单加载时添加自定义菜单。
function custom_admin_menu() {
add_menu_page(
'Custom Menu',
'Custom Menu',
'manage_options',
'custom-menu',
'custom_menu_page_html'
);
}
function custom_menu_page_html() {
echo '<h1>这是自定义菜单页面</h1>';
}
add_action('admin_menu', 'custom_admin_menu');
save_post
当文章或页面保存时触发。
function on_save_post($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
// 保存时执行操作
}
add_action('save_post', 'on_save_post');
publish_post
文章发布时触发。
function on_publish_post($ID, $post) {
// 发布时执行的操作
}
add_action('publish_post', 'on_publish_post', 10, 2);
the_content
修改文章内容输出。
function modify_content($content) {
if (is_single()) {
$content .= '<p>感谢阅读!别忘了分享这篇文章!</p>';
}
return $content;
}
add_action('the_content', 'modify_content');
template_redirect
在加载模板之前执行操作,通常用于重定向。
function custom_template_redirect() {
if (is_page('old-page')) {
wp_redirect('https://example.com/new-page');
exit();
}
}
add_action('template_redirect', 'custom_template_redirect');
wp_ajax_{action}
处理 Ajax 请求。
function my_custom_ajax_action() {
$response = array('message' => '通过 Ajax 返回的数据');
echo json_encode($response);
wp_die();
}
add_action('wp_ajax_my_custom_action', 'my_custom_ajax_action');
user_register
用户注册时触发。
function on_user_register($user_id) {
// 用户注册后执行的操作
}
add_action('user_register', 'on_user_register');
after_setup_theme
主题初始化时执行,通常用来设置主题支持功能。
function theme_setup() {
add_theme_support('post-thumbnails');
}
add_action('after_setup_theme', 'theme_setup');
login_head
在 WordPress 登录页的 <head>
部分插入代码。
function custom_login_head() {
echo '<link rel="stylesheet" href="https://example.com/custom-login.css">';
}
add_action('login_head', 'custom_login_head');
shutdown
在 WordPress 关闭时执行,通常用于清理。
function on_shutdown() {
error_log('WordPress 正在关闭');
}
add_action('shutdown', 'on_shutdown');
comment_post
评论发布时触发。
function on_comment_post($comment_ID, $comment_approved) {
if ($comment_approved) {
// 评论通过后执行操作
}
}
add_action('comment_post', 'on_comment_post', 10, 2);
Filter Hooks (过滤钩子)
the_content
修改文章内容输出。
function modify_content($content) {
if (is_single()) {
$content .= '<p>感谢阅读!别忘了分享这篇文章!</p>';
}
return $content;
}
add_filter('the_content', 'modify_content');
the_title
修改文章标题。
function modify_title($title) {
if (is_single()) {
return $title . ' - 由 MySite 提供';
}
return $title;
}
add_filter('the_title', 'modify_title');
excerpt_length
修改文章摘要的长度。
function custom_excerpt_length($length) {
return 50; // 设置摘要为50个字符
}
add_filter('excerpt_length', 'custom_excerpt_length');
widget_title
修改小工具标题。
function custom_widget_title($title) {
return '小工具: ' . $title;
}
add_filter('widget_title', 'custom_widget_title');
post_thumbnail_html
修改文章缩略图的 HTML 输出。
function custom_thumbnail_html($html, $post_id) {
$html = str_replace('<img', '<img class="custom-thumbnail"', $html);
return $html;
}
add_filter('post_thumbnail_html', 'custom_thumbnail_html', 10, 2);
login_url
修改登录页 URL。
function custom_login_url($url) {
return home_url(); // 修改登录页面的 URL 指向首页
}
add_filter('login_url', 'custom_login_url');
sanitize_text_field
过滤文本字段的内容。
function custom_sanitize_text($text) {
return filter_var($text, FILTER_SANITIZE_STRING);
}
add_filter('sanitize_text_field', 'custom_sanitize_text');
pre_get_posts
修改 WordPress 查询参数。
function modify_main_query($query) {
if (!is_admin() && $query->is_main_query()) {
if (is_home()) {
// 在首页查询时只显示 5 篇文章
$query->set('posts_per_page', 5);
}
}
}
add_filter('pre_get_posts', 'modify_main_query');
option_{option_name}
修改选项的值(例如:siteurl
)。
function custom_option_value($value) {
if ('siteurl' === get_option('option_name')) {
return 'https://new-url.com';
}
return $value;
}
add_filter('option_siteurl', 'custom_option_value');
widget_text
修改小工具文本的内容。
function custom_widget_text($text) {
return $text . ' - 显示更多内容!';
}
add_filter('widget_text', 'custom_widget_text');
redirect_canonical
在执行重定向时触发,可以用来修改重定向的 URL。
function custom_redirect($url) {
if (is_page('old-page')) {
return home_url();
}
return $url;
}
add_filter('redirect_canonical', 'custom_redirect');
body_class
修改页面 <body>
标签的 class 名称。
function custom_body_class($classes) {
$classes[] = 'custom-class';
return $classes;
}
add_filter('body_class', 'custom_body_class');