All dynamic actions in WordPress

WordPress has an infinite amount of actions and filters. Indeed it comes with dynamic actions that listen on actions for your custom post type, option, plugin or other. This allows you to keep the performance and make the code easy to maintain. In this month I’ll show you some neat little tricks for them. This post starts with an overview over all possible actions and filter.Read the more up to date and famous one afterwards!
Everything cool and wrong about upcoming WordPress 4.7
Open in new tab for later

How to use dynamic actions (or filter)

Just for research I like to enlist all dynamic actions. You can use them to react on only one specific (your) plugin, option, taxonomy, post or other. Like this action that will come up, when a post of the type “example” will be saved:

function my_example_saved( $post_id ) {
  // some fancy stuff that needs to be done
}

add_action( 'save_post_example', 'my_example_saved' );

This is something you want to do. Because instead of listening to every post being saved via the general “save_post” action you only listen to the post-type you want. That brings a bit of performance and makes the code simpler to maintain.

Readable and more performance

WordPress allow you to hook in on every post_save or every update_option. But is this necessary? Take this code for example:

add_action( 'save_post', function ( $post_id ) {
  if ( 'my_own' != get_post_type( $post_id ) ) {
    return;
  }

  // fancy stuff for your own post type
} );

You listen to every post save, take a look at it and when it is not yours then it will be dropped. So your hook will be nagged with irrelevant calls. When everyone does that it can take a lot of time to save just one simple post. It is better to only get triggered when the information is for you:

add_action( 'save_post_my_own', function ( $post_id ) {
  // fancy stuff for your own post type
} );

This is very readable – more than the other example above. And it will only be fired when the “my_own” content will be saved in the backend. So WordPress won’t run to your hook for every post-type but instead only for the one you are interested in. This is why dynamic actions are so important.

What when there is no filter or action?

When you don’t find something here or want to manipulate an entity of WordPress that has no filter or action, then you still can do it via the object-cache drop-in.

Read this article to learn more about it: Faster bootstrap with object cache in memory

List of every dynamic action in WordPress

Ajax

Everyone might know these two actions:

wp_ajax_nopriv_{$_REQUEST[action]}
wp_ajax_{$_REQUEST[action]}

They are called based on the AJAX-Request you do via `wp.ajax`.

Admin

In the backend are some generic actions too depending on the screen that you currently use:

admin_action_{$_REQUEST[action]}
admin_footer-{$hook_suffix}
admin_head_{$content_func}
admin_post_{$action}
admin_post_nopriv_{$action}
network_admin_edit_{$_GET[action]}
admin_head-{$hook_suffix}
admin_print_footer_scripts-{$hook_suffix}
admin_print_scripts-{$hook_suffix}
admin_print_styles-{$hook_suffix}

Those can help you inject features to only a part of the backend.

Media

For those who want to manipulate the files while uploading there are two filters:

media_upload_{$tab}
media_upload_{$type}

It is a works like the “media_upload_tabs” action and uses the $_GET parameter.

Meta-Type

When you like to handle meta data then those are your actions:

added_{$meta_type}_meta
add_meta_boxes_{$post_type}
add_{$meta_type}_meta
deleted_{$meta_type}meta
delete_{$meta_type}meta
deleted_{$meta_type}_meta
delete_{$meta_type}_meta
updated_{$meta_type}_meta
update_{$meta_type}_meta

This can be handy when you have an own special “Did you mean …?” search index and want meta fields in there.

Options

Options have very detailed filter and actions in their functions:

add_option_{$option}
add_site_option_{$option}
delete_option_{$option}
delete_site_option_{$option}
pre_delete_site_option_{$option}
update_option_{$option}
update_site_option_{$option}

It’s worth to have a look how the “get_option” function uses filter for early exit
or manipulating data.

Plugins

All the hooks that listen to specific plugins or are mare to manipulate plugins in WordPress:

uninstall_{$file}
activate_{$plugin}
deactivate_{$plugin}
activate_{$plugin}
after_plugin_row_{$plugin_file}
in_plugin_update_message-{$file}
install_plugins_pre_{$tab}
install_plugins_{$tab}
load-{$plugin_page}

But please don’t abuse them for advertising.

Posts

This is the best part but also a very weak one. Unfortunately posts do not have as many actions as you might wish for. Only these are given:

manage_{$post->post_type}_posts_custom_column
{$new_status}_{$post->post_type}
{$old_status}_to_{$new_status}
post_action_{$action}
save_post_{$post->post_type}
comment_{$new_status}_{$comment->comment_type}
comment_{$old_status}_to_{$new_status}

But there are filters too which can be handy for the page of your customer.

Themes

Plenty of general things for installing, updating and customizing themes:

after_theme_row_{$stylesheet}
install_themes_pre_{$tab}
install_themes_{$tab}
in_theme_update_message-{$theme_key}
get_template_part_{$slug}

customize_post_value_set_{$setting_id}
customize_preview_{$this->id}
customize_preview_{$this->type}
customize_render_control_{$this->id}
customize_render_panel_{$this->id}
customize_render_section_{$this->id}
customize_save_{$this->id_data[base]}
customize_update_{$this->type}

Very nice if you like to recompile SCSS files or other fancy things before reloading the preview.

Taxonomies

This might be mixed up with the backend but here are all “dynamic” actions for taxonomies:

after-{$taxonomy}-table
created_{$taxonomy}
create_{$taxonomy}
delete_{$taxonomy}
edited_{$taxonomy}
edit_{$taxonomy}
{$taxonomy}_add_form
{$taxonomy}_add_form_fields
{$taxonomy}_edit_form
{$taxonomy}_edit_form_fields
{$taxonomy}_pre_add_form
{$taxonomy}_pre_edit_form
{$taxonomy}_term_edit_form_tag
{$taxonomy}_term_edit_form_top
{$taxonomy}_term_new_form_tag

Transients

Transients as well as options are detailed enough to manipulate almost everything:

delete_site_transient_{$transient}
delete_transient_{$transient}
set_site_transient_{$transient}
set_transient_{$transient}

To have more power read the post about the options page.

Others

And some other actions that didn’t really fit in one of the above:

do_feed_{$feed}
load-importer-{$importer}
load-{$page_hook}
load-{$pagenow}
login_form_{$action}
{$page_hook}
update-core-custom_{$action}
update-custom_{$action}

Conclusion

I need to write more articles about those filters and I will. A month might be a bit to less because every filter can have plenty of magic in it or a lot of use cases. Follow the blog to stay tuned and see what nerdy stuff can be done here. I hope to keep track of them as this list only is valid for WordPress 4.6 .

2 thoughts on “All dynamic actions in WordPress”

  1. I have been looking for dynamic actions for WP for quite a while, your list is really helpful, now I can finally hook in the right places instead of some general actions or filters , thx alot 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *