Created
March 7, 2013 18:42
-
-
Save kucrut/5110603 to your computer and use it in GitHub Desktop.
For Alex: Passing Arguments to Callback Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Example class | |
* | |
* This class demonstrates how to pass data to a callback function | |
* without using global varaibles. | |
*/ | |
class Example_Class { | |
public function __construct( $strings ) { | |
$this->strings = $strings; | |
foreach( $this->strings as $string ) { | |
add_action( $string[ 'hook' ], array( $this, 'echo_strings' ) ); | |
} | |
} // End function __construct() | |
public function echo_strings() { | |
$hook = current_filter(); | |
foreach ( $this->strings as $string ) { | |
if ( $string['hook'] == $hook ) { | |
echo $string['message'] . "<br />\n"; | |
} | |
} | |
} | |
} // End class Example_Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Beautiful! Thanks.