{"id":114738,"date":"2024-08-26T08:40:22","date_gmt":"2024-08-26T08:40:22","guid":{"rendered":"\/tutorials\/?p=114738"},"modified":"2026-03-09T19:17:13","modified_gmt":"2026-03-09T19:17:13","slug":"wordpress-add_action","status":"publish","type":"post","link":"\/ng\/tutorials\/wordpress-add_action\/","title":{"rendered":"What are WordPress actions and how to use WordPress add_action() function"},"content":{"rendered":"<?xml encoding=\"utf-8\" ?><p>WordPress is a highly customizable platform that allows users to extend its functionality through various hooks and functions. One of the most used functions is <strong>add_action()<\/strong>, which lets you add custom behavior to your site without modifying WordPress core files.<\/p><p>In this article, you&rsquo;ll learn how to use WordPress&rsquo;s <strong>add_action()<\/strong> function to connect your custom code with predefined actions within the platform. By mastering this function, you can easily enhance your website&rsquo;s capabilities and tailor it to your needs.<\/p><p class=\"has-text-align-center\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Mega-WordPress-Cheat-EN.pdf\">Download all-in-one WordPress cheat sheet<\/a><\/p><p>\n\n\n\n<div class=\"protip\"><div class=\"protip__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path d=\"M1.49234 23.5024C1.23229 23.5024 0.972242 23.4024 0.782206 23.2123C0.562165 22.9923 0.452144 22.6822 0.502153 22.3722C0.562165 21.9221 1.14227 17.9113 3.00262 16.351C3.63274 15.8209 4.43289 15.5509 5.26305 15.5609C6.09321 15.5909 6.87335 15.9109 7.47347 16.4911C8.6937 17.6913 8.76371 19.6717 7.6435 20.9919C6.0832 22.8523 2.08245 23.4324 1.63237 23.4924C1.59236 23.4924 1.54235 23.4924 1.50234 23.4924L1.49234 23.5024ZM5.16303 17.5613C4.84297 17.5613 4.53291 17.6713 4.29287 17.8813C3.60274 18.4614 3.07264 19.9317 2.75258 21.242C4.06282 20.9219 5.5331 20.3918 6.11321 19.7017C6.55329 19.1716 6.54329 18.3814 6.0832 17.9213C5.85316 17.7013 5.5431 17.5713 5.20304 17.5613C5.19304 17.5613 5.17303 17.5613 5.16303 17.5613ZM11.7243 21.8821C11.4942 21.8821 11.2642 21.8021 11.0841 21.652C10.8541 21.462 10.7241 21.1819 10.7241 20.8819V15.9109L8.08358 13.2705H3.11264C2.81259 13.2705 2.53254 13.1404 2.3425 12.9104C2.15246 12.6803 2.07245 12.3803 2.12246 12.0902C2.19247 11.7102 2.84259 8.36953 4.70294 7.12929C6.33325 6.04909 8.96375 6.49918 10.244 6.80923C11.5442 4.96889 13.2546 3.4286 15.2349 2.33839C17.4553 1.11816 19.9858 0.518051 22.4963 0.498047C23.0464 0.498047 23.4865 0.948132 23.4865 1.49824C23.4865 5.0389 22.3763 9.97983 17.1753 13.7605C17.4853 15.0408 17.9354 17.6613 16.8552 19.2816C15.615 21.1419 12.2744 21.7921 11.8943 21.8621C11.8343 21.8721 11.7743 21.8821 11.7143 21.8821H11.7243ZM12.7245 16.181V19.6016C13.7146 19.2916 14.7948 18.7915 15.2049 18.1814C15.675 17.4812 15.605 16.091 15.385 14.9008C14.5248 15.3808 13.6346 15.8109 12.7245 16.181ZM9.66388 12.0302L11.9643 14.3307C13.1845 13.8306 14.3648 13.2204 15.485 12.5103C19.9358 9.51974 21.2361 5.60901 21.4561 2.53843C19.6157 2.67846 17.8254 3.20856 16.2051 4.09872C14.2847 5.14892 12.6544 6.68921 11.4942 8.54956C10.7841 9.65977 10.174 10.82 9.66388 12.0302ZM4.39289 11.2701H7.81353C8.1936 10.3599 8.63368 9.46974 9.11377 8.60957C7.92355 8.38953 6.51329 8.31952 5.81315 8.78961C5.19304 9.19968 4.70294 10.3099 4.39289 11.2701Z\" fill=\"#673DE6\"\/>\n                <\/svg>\n                <p class=\"protip__title\">\n                   What is WordPress action?\n                <\/p>\n            <\/div>\n            <p class=\"protip__content\"> WordPress actions are hooks that execute custom code at specific points during the WordPress execution process. These action hooks trigger functions when certain events occur, like loading the header or publishing a post.<br>\n<\/p><\/div>\n\n\n\n<\/p><p><a href=\"\/ng\/tutorials\/what-are-wordpress-hooks\/\">WordPress hooks<\/a> are divided into two types&mdash;<strong>actions<\/strong> and <strong>filters<\/strong>. While actions let you add or modify functionality by attaching functions to specific events, filters alter data as WordPress processes it.<\/p><p>For example, the <strong>add_action()<\/strong> function adds your custom code to run when a post is published, while <strong>add_filter()<\/strong> modifies a post&rsquo;s content before displaying it.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-do-wordpress-actions-work\">How do WordPress actions work?<\/h3><p>WordPress actions work by inserting custom functions into predefined events in the WordPress lifecycle. The <strong>add_action()<\/strong> function hooks these events so that your custom code runs at the appropriate time.<\/p><p>For instance, if you want to add a custom script to your site&rsquo;s header, you can use the <strong>wp_head<\/strong> action hook. WordPress will execute your code whenever it reaches your site&rsquo;s header while the page loads by hooking your function to this event.<\/p><p><strong>Adding actions to WordPress<\/strong><\/p><p>You can add actions to WordPress in several ways, depending on where you want to place your custom code:<\/p><ul class=\"wp-block-list\">\n<li><strong>Theme&rsquo;s functions.php file<\/strong>. This is the most common place to add actions if your custom functionality is tied to the theme. Open your active theme&rsquo;s <a href=\"\/ng\/tutorials\/functions-php-wordpress\/\">functions.php file<\/a> and add your <strong>add_action()<\/strong> code.<\/li>\n\n\n\n<li><strong>Child theme&rsquo;s functions.php file<\/strong>. If you&rsquo;ve <a href=\"\/ng\/tutorials\/how-to-create-wordpress-child-theme\/\">created a child theme<\/a>, you can place your actions in the child theme&rsquo;s <strong>functions.php<\/strong> file. This method is helpful if you want to keep your changes separate from the parent theme.<\/li>\n\n\n\n<li><strong>Custom plugin<\/strong>. Consider creating a custom plugin if you want your custom actions independent of the theme. This way, your code will continue to work even if you change your site&rsquo;s theme. To do this, create a new PHP file in the <strong>wp-content\/plugins\/ <\/strong>directory, add your <strong>add_action()<\/strong> code, and activate the plugin.<\/li>\n<\/ul><h3 class=\"wp-block-heading\" id=\"h-wordpress-add-action-parameters\">WordPress add_action() parameters<\/h3><p>The WordPress <strong>add_action()<\/strong> function controls how and when your custom code is executed by taking several parameters. Let&rsquo;s break them down:<\/p><ul class=\"wp-block-list\">\n<li><strong>$hook_name<\/strong>. This parameter specifies the action hook name to which you want to attach your custom function. This hook determines when your function will run. WordPress has many predefined hooks, such as <strong>init<\/strong> to run code after WordPress has finished loading and <strong>wp_footer<\/strong> to add code to the footer.<\/li>\n\n\n\n<li><strong>$callback<\/strong>. Often referred to as the callback function, it&rsquo;s the function name you wish to execute when the specified action hook is triggered. This function contains the custom code that will run at the appropriate point in WordPress.<\/li>\n\n\n\n<li><strong>$priority (optional)<\/strong>. The <strong>$priority<\/strong> parameter determines the order in which WordPress executes functions tied to the same hook. The default priority is <strong>10<\/strong>, but you can adjust this value to control whether your function runs earlier or later than other functions in the same action. Lower numbers mean the function will run sooner.<\/li>\n\n\n\n<li><strong>$accepted_args (optional)<\/strong>. It defines the number of arguments that your callback function can accept. By default, WordPress sets this to <strong>1<\/strong>, but you can increase it if your function needs to accept more arguments.<\/li>\n<\/ul><p>Here&rsquo;s an example of a complete <strong>add_action()<\/strong> function:<\/p><pre class=\"wp-block-preformatted\">function my_custom_script() {<br><br>echo '&lt;script&gt;alert(\"Hello, World!\");&lt;\/script&gt;';<br><br>}<br><br>add_action( 'wp_footer', 'my_custom_script', 10, 1 );<\/pre><ul class=\"wp-block-list\">\n<li><strong>$hook_name &ndash; &lsquo;wp_footer&rsquo;<\/strong>. This specifies that the function should run when the <strong>wp_footer<\/strong> action hook is triggered, which happens when WordPress is about to close the body tag on every page.<\/li>\n\n\n\n<li><strong>$callback &ndash; &lsquo;my_custom_script&rsquo;<\/strong>. The function name that will be executed when the <strong>wp_footer<\/strong> hook is called.<\/li>\n\n\n\n<li><strong>$priority &ndash; 10<\/strong>. This is the default priority, meaning the function will run in the standard order relative to other functions hooked to <strong>wp_footer<\/strong>.<\/li>\n\n\n\n<li><strong>$accepted_args &ndash; 1<\/strong>. Although this function doesn&rsquo;t require arguments, WordPress sets it to accept <strong>1<\/strong> argument by default.<\/li>\n<\/ul><p>As the WordPress page loads the footer, the <strong>my_custom_script<\/strong> function executes, inserting a script that displays an alert box with the message &ldquo;Hello, World!&rdquo; before the page closes.<\/p><h2 class=\"wp-block-heading\" id=\"h-example-of-add-action-in-wordpress\">Example of add_action() in WordPress<\/h2><p>This section demonstrates some practical uses of the <strong>add_action()<\/strong> function. If you <a href=\"\/ng\/wordpress-hosting\">host your WordPress site on Hostinger<\/a> and want to try these examples, test them in a staging environment first.<\/p><p>This way, you can safely experiment without affecting your live site. You can follow our guide on <a href=\"\/ng\/tutorials\/wordpress-staging-environment\/\">setting up a WordPress staging environment<\/a> using Hostinger&rsquo;s staging tool.<\/p><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-wordpress-hosting\" href=\"\/ng\/wordpress-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"600\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=1024,h=1024,fit=scale-down\" alt=\"\" class=\"wp-image-111781\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=2048,fit=scale-down 2048w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=1536,fit=scale-down 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/a><\/figure><h3 class=\"wp-block-heading\" id=\"h-adding-a-custom-script-to-the-wordpress-frontend\">Adding a custom script to the WordPress frontend<\/h3><p>One common use of <strong>add_action()<\/strong> is inserting custom scripts into your WordPress site&rsquo;s frontend. This can be useful when you need to include JavaScript files or inline scripts that enhance your site&rsquo;s functionality or design.<\/p><p>For instance, add a custom JavaScript file to your site&rsquo;s header to create interactive elements or track user behavior. To achieve this, hook into the <strong>wp_enqueue_scripts<\/strong> action, which enqueues scripts and styles in WordPress.<\/p><p>Here&rsquo;s how to do it:<\/p><pre class=\"wp-block-preformatted\">function add_custom_script() {<br><br>wp_enqueue_script( 'custom-js', get_template_directory_uri() . '\/js\/custom-script.js', array(), null, true );<br><br>}<br><br>add_action( 'wp_enqueue_scripts', 'add_custom_script' );<\/pre><p>In this example, you call the <strong>wp_enqueue_script<\/strong> function with several parameters:<\/p><ul class=\"wp-block-list\">\n<li><strong>&lsquo;custom-js&rsquo;<\/strong> &ndash; a unique handle for your script.<\/li>\n\n\n\n<li><strong>get_template_directory_uri() . &lsquo;\/js\/custom-script.js&rsquo;<\/strong> &ndash; your JavaScript file&rsquo;s location within your theme directory.<\/li>\n\n\n\n<li><strong>array()<\/strong> &ndash; an array of dependencies for your script. You can leave this empty if there are none.<\/li>\n\n\n\n<li><strong>null (optional)<\/strong> &ndash; the script version number.<\/li>\n\n\n\n<li><strong>true<\/strong> &ndash; whether to load the script in the footer (<strong>true<\/strong>) or the header (<strong>false<\/strong>).<\/li>\n<\/ul><p>The <strong>add_action(&lsquo;wp_enqueue_scripts&rsquo;, &lsquo;add_custom_script&rsquo;)<\/strong> line hooks your <strong>add_custom_script<\/strong> function to the <strong>wp_enqueue_scripts<\/strong> action. This approach prevents conflicts with other themes or plugins that might be enqueuing their scripts.<\/p><h3 class=\"wp-block-heading\" id=\"h-adding-a-custom-footer-message\">Adding a custom footer message<\/h3><p>You can also use the <strong>add_action() <\/strong>function to customize your WordPress site&rsquo;s footer. Whether you want to display a personalized message, add a copyright notice, or include extra HTML or scripts, the <strong>wp_footer<\/strong> hook provides the ideal opportunity.<\/p><p>This hook is triggered just before the closing <strong>&lt;\/body&gt;<\/strong> tag on every page of your WordPress site. It&rsquo;s the perfect place to add custom content that you want to appear site-wide, as shown in the following example:<\/p><pre class=\"wp-block-preformatted\">function add_custom_footer_message() {<br><br>echo '&lt;p style=\"text-align: center;\"&gt;Thank you for visiting our website!&lt;\/p&gt;';<br><br>}<br><br>add_action( 'wp_footer', 'add_custom_footer_message' );<\/pre><p>Here, you add a simple HTML paragraph with a centered text message. Then, <strong>add_action()<\/strong> hooks the <strong>add_custom_footer_message<\/strong> function to the <strong>wp_footer<\/strong> action.<\/p><p>As a result, every time WordPress reaches the <strong>wp_footer<\/strong> hook, the custom message is displayed in the site&rsquo;s footer.<\/p><h3 class=\"wp-block-heading\" id=\"h-adding-a-custom-admin-notice\">Adding a custom admin notice<\/h3><p>In WordPress, admin notices effectively communicate important information to users within the admin dashboard. These notices can display warnings, updates, or other messages that need the site administrator&rsquo;s attention.<\/p><p>Hooking <strong>add_action()<\/strong> into the <strong>admin_notices<\/strong> action triggers the notice at a specific point whenever the WordPress admin area loads. The following code snippet shows how to add a custom admin notice:<\/p><pre class=\"wp-block-preformatted\">function custom_admin_notice() {<br><br>echo '&lt;div class=\"notice notice-success is-dismissible\"&gt;<br><br>&lt;p&gt;This is a custom admin notice!&lt;\/p&gt;<br><br>&lt;\/div&gt;';<br><br>}<br><br>add_action( 'admin_notices', 'custom_admin_notice' );<\/pre><p>This custom WordPress function generates the HTML for the admin notice. The <strong>&lt;div&gt;<\/strong> element includes several classes:<\/p><ul class=\"wp-block-list\">\n<li><strong>notice<\/strong> &ndash; this class is required to style the element as an admin notice.<\/li>\n\n\n\n<li><strong>notice-success<\/strong> &ndash; it defines the notice as a success message. To display different types of messages, change this to <strong>notice-warning<\/strong>, <strong>notice-error<\/strong>, or <strong>notice-info<\/strong>.<\/li>\n\n\n\n<li><strong>is-dismissible<\/strong> &ndash; this class adds a close button so users can dismiss the notice.<\/li>\n<\/ul><p>You can add logic to your custom function to display the notice only under certain conditions, such as when editing a specific post type or after updating a plugin.<\/p><p>Here&rsquo;s an example that shows an admin notice only when you edit a certain post type:<\/p><pre class=\"wp-block-preformatted\">function conditional_admin_notice() {<br><br>global $post;<br><br>if ( $post-&gt;post_type == 'your_post_type' ) {<br><br>echo '&lt;div class=\"notice notice-warning is-dismissible\"&gt;<br><br>&lt;p&gt;This is a custom notice for your post type.&lt;\/p&gt;<br><br>&lt;\/div&gt;';<br><br>}<br><br>}<br><br>add_action( 'admin_notices', 'conditional_admin_notice' );<\/pre><h3 class=\"wp-block-heading\" id=\"h-adding-a-custom-sidebar-widget\">Adding a custom sidebar widget<\/h3><p>A <a href=\"\/ng\/tutorials\/wordpress-widgets\/\">widget is<\/a> a small block that performs a specific function, such as displaying recent posts, a search bar, or custom HTML. Using the <strong>widgets_init<\/strong> action hook, you can register and display your custom widget in your sidebar or any other widget area.<\/p><p>First, define a custom class that extends <strong>WP_Widget<\/strong> to add a custom sidebar widget. This class will contain your custom widget&rsquo;s logic, including how it&rsquo;s displayed and any form fields for customizing its content.<\/p><pre class=\"wp-block-preformatted\">class My_Custom_Widget extends WP_Widget {<br><br>\/\/ Constructor to set up the widget's name, description, etc.<br><br>public function __construct() {<br><br>parent::__construct(<br><br>'my_custom_widget', \/\/ Base ID<br><br>__( 'My Custom Widget', 'text_domain' ), \/\/ Widget name in the admin<br><br>array( 'description' =&gt; __( 'A custom widget that displays a message.', 'text_domain' ), ) \/\/ Widget description<br><br>);<br><br>}<br><br>\/\/ Output the content of the widget on the frontend<br><br>public function widget( $args, $instance ) {<br><br>echo $args['before_widget'];<br><br>echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];<br><br>echo '&lt;p&gt;' . __( 'Hello, this is my custom widget!', 'text_domain' ) . '&lt;\/p&gt;';<br><br>echo $args['after_widget'];<br><br>}<br><br>\/\/ Output the widget options form on the admin<br><br>public function form( $instance ) {<br><br>$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'New title', 'text_domain' );<br><br>?&gt;<br><br>&lt;p&gt;<br><br>&lt;label for=\"&lt;?php echo esc_attr( $this-&gt;get_field_id( 'title' ) ); ?&gt;\"&gt;&lt;?php _e( 'Title:', 'text_domain' ); ?&gt;&lt;\/label&gt;<br><br>&lt;input class=\"widefat\" id=\"&lt;?php echo esc_attr( $this-&gt;get_field_id( 'title' ) ); ?&gt;\" name=\"&lt;?php echo esc_attr( $this-&gt;get_field_name( 'title' ) ); ?&gt;\" type=\"text\" value=\"&lt;?php echo esc_attr( $title ); ?&gt;\"&gt;<br><br>&lt;\/p&gt;<br><br>&lt;?php&nbsp;<br><br>}<br><br>\/\/ Process widget options to save<br><br>public function update( $new_instance, $old_instance ) {<br><br>$instance = array();<br><br>$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';<br><br>return $instance;<br><br>}<br><br>}<\/pre><p>In the above snippet, <strong>My_Custom_Widget<\/strong> extends the <strong>WP_Widget<\/strong> class and includes methods for displaying the widget (<strong>widget<\/strong>), generating the widget&rsquo;s admin form (<strong>form<\/strong>), and saving the widget&rsquo;s options (<strong>update<\/strong>).<\/p><p>After defining the custom class, register it using the <strong>widgets_init<\/strong> action hook. This is where <strong>add_action()<\/strong> comes into play:<\/p><pre class=\"wp-block-preformatted\">function register_my_custom_widget() {<br><br>register_widget( 'My_Custom_Widget' );<br><br>}<br><br>add_action( 'widgets_init', 'register_my_custom_widget' );<\/pre><p>By hooking your <strong>register_my_custom_widget<\/strong> function to <strong>widgets_init<\/strong>, WordPress will register your custom widget when it prepares the widget system. You can then add it to the sidebar or other widgetized area.<\/p><h2 class=\"wp-block-heading\" id=\"h-other-wordpress-action-functions\">Other WordPress action functions<\/h2><p>In addition to <strong>add_action()<\/strong>, several other functions in WordPress interact with action hooks. These functions enable you to execute custom code, remove actions, and check if specific actions have been registered.<\/p><p><strong>do_action()<\/strong><\/p><p>The <strong>do_action()<\/strong> function triggers an action hook. It lets you execute all the functions hooked to a particular action. You can use it within your custom code to create a new action that you or other developers can hook into, allowing for extensibility and modular code.<\/p><pre class=\"wp-block-preformatted\">do_action( 'my_custom_action' );<\/pre><p>In this example, the <strong>do_action()<\/strong> function triggers the <strong>my_custom_action<\/strong> hook.<\/p><p><strong>remove_action()<\/strong><\/p><p>In contrast, <strong>remove_action()<\/strong> unhooks a function from a specific action hook. This can be helpful when you need to override or stop a particular function from running, especially when dealing with third-party plugins or themes.<\/p><pre class=\"wp-block-preformatted\">remove_action( 'wp_head', 'wp_generator' );<\/pre><p>Here, you use <strong>remove_action()<\/strong> to unhook <strong>wp_generator<\/strong> from <strong>wp_head<\/strong>.<\/p><p><strong>has_action()<\/strong><\/p><p>Use the <strong>has_action()<\/strong> function to verify if an action hook has any functions attached to it before executing certain code, ensuring that the action you intend to modify is indeed hooked.<\/p><pre class=\"wp-block-preformatted\">if ( has_action( 'wp_footer', 'my_custom_footer' ) ) {<br><br>\/\/ The custom footer function is hooked, so do something<\/pre><p>In the above example, <strong>has_action()<\/strong> checks whether the <strong>my_custom_footer<\/strong> function is hooked to the <strong>wp_footer<\/strong> action. If it is, you can use a conditional statement to execute additional logic based on that condition.<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>This article covered how the WordPress<strong> add_action()<\/strong> function enables you to extend and customize your website. From adding custom scripts and footer messages to creating unique sidebar widgets, you can quickly tailor your site&rsquo;s functionality without altering core files.<\/p><p>We&rsquo;ve also discussed other action-related functions like <strong>do_action()<\/strong>, <strong>remove_action()<\/strong>, and <strong>has_action()<\/strong>, giving you even more control over how and when WordPress executes your custom code. Mastering these tools lets you create a more dynamic and personalized WordPress website.<\/p><h2 class=\"wp-block-heading\" id=\"h-wordpress-add-action-faq\">WordPress add_action() FAQ<\/h2><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1724661476448\"><h3 class=\"schema-faq-question\">What is the difference between add_action() and add_filter()?<\/h3> <p class=\"schema-faq-answer\"><strong>add_action()<\/strong> inserts custom functions into action hooks that execute at specific points, while <strong>add_filter()<\/strong> attaches functions to filter hooks that modify data before it is displayed. Both are important for extending WordPress functionality.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1724661482850\"><h3 class=\"schema-faq-question\">Is it safe to use add_action() in the functions.php file?<\/h3> <p class=\"schema-faq-answer\">Yes, it&rsquo;s safe to use <strong>add_action()<\/strong> in the <strong>functions.php<\/strong> file. However, consider creating a custom plugin for complex or reusable code to keep your WordPress code clean and maintainable.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1724661488701\"><h3 class=\"schema-faq-question\">What is the difference between do_action() and add_action()?<\/h3> <p class=\"schema-faq-answer\"><strong>add_action()<\/strong> registers a function to run when an action hook is triggered, while <strong>do_action()<\/strong> triggers that hook, executing all attached functions. Together, they allow for flexible and modular code execution in WordPress.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>WordPress is a highly customizable platform that allows users to extend its functionality through various hooks and functions. One of the most used functions is add_action(), which lets you add custom behavior to your site without modifying WordPress core files. In this article, you&rsquo;ll learn how to use WordPress&rsquo;s add_action() function to connect your custom [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/ng\/tutorials\/wordpress-add_action\/\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":411,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to use WordPress add_action() + practical examples","rank_math_description":"WordPress add_action attaches an action hook to a function. Read this article to learn what WordPress actions are and how to use the add_action.","rank_math_focus_keyword":"wordpress add_action","footnotes":""},"categories":[22637],"tags":[],"class_list":["post-114738","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/wordpress-add_action","default":0},{"locale":"en-GB","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/wordpress-add_action","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/wordpress-add_action","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/wordpress-add_action","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/wordpress-add_action","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/wordpress-add_action","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/wordpress-add_action","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/wordpress-add_action","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/114738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/users\/411"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/comments?post=114738"}],"version-history":[{"count":6,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/114738\/revisions"}],"predecessor-version":[{"id":143706,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/114738\/revisions\/143706"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/media?parent=114738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/categories?post=114738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/tags?post=114738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}