If you want to use a variable from a another context in a function, create a class:
class CustomLog
{
private $debug_msg = "";
public function __construct( $msg )
{
$this->debug_msg = $msg;
}
public function log()
{
error_log( $this->debug_msg, 0 );
}
}
add_action( 'init', [ new CustomLog( "Hi there!" ), 'log' ], 20 );
Now you can reuse the class multiple times for different messages and hooks.
Related Posts:
- remove_action on after_setup_theme not working from child theme
- Reuse variable in hook callback
- Add back in child theme what the parent theme removed with remove_action
- How Do I Unhook This Parent Theme Function?
- get_template_directory_uri pointing to parent theme not child theme
- How to customize the_archive_title()?
- How to override parent functions in child themes?
- Remove Actions/Filters added via Anonymous Functions
- Adding a second email address to a completed order in WooCommerce [closed]
- Child theme – Overriding ‘require_once’ in functions.php
- Trying to use add_action and do_action with parameters
- Include files in child theme functions file
- Check if post is being published for the first time, or is an already published post being updated
- Redeclare a function in a child theme
- How to use the do_action () with parameter
- Perform an action when post is updated/published
- WordPress Theme Update Action?
- How to override a function when isn’t at functions.php
- Extract image from content and set it as the featured image
- Child theme functions.php do I use php open and close tags?
- Do WordPress’ cron’s clean up expired transients?
- add_action in a function, is it possible?
- Declaring an instance of class included in parent theme from child theme functions.php
- One Child Functions.php for Multiple Child Themes
- Using a private method as an action callback from within a class
- Refresh page after form action
- How to restrict actions and filters “properly” by conditions
- How to count number of functions attached to an action hook?
- Add action hook conditionally – only when home.php in use
- Removing custom background and header feature in child theme
- Using add_filter() in Widgets
- How to override function in functions.php of parent theme?
- Implementing DNS Prefetching with WordPress
- How can I tell if I’m on a login page? [duplicate]
- override parent theme configuration in child functions.php
- Child theme – overriding add_image_size by a child theme [duplicate]
- syntax for remove_filter in parent theme with class
- Is it possible to override this function/class in a child theme?
- How to override functions.php in child theme?
- Overriding core functions in child theme
- Change meta tags programatically
- Convert hyphen to underscore in permalinks
- Override parent theme function that is not hooked or in the functions.php file
- Using get_terms for custom taxonomy in functions.php
- Fatal error: Call to undefined function add_action() – an untouched problem
- Function in Child Theme not overriding Parent Theme function [duplicate]
- Change parent theme file function in child themes functions.php
- Adding extra SVGs to TwentyNineteen child theme using class TwentyNineteen_SVG_Icons
- How to hook into the quick edit action?
- Child theme – copied some files from parent to child website still uses parent files
- How to override filter in child theme?
- I created a child theme and it doesn’t work for some of the css files
- Call to undefined add_action() in theme’s functions.php
- add sidebar area to header of child theme
- Unregister Nav Menu with fallback?
- Child Theme Not Overriding Parent Theme
- WooCommerce add_action hook results in 500 error
- Changes to functions.php not working
- Overriding methods in a child theme
- How to override enqueued styles using a child theme
- Should I ask a theme developer to use locate_template rather than require_once
- Automatically Add Specified Value to Attachment Metadata upon Upload
- Using the child theme functions.php to change the customizer.php on parent theme
- Can’t Update function.php after writing short code
- Child theme functions.php not executing
- How to store / access files in child theme folder
- How to update feed only 2-3 times a week (for Feedburner email)?
- Make “sidebar template” the default template for new pages
- override parent theme configuration in child functions.php
- wp_enqueue JavaScript in child-theme (ReferenceError) using Search & Go
- Overwrite Parent Theme add_image_size in Child Theme
- Proper way of using functions in action hook?
- How would go about if I just want a temporary function?
- Twenty Eleven Child theme error when attempting to use a rewritten function
- How do I get a child theme to load scripts from the parent theme?
- Passing arguments to my function with do_action and add_action is not working
- Pass parameters to function through an action
- Accepted arguments value in hook functions
- how to edit functions.php in a child theme
- How to select a page within admin?
- How to manage arrays from custom functions stored in functions.php?
- Best practice when altering a parent theme function inside a child theme?
- How to make child theme inherit parent custom theme options [closed]
- How do I pass arguments for multiple functions hooked to a single action?
- Which action hook to use for function?
- Include files in child themes, declare in functions.php
- How to remove set_post_thumbnail_size() in child themes?
- Custom HTML in specific category single page and its descendant categories
- Avoid loading css from parent theme
- Modify a function without editing template
- Creating a child theme – functions.php code issue
- Echo tags used to describe the theme
- Don’t delete a page if it holds users
- Overwriting Parent Theme’s Image Sizes
- How do I add Bootstrap and LESS to my migrated WordPress site?
- Child theme within a child theme?
- Problem only while using require_once() within functions.php
- How can I change a function in a parent theme via a child themes functions.php
- Which action does wp_update_user triggers?
- Twentyten Child Theme: header images display messed up in admin panel