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 to change background color in the Notepad++ text editor?
- How to change or add theme to Android Studio?
- WordPress frameworks and parent themes [closed]
- Opinions and recommendations on the best barebones base theme [closed]
- Remove JSON API links in header html
- Using OOP in themes
- Where can I download WordPress themes from? [closed]
- Disable plugin / plugin action via theme
- How can I allow the Editor Role to change Theme Settings?
- What are all the available parameters for query_posts?
- remove_action on after_setup_theme not working from child theme
- Customizing a WordPress theme without changing it?
- Where can I learn to create my own theme?
- How do I turn off self-closing tags for markup in WordPress (for HTML5, or HTML4, for example)?
- how to add version of style.css in wordpress
- Does WordPress work without a theme?
- What is the role and history of the $content_width global variable?
- Adding admin-ajax.php to the frontend. Good or bad idea?
- get_template_part vs action hooks in themes
- How to move the sidebar in TwentyFifteen to the right?
- How to Link External jQuery/Javascript files with WordPress
- Installation failed: Download failed. No working transports found
- Issues with title-tag and document_title_parts
- What is theme-compat?
- Override parent theme translation on child theme
- WordPress Theme activation hook?
- How to load scripts/styles specific for a page
- Can wordpress theme folder name be changed freely and nothing technically happens
- How many times can I hook into the same action?
- Link to specific Customizer section
- Is there any global functions.php file which works for any theme?
- What does exactly GPL license mean for my WordPress theme?
- Am I allowed to license my WordPress theme under the aGPL
- Using classes instead of global functions in functions.php
- How do I change the header image height in Twenty Seventeen?
- Starter Theme vs Parent Theme? Pros and cons
- How to change the language for the front-end only?
- W3 Total Cache, CDN and theme files [closed]
- Child theme – Overriding ‘require_once’ in functions.php
- Best way to include Bootstrap in WordPress
- How to set thumbnail image for a (child) theme
- Different template of products for specific category. WooCommerce
- Show different theme for admin?
- How can I version the main CSS file?
- Add custom classes to anchor in wp_nav_menu
- what the best way to include images from the template’s images folder?
- Display random categories on the front page (Finding and Editing Theme Functions)
- Is it OK to remove theme credits from footer? [duplicate]
- Undefined offset: 0 in > […] /wp-includes/capabilities.php on line 1067
- Shared functionality in plugins and themes
- How can I get the title attribute from get_the_post_thumbnail()?
- Import WordPress XML File from Within Functions.php
- Dash or underscore in theme folder name?
- No “Add New” Button. How to add new theme?
- How do I add support to my theme for custom menus?
- “Unexpected error” on update requests
- How to prevent plugin, theme installation failures on WordPress?
- How do I white label my self-hosted site created by wordpress?
- Do Child-Themes automatically load the Translation from the Parent-Theme?
- Define custom Page Template without its own .php file
- Does hooking into the same action multiple times drain memory?
- When cropping a header image, retain meta data (i.e. name, description, etc.) from original image?
- Best practices for a Style/CSS based theme options page?
- How to create a new theme from scratch?
- Categorising themes by folders in backend
- Switching themes without losing widgets?
- How to influence the information displayed on widget inside wp-admin
- How to use media upload on theme option page?
- Dynamic template serving, change theme_root using add_filter from current theme
- What theme is good for posting code? [closed]
- WordPress 5 / Gutenberg – theme doesn’t have featured image option showing up
- Custom Taxonomy-specific JavaScript
- How to refresh WordPress Customizer panel upon entering a value
- Set Featured Image for Archive templates
- Extract image from content and set it as the featured image
- is_front_page only works in theme file, and does not work in functions.php
- What is meant by __(‘page’,’twentytwelve’)
- How do I add a new string to a .po or .pot file?
- Adding Language Support using .pot and .mo files to a WordPress Theme?
- Is there a hook or function I can use to display all theme files being used on a current page?
- Difference between stylesheet_directory and template_directory
- Free/Open-Source Theme Frameworks as an Alternate to Thesis? [closed]
- can require “themefolder/includes/widgets.php” in “themefolder/functions.php” yet widgets.php doesn’t run