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
- Redeclare a function in a child theme
- How to use the do_action () with parameter
- How to override a function when isn’t at functions.php
- Extract image from content and set it as the featured image
- Do WordPress’ cron’s clean up expired transients?
- One Child Functions.php for Multiple Child Themes
- 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?
- Removing custom background and header feature in child theme
- How to override function in functions.php of parent theme?
- override parent theme configuration in child functions.php
- syntax for remove_filter in parent theme with class
- 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]
- Call to undefined add_action() in theme’s functions.php
- Overriding methods in a child theme
- 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
- 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
- Twenty Eleven Child theme error when attempting to use a rewritten function
- 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 do I pass arguments for multiple functions hooked to a single action?
- Creating a child theme – functions.php code issue
- Overwriting Parent Theme’s Image Sizes
- Which action does wp_update_user triggers?
- Twentyten Child Theme: header images display messed up in admin panel
- Trigger a custom function when option are saved in admin area
- Redirect to another page if the user is logged in when pressing again the login button on menu bar
- How to load parent theme template parts in child theme
- Difference b/w Simple function call & do_action call
- Set Taxonomy based on post status
- Running script based on Category
- How would I go about replacing this function in my child theme located in inc/template-tags.php
- add_action template_redirect not working for home page
- Enqueuing latest version of jQuery into a child theme returns a blank screen
- AMP – Change rel=”canonical” from functions.php [closed]
- Dequeue script to prevent javascript event conflict on wordpress child theme
- Return child theme url
- Got white blank page after copying the functions.php to child theme
- How do I change a parent theme’s function through the child theme?
- Is there a way to prevent a function/method from being called outside a specific hook?
- Missing argument for the function
- Randomizing wp_tag_cloud() in child theme
- Adding code before post title with the_title produces weird results
- Where do I go to edit this area? [closed]
- How to include any template using Shortcode fuction?
- Init action and refresh page after form action
- How to remove admin footer text from inside child theme?
- How to overwrite template.php file
- remove_action not working, even after changing priority [duplicate]
- Do we need to change our child function.php to require/include child dir files when we add an over-riding file.php into the child theme
- How to use parent theme functions in child theme functions.php file?
- Removing parent theme CSS without editing parent theme
- Child theme functions.php
- Change custom featured image size in twentythirteen child theme
- How do I find the code executed when wp_head() is called?
- Include widget file in functions.php of child theme
- how can I add filter in specfic field in my website?
- remove_action from parent theme using child theme functions.php
- How to Override Page Template if URL matches query?
- Save_post – Warning: Cannot modify header information
- If has action not working as expected
- Modifying a WordPress Plugin
- Is_Page doesnt detect my page
- How to add javascript code into Divi child theme?
- How to show only specific category post by user role without plugin and restrict all other cats
- Postback redirect through add_action is not triggered
- Remove action hook from Class, understanding OOP
- Change default color scheme in twentyfifteen child theme?
- How to change form action of wp-login page with a function
- Will dequeueing in child theme functions.php file prevent Google Fonts from loading?
- WordPress hooks to call a function inside a construct
- uninstall a theme programmaticlly
- Pass arguments to function class with do_action()
- Adding a meta box fields to child theme options page
- Update add_image_size
- What add_action reference should I be using or should I use do_action?
- Load a Child Theme’s style.css just before the closing tag
- Getting error while trying to use custom comment function
- Settings in functions.php used by a plugin
- Blank child theme – functions.php problem
- Child Theme not working – CSS gone
- How do I make my child theme’s CSS update when I save it?
- How to fix enqueue_styles error for a twenty-seventeen childtheme
- delete_term is not working properly with add_action()
- post_row_actions filter from parent theme not executing in child theme
- How to get variable from other function inside class function using add_action for Ajax call