{"id":139648,"date":"2015-07-11T11:00:58","date_gmt":"2015-07-11T16:00:58","guid":{"rendered":"https:\/\/getflywheel.com\/?p=11535"},"modified":"2023-03-28T11:32:37","modified_gmt":"2023-03-28T16:32:37","slug":"wordpress-hooks","status":"publish","type":"resource","link":"https:\/\/wpengine.com\/case-studies\/resources\/wordpress-hooks\/","title":{"rendered":"Everything you Need to Know About WordPress Hooks"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">There\u2019s a lot going on under the hood as WordPress works to render the needed component for your website. Each page is made up of quite a few functions and database queries. The WordPress core and the theme work together to output text, images, stylesheets, and other files. With the help of the browser, all of these pieces are interpreted and put together into one web page. Hooks give the ability to customize, extend, and enhance WordPress.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hooks are named appropriately because we can literally \u201chook into\u201d WordPress to retrieve, insert, or modify data, or do other tasks behind the scenes. In a sense, we \u201chang\u201d our custom code on those hooks. As we know, modifying the WordPress core is not a good idea. Because of this, action hooks and filter hooks in WordPress are the best way to change existing or create new functionality.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Types of Hooks<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Hooks are a necessary part when making customizations. There are two primary types of hooks: action hooks and filter hooks. Each offers something different, so it is important to understand when to use which one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A hook can&#8217;t be simply thrown in just anywhere; there needs to be something to \u201chook into.\u201d Having that point of execution for that hook is necessary. The good news is that throughout the WordPress core, there are built-in hooks that are available to reference.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2015\/07\/hook.jpg\" alt=\"Four blue emergency ropes with hooks on the end\" class=\"wp-image-11538\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Why use Hooks<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A lot can be changed with hooks because many of WordPress\u2019s core functions use actions and filters. Understanding hooks is absolutely necessary for anyone developing with WordPress and for designers that want to modify WordPress or the theme\u2019s behavior.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Action Hooks<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Action hooks indicate that something has happened, not to be confused with events themselves. Actions are triggered by specific events that take place in WordPress. An example of an event could be things like publishing a post, changing themes, or activating a plugin. Actions allow you to add extra functionality at a specific point in the processing of the page. &nbsp;Action hooks can do things like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Modify data in the WordPress database<\/li>\n\n\n\n<li>Modify what is displayed in the browser<\/li>\n\n\n\n<li>Send an email when an event has happened<\/li>\n\n\n\n<li>Add a widget, menu, or a custom message to the page<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Action Hook Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;php]\n\nadd_action( $hook, $function_to_add, $priority, $accepted_args );\n\n&#091;\/php]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The required parameters of the add_action function are the hook and function to add. Including the priority is optional. With an integer value based on a scale of 1 to 999, this number determines the priority order of functions for that specific hook. You may not need it, but the last parameter is used when you need to pass or accept multiple arguments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s take a look at a basic action hook:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;php]\n\n\/\/ This is the function, name it accordingly\n\nfunction custom_welcome_text() { ?&amp;gt;\n\n&amp;lt;div class=\"optional-custom-class\"&amp;gt;Hello WordPress!&amp;lt;\/div&amp;gt;\n\n&amp;lt;?php }\n\n&amp;amp;nbsp;\n\n\/\/ Action function that outputs the function above into the theme hook\n\nadd_action( 'welcome_hook', 'custom_welcome_text', 5 );\n\n&#091;\/php]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This basic hook will add \u201cHello WordPress\u201d to the top of the page. It has a priority of 5. Lower numbers correspond with earlier execution. By default it is set to 10, so this example executes before the default.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Filter Hooks<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s go over a common scenario and how filter hooks come into play. Think of all the times that your users visit your site and a post loads for them to read. As part of the WordPress page lifecycle, WordPress queries the database for that post, then it is returned to the browser, ready to for the user to read. It all sounds simple, but before the content is served, WordPress runs the data through any filters that have been established. These filter hooks can easily be created when needed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of it this way: actions are triggered by specific events, but filters allow for the interception and modification of data as it is processed. Filter hooks are used when you need to intercept, manage, or return data before rendering it to the browser or saving data from the browser to the database. For example, you might want to insert another CSS class in a WordPress HTML element, or register additional meta field types. Both of these can be executed with a filter hook.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/getflywheel-images.s3.us-east-2.amazonaws.com\/uploads\/2015\/07\/wordpress-hooks-filter.jpg\" alt=\"a stainless steel filter\" class=\"wp-image-11540\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Filters sit between the database and the browser as WordPress generates pages. Also, they sit between the browser and database as WordPress adds new posts and comments to the database. Because of this, filter hooks can do helpful things like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add data to the database or send to the browser screen<\/li>\n\n\n\n<li>Manipulate data coming out of the database before it goes into the browser&nbsp;or coming from the browser prior to going into the database.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Filter Hook Example<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In the example below, we\u2019ll look at how a filter hook works on content. This hook is fired just before content will be used, put in the database, or rendered by WordPress for onscreen viewing. This alters the data when the filter hook is fired.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>add_filter<\/code> function is what we need to hook into a filter. The arguments may look familiar to you, the <code>add_filter<\/code> is the same as that of <code>add_action<\/code>. Let\u2019s now hook into the WordPress filter the_content, which is called before any post content is displayed. In this simple example, the filter will allow us to add a copyright note to every post. At this point, the filters will take action on data that&#8217;s being passed to them.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;php]\n\nadd_filter( 'the_content', 'add_copyright_notice' );\n\nfunction add_copyright_notice( $content )\n\n{\n\nreturn $content . \" &amp;lt;br&amp;gt;This content is copyrighted.\";\n\n}\n\n&#091;\/php]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Please note, when you use a filter, you must always return something or things will break.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Adding Hooks to a Theme<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When adding hooks and filters to the theme, be sure that you are working in a child theme. The main reason is that if you are working in the parent theme, these modifications could be overwritten if there is an update to the theme files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hopefully you have a better understanding of hooks, what they can do, and how they can help. Hooks can be very simple, very complex, or somewhere in between. Once you have the basic concepts, you will be hooked in no time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There\u2019s a lot going on under the hood as WordPress works to render the needed component for your website. Each page is made up of quite a few functions and database queries. The WordPress core and the theme work together to output text, images, stylesheets, and other files. With the help of the browser, all<span class=\"tile__ellipses\">&hellip;<\/span><span class=\"tile__ellipses--animated\"><\/span><\/p>\n","protected":false},"author":1,"featured_media":141945,"template":"","resource-topic":[901],"resource-role":[1397,896,897],"resource-type":[916],"class_list":["post-139648","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>Everything you need to know about WordPress hooks<\/title>\n<meta name=\"description\" content=\"WordPress hooks are the best way to create new functionality for your site. Here&#039;s everything you need to know about action and filter hooks.\" \/>\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=\"Everything you need to know about WordPress hooks\" \/>\n<meta property=\"og:description\" content=\"WordPress hooks are the best way to create new functionality for your site. Here&#039;s everything you need to know about action and filter hooks.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpengine.com\/case-studies\/resources\/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=\"2023-03-28T16:32:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2015\/07\/hooks-header.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"627\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\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\/wordpress-hooks\/\",\"url\":\"https:\/\/wpengine.com\/case-studies\/resources\/wordpress-hooks\/\",\"name\":\"Everything you need to know about WordPress hooks\",\"isPartOf\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\"},\"datePublished\":\"2015-07-11T16:00:58+00:00\",\"dateModified\":\"2023-03-28T16:32:37+00:00\",\"description\":\"WordPress hooks are the best way to create new functionality for your site. Here's everything you need to know about action and filter hooks.\",\"breadcrumb\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/wordpress-hooks\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/wpengine.com\/case-studies\/resources\/wordpress-hooks\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/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\":\"Everything you Need to Know About 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\/f5301455463371a10d1fc290e9ad0085\",\"name\":\"WP Engine\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"caption\":\"WP Engine\"},\"sameAs\":[\"https:\/\/wpengine.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Everything you need to know about WordPress hooks","description":"WordPress hooks are the best way to create new functionality for your site. Here's everything you need to know about action and filter hooks.","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"Everything you need to know about WordPress hooks","og_description":"WordPress hooks are the best way to create new functionality for your site. Here's everything you need to know about action and filter hooks.","og_url":"https:\/\/wpengine.com\/case-studies\/resources\/wordpress-hooks\/","og_site_name":"WP Engine","article_publisher":"https:\/\/www.facebook.com\/wpengine","article_modified_time":"2023-03-28T16:32:37+00:00","og_image":[{"width":1200,"height":627,"url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2015\/07\/hooks-header.png","type":"image\/png"}],"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\/wordpress-hooks\/","url":"https:\/\/wpengine.com\/case-studies\/resources\/wordpress-hooks\/","name":"Everything you need to know about WordPress hooks","isPartOf":{"@id":"https:\/\/wpengine.com\/case-studies\/#website"},"datePublished":"2015-07-11T16:00:58+00:00","dateModified":"2023-03-28T16:32:37+00:00","description":"WordPress hooks are the best way to create new functionality for your site. Here's everything you need to know about action and filter hooks.","breadcrumb":{"@id":"https:\/\/wpengine.com\/case-studies\/resources\/wordpress-hooks\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpengine.com\/case-studies\/resources\/wordpress-hooks\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpengine.com\/case-studies\/resources\/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":"Everything you Need to Know About 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\/f5301455463371a10d1fc290e9ad0085","name":"WP Engine","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","caption":"WP Engine"},"sameAs":["https:\/\/wpengine.com"]}]}},"acf":[],"grid_image_url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2015\/07\/hooks-grid.png","media-type":{"term_id":916,"name":"Article","slug":"article"},"role":"<strong>Roles:<\/strong> Designer, Developer, Freelancer","topic":"<strong>Topics:<\/strong> WordPress","_links":{"self":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource\/139648","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\/1"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media\/141945"}],"wp:attachment":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media?parent=139648"}],"wp:term":[{"taxonomy":"resource-topic","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-topic?post=139648"},{"taxonomy":"resource-role","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-role?post=139648"},{"taxonomy":"resource-type","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-type?post=139648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}