{"id":109565,"date":"2020-08-18T17:20:29","date_gmt":"2020-08-18T22:20:29","guid":{"rendered":"https:\/\/wpengine.com\/?post_type=resource&#038;p=109565"},"modified":"2024-06-25T19:10:37","modified_gmt":"2024-06-26T00:10:37","slug":"what-are-wordpress-hooks","status":"publish","type":"resource","link":"https:\/\/wpengine.com\/case-studies\/resources\/what-are-wordpress-hooks\/","title":{"rendered":"What Are WordPress Hooks?"},"content":{"rendered":"\n<p>WordPress is known for its power and customizability, and themes make that power easier to access for most users. However, themes are limited by the functionality developers build into them. Finding one that does exactly what you need isn\u2019t always easy.<\/p>\n\n\n\n<p>That\u2019s where hooks come in. <a href=\"https:\/\/developer.wordpress.org\/plugins\/hooks\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress hooks<\/a> are a powerful way to quickly add almost any function to your site. They can either perform an action (<em>actions<\/em>) or change something (<em>filters<\/em>) on your site. The best part is their flexibility. If there\u2019s something you want to do, there\u2019s probably a hook that can help.<\/p>\n\n\n\n<p>In this article, we\u2019ll cover the basics of hooks, actions, and filters, and then walk you through some examples. Let\u2019s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Terms Related to Hooks<\/h2>\n\n\n\n<p>It\u2019s helpful to have a little background before we dive into how to create your own hooks. There are a few common terms that you\u2019ll frequently run across when dealing with hooks: actions, filters, <code>wp_head<\/code>, and <code>the_content<\/code>.<\/p>\n\n\n\n<p>Actions and filters are the two basic functions performed by hooks, while <code>wp_head<\/code> and <code>the_content<\/code> are two of the most commonly-used hooks for actions and filters, respectively.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Action<\/h3>\n\n\n\n<p>An action is a function performed when a given event occurs in WordPress. In other words, actions <em>do<\/em> something. Some common examples of actions include:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Sending an email or other notification to users when a new post is published<\/li><li>Loading a widget in the footer of a page<\/li><li>Displaying an instruction box above a login form<\/li><\/ul>\n\n\n\n<p>Actions are already in use on nearly every WordPress site, although they\u2019re typically hidden behind the Graphical User Interface (GUI) of themes and plugins. You\u2019ll only really see them if you intentionally dig into the source code to look for them \u2013 or if you <a href=\"https:\/\/wpengine.com\/resources\/wordpress-header-and-footer-code\/\" target=\"_blank\" rel=\"noreferrer noopener\">write your own code<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Filter<\/h3>\n\n\n\n<p>If an action is a <em>do<\/em> hook, a filter is a <em>change <\/em>hook. This type of hook enables you to modify another function. Common examples of filters in action include:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Capitalizing the words in a post\u2019s title automatically<\/li><li>Attaching links to related posts at the end of a piece of content<\/li><li>Pulling up posts from a specific tag or category<\/li><\/ul>\n\n\n\n<p>Like actions, you\u2019re probably using filters all over your website already, without even knowing it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">wp_head<\/h3>\n\n\n\n<p>The <code>wp_head<\/code> action hook triggers an action in the header of your page \u2013 inside the <code>&lt;head&gt;&lt;\/head&gt;<\/code> section of the theme\u2019s code. This is important because numerous critical functions are located within the header in WordPress. Some examples include <a href=\"https:\/\/wpengine.com\/resources\/wordpress-google-analytics-tag-manager\/\" target=\"_blank\" rel=\"noreferrer noopener\">Google Analytics tracking codes<\/a> and the noindex action, which tells search engines to not include a page in search results.\u00a0<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">the_content<\/h3>\n\n\n\n<p>Next up, <code>the_content<\/code> is a filter that applies to the content of a post. It\u2019s used to apply basic formatting \u2013 a common example is adding paragraph tags and line breaks into a post\u2019s code automatically. Another frequent use of <code>the_content<\/code> filters is displaying social sharing buttons at the bottom of a post.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Do I Create a Hook in WordPress?<\/h2>\n\n\n\n<p>Now that you understand the basics of hooks, it\u2019s time to create one of your own. To do that, you need to know that hooks are made up of three basic parts:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The hook itself, which tells WordPress <em>where <\/em>to perform the function<\/li><li>An action or filter, which tells WordPress <em>what <\/em>function you want to perform<\/li><li>A callback to a function in the WordPress library, which tells WordPress <em>how <\/em>to carry out the action or filter<\/li><\/ul>\n\n\n\n<p>For the most part, the hook, action, and filter will already be defined within either the WordPress core files or your theme\u2019s files. This means you\u2019ll just need to connect the two, and depending on the function you want to execute, add some parameters.<\/p>\n\n\n\n<p>For the latter, you can consult the relevant page on the <a href=\"https:\/\/codex.wordpress.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress Codex<\/a>. Let\u2019s see what all of this looks like in action with a couple of examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of an Action Hook<\/h3>\n\n\n\n<p>To illustrate an action hook, we\u2019ll use <code>wp_head<\/code> and the <code>noindex<\/code> function. This is a fairly common function that tells search engine crawlers to skip <a href=\"https:\/\/wpengine.com\/support\/read-use-robots-txt\/\" target=\"_blank\" rel=\"noreferrer noopener\">indexing a page<\/a>, and it\u2019s typically run from the page\u2019s header section.\u00a0<\/p>\n\n\n\n<p>The <code>wp_head<\/code> hook is found in your theme\u2019s <em>header.php<\/em> file:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/JYsXnx_B7snLns2uWx89VmMWPHdgp79kNdxzOcWeRefp9cvayI_zC1Zul0RylcYFANle9m7G7Jehe9as6V-wUhDe6JT9Su3oD6NnHe2XWdQ7Bu1jXdlaKpFF_bL02SmOwwIExkOs\" alt=\"Image of where to find the wp_head hook in your WordPress theme's header\" \/><\/figure>\n\n\n\n<p>The action, noindex, is found in <em>wp-includes\/general-template.php. <\/em>This file is part of WordPress core, and contains <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/noindex\/\" target=\"_blank\" rel=\"noreferrer noopener\">the following code<\/a>:<\/p>\n\n\n\n<p><code>function noindex() {<br>    \/\/ If the blog is not public, tell robots to go away.<br>    if ( '0' == get_option( 'blog_public' ) ) {<br>       wp_no_robots()<br>\u00a0\u00a0\u00a0}<br>}<\/code><\/p>\n\n\n\n<p>You\u2019ll need to connect these two with a callback in <a href=\"https:\/\/codex.wordpress.org\/Functions_File_Explained\" target=\"_blank\" rel=\"noreferrer noopener\">the <em>functions.php <\/em>file<\/a> of your theme. It\u2019s best to add this to <a href=\"https:\/\/wpengine.com\/resources\/create-child-theme-wordpress\/\" target=\"_blank\" rel=\"noreferrer noopener\">a child theme<\/a> rather than to your main theme, because it may otherwise get overwritten when the theme updates.<\/p>\n\n\n\n<p>Open up your <em>functions.php<\/em> file in your WordPress dashboard (<em>Appearance &gt; Theme Editor<\/em>), and add the following code to the bottom of the file:<\/p>\n\n\n\n<p><code>add_action( 'wp_head', 'noindex' );<\/code><\/p>\n\n\n\n<p>Now, when the header loads it will perform the noindex function, which \u2018tells robots to go away\u2019.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of a Filter Hook<\/h3>\n\n\n\n<p>For our filter example, we\u2019ll use <code>the_content<\/code> and <code>wpautop<\/code>. This function automatically adds HTML tags for your paragraphs and includes line breaks between them, so they\u2019re properly displayed in the browser.<\/p>\n\n\n\n<p>The hook, <code>the_content<\/code>, is found in the <em>wp-includes\/post-template.php <\/em>file. This is the relevant code:<\/p>\n\n\n\n<p><code>apply_filters( 'the_content', string $content )<\/code><\/p>\n\n\n\n<p>The <code>wpautop<\/code> function is found in the <em>wp-includes\/formatting.php<\/em> file. Here\u2019s what it looks like:<\/p>\n\n\n\n<p><code>function wpautop( $pee, $br = true ) {<br>\u00a0\/\/ \u2026<br>return $pee;<br>}<\/code><\/p>\n\n\n\n<p>Here\u2019s the code you need to add to <em>functions.php<\/em> in order to connect these two:<\/p>\n\n\n\n<p><code>add_filter( 'the_content', 'wpautop' );<\/code><\/p>\n\n\n\n<p>Once you\u2019ve added the code and saved the file, your new hook should start doing its job right away.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Where Can I Find a List of All WordPress Hooks?<\/h2>\n\n\n\n<p>There are an incredible number of hooks built into WordPress. Memorizing all of them is a challenge, even for seasoned developers. Fortunately, there are some excellent references online that list all of the available hooks. The <a href=\"https:\/\/codex.wordpress.org\/Plugin_API\/Action_Reference\" target=\"_blank\" rel=\"noreferrer noopener\">Action Reference<\/a> and <a href=\"https:\/\/codex.wordpress.org\/Plugin_API\/Filter_Reference\" target=\"_blank\" rel=\"noreferrer noopener\">Filter Reference<\/a> on the <a href=\"https:\/\/codex.wordpress.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress Codex<\/a> are perfect starting points.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Explore WP Engine\u2019s Developer Resources<\/h2>\n\n\n\n<p>Hooks in WordPress enable you to quickly customize your site. Plus, you don\u2019t have to be a pro web developer to use them. Even a coding newbie can learn to implement hooks in WordPress with a little practice.&nbsp;<\/p>\n\n\n\n<p>No matter what type of WordPress user you are, WP Engine offers in-depth <a href=\"https:\/\/wpengine.com\/resources\/\" target=\"_blank\" rel=\"noreferrer noopener\">developer resources<\/a> to help you provide an incredible digital experience for your users. Be sure to check them out!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WordPress is known for its power and customizability, and themes make that power easier to access for most users. However, themes are limited by the functionality developers build into them. Finding one that does exactly what you need isn\u2019t always easy. That\u2019s where hooks come in. WordPress hooks are a powerful way to quickly add<span class=\"tile__ellipses\">&hellip;<\/span><span class=\"tile__ellipses--animated\"><\/span><\/p>\n","protected":false},"author":297,"featured_media":109566,"template":"","resource-topic":[901],"resource-role":[895,896,1296],"resource-type":[916],"class_list":["post-109565","resource","type-resource","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What Are WordPress Hooks? | WP Engine<\/title>\n<meta name=\"description\" content=\"Learn how WordPress hooks work and how you can use them to customize, extend, and optimize your WordPress site&#039;s functionality.\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What Are WordPress Hooks? | WP Engine\" \/>\n<meta property=\"og:description\" content=\"Learn how WordPress hooks work and how you can use them to customize, extend, and optimize your WordPress site&#039;s functionality.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpengine.com\/case-studies\/resources\/what-are-wordpress-hooks\/\" \/>\n<meta property=\"og:site_name\" content=\"WP Engine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/wpengine\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-26T00:10:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2020\/08\/shutterstock_1006716628.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@wpengine\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/what-are-wordpress-hooks\/\",\"url\":\"https:\/\/wpengine.com\/case-studies\/resources\/what-are-wordpress-hooks\/\",\"name\":\"What Are WordPress Hooks? | WP Engine\",\"isPartOf\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\"},\"datePublished\":\"2020-08-18T22:20:29+00:00\",\"dateModified\":\"2024-06-26T00:10:37+00:00\",\"description\":\"Learn how WordPress hooks work and how you can use them to customize, extend, and optimize your WordPress site's functionality.\",\"breadcrumb\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/what-are-wordpress-hooks\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/wpengine.com\/case-studies\/resources\/what-are-wordpress-hooks\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/what-are-wordpress-hooks\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/wpengine.com\/case-studies\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Resources\",\"item\":\"https:\/\/wpengine.com\/case-studies\/resources\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"What Are WordPress Hooks?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\",\"url\":\"https:\/\/wpengine.com\/case-studies\/\",\"name\":\"WP Engine\",\"description\":\"Managed Hosting for WordPress\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/3a22232b01de39dcf588fb8e421c0521\",\"name\":\"Erin Myers\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g\",\"caption\":\"Erin Myers\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What Are WordPress Hooks? | WP Engine","description":"Learn how WordPress hooks work and how you can use them to customize, extend, and optimize your WordPress site's functionality.","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"What Are WordPress Hooks? | WP Engine","og_description":"Learn how WordPress hooks work and how you can use them to customize, extend, and optimize your WordPress site's functionality.","og_url":"https:\/\/wpengine.com\/case-studies\/resources\/what-are-wordpress-hooks\/","og_site_name":"WP Engine","article_publisher":"https:\/\/www.facebook.com\/wpengine","article_modified_time":"2024-06-26T00:10:37+00:00","og_image":[{"width":1100,"height":500,"url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2020\/08\/shutterstock_1006716628.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_site":"@wpengine","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/wpengine.com\/case-studies\/resources\/what-are-wordpress-hooks\/","url":"https:\/\/wpengine.com\/case-studies\/resources\/what-are-wordpress-hooks\/","name":"What Are WordPress Hooks? | WP Engine","isPartOf":{"@id":"https:\/\/wpengine.com\/case-studies\/#website"},"datePublished":"2020-08-18T22:20:29+00:00","dateModified":"2024-06-26T00:10:37+00:00","description":"Learn how WordPress hooks work and how you can use them to customize, extend, and optimize your WordPress site's functionality.","breadcrumb":{"@id":"https:\/\/wpengine.com\/case-studies\/resources\/what-are-wordpress-hooks\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpengine.com\/case-studies\/resources\/what-are-wordpress-hooks\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpengine.com\/case-studies\/resources\/what-are-wordpress-hooks\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpengine.com\/case-studies\/"},{"@type":"ListItem","position":2,"name":"Resources","item":"https:\/\/wpengine.com\/case-studies\/resources\/"},{"@type":"ListItem","position":3,"name":"What Are WordPress Hooks?"}]},{"@type":"WebSite","@id":"https:\/\/wpengine.com\/case-studies\/#website","url":"https:\/\/wpengine.com\/case-studies\/","name":"WP Engine","description":"Managed Hosting for WordPress","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/3a22232b01de39dcf588fb8e421c0521","name":"Erin Myers","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g","caption":"Erin Myers"}}]}},"acf":[],"grid_image_url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2020\/08\/shutterstock_1006716628-1.jpg","media-type":{"term_id":916,"name":"Article","slug":"article"},"role":"<strong>Roles:<\/strong> Agency, Developer, Publisher","topic":"<strong>Topics:<\/strong> WordPress","_links":{"self":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource\/109565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource"}],"about":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/types\/resource"}],"author":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/users\/297"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media\/109566"}],"wp:attachment":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media?parent=109565"}],"wp:term":[{"taxonomy":"resource-topic","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-topic?post=109565"},{"taxonomy":"resource-role","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-role?post=109565"},{"taxonomy":"resource-type","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-type?post=109565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}