It is not always required technically but it is good practice to follow for multiple reasons:
- Code is more organized by keeping hook-related stuff together
- The more precise conditions when it runs – the better for performance
- It is easier to unhook function, that performs multiple add_, then unhook each add_ individually.
- The order of adding functions matters when not specifying priority.
- The child theme (if used) is processed before parent theme and it can lead to confusing stuff with hooks if add_ and remove_ are performed inline.
Of course in some specific examples (yours does fit) it does seem overly verbose to write out all of that just to manipulate single item on single hook.
Myself I use couple of my own (and somewhat messy functions) – add_filter_return()
and add_action_with_arguments()
to lower amount of wrapping when working with hooks.
Related Posts:
- Issue with get_theme_mod returning a blank value instead of the saved value
- How do I “unhook” / de-register jQuery so that it’s not called as part of wp_footer();?
- How to trigger function on theme delete?
- Help with WordPress Query
- Problem only while using require_once() within functions.php
- How can I add class to every quote element?
- How to display message (with switch_theme hook) after deactivating My theme?
- What hook is the currently active theme’s functions.php file loaded on? [duplicate]
- after_setup_theme is called three times
- Why I can’t load my JavaScripts using an hook inside my functions.php file?
- Functions.php error – when trying to change the theme
- How would I got about calling a php file every time someone publishes a new post?
- Set custom directory in theme for page templates
- Webflow Interactions on WordPress site?
- Don’t prepend WordPress base url to image paths
- Adding a wp_head hook from an included PHP file
- Fatal error Class-wp-hook, again, but not sure what’s triggering it
- How can I allow the Editor Role to change Theme Settings?
- get_template_part vs action hooks in themes
- Undefined offset: 0 in > […] /wp-includes/capabilities.php on line 1067
- why doesnt is_home() work in functions.php
- Editing Links in the Footer of WordPress Themes with Base64 Encrypted Code?
- How to set two different themes on one WordPress? (Desktop vs. Mobile)
- How can I add a set featured image function to a theme that doesn’t already have it built in?
- WP showing “warning: call_user_func_array()”, What to do?
- How can I tell if I’m on a login page? [duplicate]
- Display custom_background outside wp_head()
- Declare a function before plugin does on the theme functions.php file
- register_theme_directory and broken deploys
- Can we create a child theme for a premium child theme built on Genesis framework?
- WordPress ignoring specified template for front page. Why?
- Migrating October CMS to WordPress
- Why are my sidebars not registering in unit tests
- Set WordPress Featured Image For All Post Slugs Matching Image File Name in Specified Directory
- Theme editor not showing correct index.php
- How to start learning CSS and theme development?
- How do I make the category template display full posts instead of partial posts?
- Is It wrong to use oop approach on functions.php?
- PHP Parse error: syntax error, unexpected end of file, expecting elseif (T_ELSEIF) or else (T_ELSE) or endif (T_ENDIF) on line 230 [closed]
- Child theme not using parent theme css/styles/formatting?
- Add function to every post?
- Echo tags used to describe the theme
- Get WordPress to use another custom “author.php” file (“custom-author-file.php”)
- wp_add_inline_style() not working?
- X-Axis of Site is Messed Up Because it is in Hebrew
- How to create my own sidebar in Twenty Eleven child theme?
- Using a post-signup hook to get user details
- WP can’t create new directory
- how to make custom ajax handler?
- Theme Customizer not loading JS for live preview
- style.css not reflecting changes. I have no cache installed
- Can I use wordpress themes without wordpress?
- jQuery does not work
- Override the WordPress core function wp_referer_field
- Preserve theme settings when migrating from local to live
- Unable to access wordpress login.php [closed]
- How to return hook data when multiple parameters are present?
- Permissions for installing themes and files in general?
- Translate a site based on different themes
- Any Good WordPress Themes Compatible With The BBPress Plugin? [closed]
- Template messed up on viewing posts
- WordPress wraps span tags into p tags
- Show hyperlinks only when user is loggedin
- What data does WordPress theme update pull If there is an update?
- remove theme’s name from dashboard .. How?
- how to stop a gallery slideshow
- How can i set default pages to a word press theme? it shouldn’t be changed even if i install the theme in different host
- WordPress 2022 template, how to see the blocks added in post template on post edit?
- Two themes using a Multiple Themes plugin
- Broken theme, stylesheet is missing after uploading the right theme via FTP
- Contain multiple page templates in one PHP custom template file in WordPress?
- Header menu aligned right on all pages except for single-post page [closed]
- I cannot get an Advanced Custom Field code snippet to work – and ACF support say it should work
- Theme seems to be preventing me from adding a new page to my site
- How Do I Unhook This Parent Theme Function?
- WordPress margin issues
- Use action, filter, or hook to append HTML to WordPress plugin function
- Remove style id inline CSS in WordPress
- How to find file that placed a line in my header
- Fatal error when using array_diff() function inside of wp_update_nav_menu hook?
- Warning: call_user_func() expects parameter 1 to be a valid callback
- Phone numbers showing same color as header [closed]
- Changing admin Textarea to integrated WP editor
- Line break no working on theme
- QA/Staging envirnoment for wordpress sites
- Diagnosing a connection reset problem
- Create /archive page in WordPress Theme
- Symbolic Links for themes – linking one theme to many wordpress installs
- Fatal error: Call to undefined function get_header() and BuddyPress [closed]
- which theme is this? [closed]
- JQuery Error in theme for .live is not a function [closed]
- How can I append and prepend something to all post hyperlinks without using ::before or ::after? PHP hook solution?
- How can I either move the nav bar down or create more space for the header? Theme – Ajaira
- To create a theme or use an existing one? [closed]
- Overwrite functions.php of a theme – What if the theme is updated?
- How to add/edit content Custom Content item in theme
- Is there a WordPress theme for twittstrap? [closed]
- register_nav_menus syntax error, unexpected identifier “register_nav_menus” in
- RTL in posts only [closed]
- Blocking Google Fonts in wordpress website