如何打印wordpress所有的钩子

作者

制作wodpress模板,或者插件的时候,经常用到wordpress的钩子。对于不同的访问页面,wordpress的钩子可能是不同的,比如访文章和访问后台,或者更新一篇文章部分钩子是不同的。当然更多的钩子还是通用的,意思是不管访问那些页面,这些钩子都存在。wordrpess的所有钩子都是存储在全局变量 $GLOBALS['wp_filter'] 中的,只要打印这个值就知道当前都用什么钩子。

wordpress打印所有的钩子
wordpress打印所有的钩子

php退出的时候,打印一下wordpress所有的钩子

ob_start('show_wp_filter');
function show_wp_filter($html) {
    echo "<!--";
    foreach($GLOBALS['wp_filter'] as $k => $v) {
        echo  $k . "\n";
    }
    echo "-->";
    return $html;
}

如果是访问首页,打印的结果可能是

<!--query
pre_term_name
pre_comment_author_name
pre_link_name
pre_link_target
pre_link_rel
pre_user_display_name
pre_user_first_name
pre_user_last_name
pre_user_nickname
term_name
comment_author_name
link_name
link_target
link_rel
user_display_name
user_first_name
user_last_name
user_nickname
pre_term_description
pre_link_description
pre_link_notes
pre_user_description
pre_comment_author_email
pre_user_email
comment_author_email
user_email
pre_comment_author_url
pre_user_url
pre_link_url
pre_link_image
pre_link_rss
pre_post_guid
user_url
link_url
link_image
link_rss
comment_url
post_guid
pre_term_slug
wp_insert_post_data
pre_post_type
pre_post_status
pre_post_comment_status
pre_post_ping_status
pre_post_mime_type
post_mime_type
register_meta_args
content_save_pre
excerpt_save_pre
comment_save_pre
pre_comment_content
comment_author
link_description
link_notes
bloginfo
wp_title
widget_title
the_content
the_title
comment_text
single_post_title
single_cat_title
single_tag_title
single_month_title
nav_menu_attr_title
nav_menu_description
term_description
get_the_post_type_description
term_name_rss
wp_insert_post_parent
wp_update_term_parent
the_excerpt
get_the_excerpt
the_post_thumbnail_caption
comment_excerpt
list_cats
wp_sprintf
widget_text
widget_text_content
date_i18n
the_title_rss
the_content_rss
the_content_feed
the_excerpt_rss
comment_author_rss
comment_text_rss
bloginfo_rss
the_author
the_guid
wp_mail
publish_post
publish_page
wp_ajax_save-widget
wp_ajax_widgets-order
customize_save_after
option_ping_sites
option_blog_charset
option_home
option_siteurl
tiny_mce_before_init
teeny_mce_before_init
pre_kses
sanitize_title
check_comment_flood
comment_flood_filter
comment_email
option_tag_base
option_category_base
the_posts
comments_open
pings_open
editable_slug
nav_menu_meta_box_object
pingback_ping_source_uri
xmlrpc_pingback_error
title_save_pre
transition_comment_status
http_request_host_is_external
xmlrpc_rsd_apis
wp_head
template_redirect
auth_cookie_malformed
auth_cookie_expired
auth_cookie_bad_username
auth_cookie_bad_hash
auth_cookie_valid
rest_authentication_errors
publish_future_post
wp_footer
wp_print_footer_scripts
init
after_switch_theme
wp_print_styles
login_head
login_footer
login_init
rss2_head
commentsrss2_head
rss_head
rdf_header
atom_head
comments_atom_head
opml_head
app_head
do_feed_rdf
do_feed_rss
do_feed_rss2
do_feed_atom
do_pings
do_robots
set_comment_cookies
sanitize_comment_cookies
admin_print_scripts
admin_print_footer_scripts
admin_print_styles
plugins_loaded
shutdown
post_updated
transition_post_status
comment_form
admin_init
welcome_panel
user_request_action_confirmed
wp_privacy_personal_data_exporters
wp_privacy_personal_data_erasers
wp_privacy_delete_old_export_files
wp_scheduled_delete
wp_scheduled_auto_draft_delete
importer_scheduled_cleanup
upgrader_scheduled_cleanup
delete_expired_transients
delete_post
delete_term
begin_fetch_post_thumbnail_html
end_fetch_post_thumbnail_html
attachment_updated
pre_option_gmt_offset
admin_color_scheme_picker
default_option_link_manager_enabled
pre_option_embed_autourls
heartbeat_settings
admin_enqueue_scripts
heartbeat_send
heartbeat_nopriv_send
authenticate
determine_current_user
split_shared_term
wp_split_shared_term_batch
comment_post
after_password_reset
register_new_user
edit_user_created_user
rest_api_init
parse_request
wp_loaded
delete_attachment
save_post
update_option_start_of_week
update_option_gmt_offset
admin_menu
before_delete_post
wp_trash_post
change_locale
request
term_link
get_post_format
get_terms
wp_get_object_terms
set_current_user
wp_default_scripts
wp_enqueue_scripts
admin_print_scripts-index.php
wp_print_scripts
print_scripts_array
customize_controls_print_styles
wp_default_styles
style_loader_src
wp_playlist_scripts
customize_controls_enqueue_scripts
nav_menu_item_id
before_signup_header
activate_header
in_admin_header
media_buttons
image_send_to_editor
media_send_to_editor
rest_pre_serve_request
embed_head
embed_content_meta
embed_footer
excerpt_more
the_excerpt_embed
oembed_dataparse
oembed_response_data
pre_oembed_result
user_has_cap
wp_version_check
load-plugins.php
load-update.php
load-update-core.php
wp_update_plugins
load-themes.php
wp_update_themes
update_option_WPLANG
wp_maybe_auto_update
edit_form_advanced
edit_page_form
wp_ajax_add-category
wp_ajax_add-post_tag
wp_ajax_add-nav_menu
wp_ajax_add-link_category
wp_ajax_add-post_format
future_post
future_page
future_attachment
future_revision
future_nav_menu_item
future_custom_css
future_customize_changeset
future_oembed_cache
future_user_request
_admin_menu
widgets_init
twentyten_credits
twentyeleven_credits
twentytwelve_credits
twentythirteen_credits
twentyfourteen_credits
twentyfifteen_credits
twentysixteen_credits
twentyseventeen_credits
locale
after_setup_theme
wp_resource_hints
wp_calculate_image_sizes
get_header_image_tag
wp_get_attachment_image_attributes
frontpage_template
widget_tag_cloud_args
header_video_settings
edit_category
body_class
customize_register
customize_preview_init
walker_nav_menu_start_el
nav_menu_item_title
comment_form_default_fields
wp_insert_comment
manage_edit-comments_columns
manage_comments_custom_column
admin_print_scripts-widgets.php
admin_footer-widgets.php
display_media_states
admin_head-widgets.php
content_filtered_save_pre
get_term_metadata
-->

因为wordpress钩子除去wordpress系统自身提供的,也可以在模板,或者插件中添加。所以,输出的钩子并不会都一应。另外wordpress的版本升级,钩子也可能会变化。

回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注