{"id":301,"date":"2025-09-30T06:06:25","date_gmt":"2025-09-30T06:06:25","guid":{"rendered":"https:\/\/mitalgoswami.in\/?p=301"},"modified":"2025-09-30T06:06:25","modified_gmt":"2025-09-30T06:06:25","slug":"differnce-between-add_filter-and-add_action","status":"publish","type":"post","link":"https:\/\/mitalgoswami.in\/?p=301","title":{"rendered":"differnce between add_filter() and add_action()"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>1. Purpose<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>add_action()<\/code><\/strong> \u2192 Used to <strong>execute custom code<\/strong> at specific points (hooks) during WordPress execution.<\/li>\n\n\n\n<li><strong><code>add_filter()<\/code><\/strong> \u2192 Used to <strong>modify existing data<\/strong> before it is used or displayed.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"> <strong>2. Syntax<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( $hook, $function_to_add, $priority, $accepted_args );\nadd_filter( $hook, $function_to_add, $priority, $accepted_args );\n<\/code><\/pre>\n\n\n\n<p> They look almost the same. The main difference is <strong>what your function does<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"> <strong>3. Example of <code>add_action()<\/code><\/strong><\/h2>\n\n\n\n<p>Add a message before the post content:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function mytheme_before_content() {\n    echo '&lt;p class=\"notice\"&gt;Welcome to my blog!&lt;\/p&gt;';\n}\nadd_action( 'the_content', 'mytheme_before_content' ); \/\/ action\n<\/code><\/pre>\n\n\n\n<p>\u2705 Runs code at the hook, doesn\u2019t return modified content.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. Example of <code>add_filter()<\/code><\/strong><\/h2>\n\n\n\n<p>Change the length of excerpts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function mytheme_custom_excerpt_length( $length ) {\n    return 20; \/\/ modifies the value\n}\nadd_filter( 'excerpt_length', 'mytheme_custom_excerpt_length' ); \/\/ filter\n<\/code><\/pre>\n\n\n\n<p>\u2705 Modifies a value and <strong>returns it back<\/strong> to WordPress.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"> <strong>5. Key Rule<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Actions<\/strong> \u2192 Do something. (output HTML, enqueue scripts, save data, etc.)<\/li>\n\n\n\n<li><strong>Filters<\/strong> \u2192 Change something. (modify titles, text, content, classes, etc.)<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"> <strong>6. Comparison Table<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th><code>add_action()<\/code><\/th><th><code>add_filter()<\/code><\/th><\/tr><\/thead><tbody><tr><td>Purpose<\/td><td>Run code at a specific hook<\/td><td>Modify and return data<\/td><\/tr><tr><td>Return value<\/td><td>Nothing required<\/td><td>Must return a value<\/td><\/tr><tr><td>Example usage<\/td><td>Enqueue styles, add meta box, display HTML<\/td><td>Modify excerpt length, change title, filter content<\/td><\/tr><tr><td>Hook type<\/td><td>Action hook<\/td><td>Filter hook<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u2705 <strong>In short<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <strong><code>add_action()<\/code><\/strong> when you want to <strong>add functionality<\/strong>.<\/li>\n\n\n\n<li>Use <strong><code>add_filter()<\/code><\/strong> when you want to <strong>change\/modify data<\/strong>.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. Purpose 2. Syntax They look almost the same. The main difference is what your function does. 3. Example of add_action() Add a message before the post content: \u2705 Runs code at the hook, doesn\u2019t return modified content. 4. Example of add_filter() Change the length of excerpts: \u2705 Modifies a value and returns it back [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,2],"tags":[],"class_list":["post-301","post","type-post","status-publish","format-standard","hentry","category-unit-5","category-wordpress"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=301"}],"version-history":[{"count":1,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/301\/revisions"}],"predecessor-version":[{"id":302,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/301\/revisions\/302"}],"wp:attachment":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}