{"id":140056,"date":"2024-03-08T13:23:35","date_gmt":"2024-03-08T13:23:35","guid":{"rendered":"https:\/\/learn.wordpress.org\/?post_type=lesson&#038;p=140056"},"modified":"2025-02-20T04:10:35","modified_gmt":"2025-02-20T04:10:35","slug":"action-hooks","status":"publish","type":"lesson","link":"https:\/\/learn.wordpress.org\/lesson\/action-hooks\/","title":{"rendered":"Action Hooks"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Action Hooks\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/D3e4ZxaQN5g?feature=oembed&#038;enablejsapi=1&#038;origin=https:\/\/learn.wordpress.org\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">There are two types of WordPress hooks, action hooks, and filter hooks. These are more commonly known as actions and filters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this video we\u2019ll focus on actions, but check out the filters lesson for more information on filter hooks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are action hooks<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As their name states, actions allow you to perform some action at a specific point during the execution of a WordPress request.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To better explain this, let\u2019s look at an example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When developing a WordPress theme, it&#8217;s possible to enable support for different post formats.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can read more about <a href=\"https:\/\/developer.wordpress.org\/advanced-administration\/wordpress\/post-formats\/\">post formats<\/a> in the advanced administration section of the WordPress developer documentation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Post formats are a way to allow users with access to create posts on a WordPress site to choose from a predefined list of formats. Depending on the chosen post format, a different template layout can be rendered, displaying the post in a different format.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To enable post formats, the documentation indicates you need to use the <code>add_theme_support<\/code> function, and recommends that this be registered via the <code>after_setup_theme<\/code> action hook.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This hook is defined in the <code>wp-settings.php<\/code> file, after the theme is loaded.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">do_action( 'after_setup_theme' );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here the do_action function defines the action hook, with the hook name <code>after_setup_theme<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can also read more about this hook, <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/after_setup_theme\/\">in the reference page for this hook<\/a> in the developer reference.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here we see that this hook is fired during each page load, after the theme is initialized, and is used to perform basic setup, registration, and initialization actions for a theme.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using action hooks<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In order to make use of an action, you register a function in your code to a pre-existing action hook, which is known as a callback function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To register your callback function on an action you use the WordPress <code>add_action<\/code> <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/add_action\/\">function<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You will need to pass the hook name and the name of your callback function as arguments to the add_action function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s take a look at what this looks like in a theme\u2019s <code>functions.php<\/code> file.<\/p>\n\n\n\n<div class=\"wp-block-wporg-notice is-alert-notice\"><div class=\"wp-block-wporg-notice__icon\"><\/div><div class=\"wp-block-wporg-notice__content\"><p>For this lesson, you&#8217;re going to write some code inside your currently active theme. Please note that this is for demonstrative purposes only. Unless you are developing the theme yourself, it is not recommended to edit an existing theme&#8217;s files directly.<\/p><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In your code editor, navigate to your currently active theme\u2019s <code>functions.php<\/code> file, and open it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your theme doesn\u2019t have a <code>functions.php<\/code> file, you can create one in the root of your theme directory. Just make sure it&#8217;s named functions.php, and has the opening PHP tag at the top of the file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then, add the following code to your <code>functions.php<\/code> file to hook a callback function into the <code>after_setup_theme<\/code> action hook.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">add_action( 'after_setup_theme', 'wp_learn_setup_theme');<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next you need to define the callback function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To do this, use the PHP function syntax, with the name of the function you want to define.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">function wp_learn_setup_theme() {\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then add the <code>add_theme_support<\/code> function call inside the callback function. For this example, you can copy the code from the Post formats documentation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">function wp_learn_setup_theme() {\n    add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With this code in your active theme, if you create a new Post now in your WordPress dashboard, you\u2019ll see the Post Formats select box appear in the post editor, and you can select the required Post Format.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here you can see the two post formats you enabled in your callback function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As you learned from this example you can use actions to perform something, either enabling some already existing feature, or adding something to the request execution.<\/p>\n\n\n<div class=\"sensei-block-wrapper\">\n<div class=\"wp-block-sensei-lms-lesson-actions\"><div class=\"sensei-buttons-container\">\n\n\n\n\n\n<\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Explain the concept of action hooks, how they work, and how to use them.<\/p>\n","protected":false},"featured_media":257086,"template":"","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_initial_content":"","_new_post":false,"_ef_editorial_meta_checkbox_code-shippet":"","_ef_editorial_meta_checkbox_gh-issue":"","_ef_editorial_meta_checkbox_tut-pass":"","_ef_editorial_meta_checkbox_callout-styling":"","_ef_editorial_meta_checkbox_brand":"","_ef_editorial_meta_checkbox_has-slides-in-browser":"","_ef_editorial_meta_checkbox_has-downloadable-slides":"","_ef_editorial_meta_checkbox_no-slides":"","_ef_editorial_meta_checkbox_included":"","_ef_editorial_meta_checkbox_image-alt-tags":"","expiration_date":"","language":"en_US","_duration":0,"presenter_wporg_username":[""],"other_contributor_wporg_username":[""],"video_url":"","_lesson_featured":"","_quiz_has_questions":false,"_lesson_complexity":"easy","_lesson_length":10,"_lesson_course":56187,"_lesson_preview":"preview"},"lesson-tag":[],"audience":[],"level":[6],"show":[],"wporg_wp_version":[],"wporg_included_content":[],"topic":[],"class_list":["post-140056","lesson","type-lesson","status-publish","has-post-thumbnail","hentry","module-wordpress-hooks","level-beginner","post"],"is_coteacher":false,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/learn.wordpress.org\/wp-json\/wp\/v2\/lessons\/140056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn.wordpress.org\/wp-json\/wp\/v2\/lessons"}],"about":[{"href":"https:\/\/learn.wordpress.org\/wp-json\/wp\/v2\/types\/lesson"}],"version-history":[{"count":8,"href":"https:\/\/learn.wordpress.org\/wp-json\/wp\/v2\/lessons\/140056\/revisions"}],"predecessor-version":[{"id":288181,"href":"https:\/\/learn.wordpress.org\/wp-json\/wp\/v2\/lessons\/140056\/revisions\/288181"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learn.wordpress.org\/wp-json\/wp\/v2\/media\/257086"}],"wp:attachment":[{"href":"https:\/\/learn.wordpress.org\/wp-json\/wp\/v2\/media?parent=140056"}],"wp:term":[{"taxonomy":"lesson-tag","embeddable":true,"href":"https:\/\/learn.wordpress.org\/wp-json\/wp\/v2\/lesson-tag?post=140056"},{"taxonomy":"audience","embeddable":true,"href":"https:\/\/learn.wordpress.org\/wp-json\/wp\/v2\/audience?post=140056"},{"taxonomy":"level","embeddable":true,"href":"https:\/\/learn.wordpress.org\/wp-json\/wp\/v2\/level?post=140056"},{"taxonomy":"show","embeddable":true,"href":"https:\/\/learn.wordpress.org\/wp-json\/wp\/v2\/show?post=140056"},{"taxonomy":"wporg_wp_version","embeddable":true,"href":"https:\/\/learn.wordpress.org\/wp-json\/wp\/v2\/wporg_wp_version?post=140056"},{"taxonomy":"wporg_included_content","embeddable":true,"href":"https:\/\/learn.wordpress.org\/wp-json\/wp\/v2\/wporg_included_content?post=140056"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/learn.wordpress.org\/wp-json\/wp\/v2\/topic?post=140056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}