Created
November 27, 2011 10:20
-
-
Save eteubert/1397353 to your computer and use it in GitHub Desktop.
WordPress: Template Helpers using Anonymous 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
| <?php | |
| // Alternative 1 | |
| function my_postbox_fun() { | |
| # code... | |
| } | |
| postbox( 'My Awesome Postbox', 'my_postbox_fun' ); | |
| // Alternative 2 | |
| class my_example_class | |
| { | |
| function __construct() { | |
| postbox( 'My Awesome Postbox', array( $this, 'my_postbox_fun' ) ); | |
| } | |
| function my_postbox_fun() { | |
| # code... | |
| } | |
| } |
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
| <div class="postbox"> | |
| <h3><span><!-- postbox name --></span></h3> | |
| <div class="inside"> | |
| <!-- postbox content --> | |
| </div> <!-- .inside --> | |
| </div> |
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
| <?php | |
| /** | |
| * Postbox helper function. | |
| * | |
| * @param string $name | |
| * @param function $content | |
| */ | |
| function postbox( $name, $content ) { | |
| ?> | |
| <div class="postbox"> | |
| <h3><span><?php echo $name; ?></span></h3> | |
| <div class="inside"> | |
| <?php call_user_func( $content ); ?> | |
| </div> <!-- .inside --> | |
| </div> | |
| <?php | |
| } | |
| postbox( 'My Awesome Postbox', function () { | |
| $my_option = get_option( 'my_option' ); | |
| ?> | |
| <form action="" method="post"> | |
| <!-- ... --> | |
| </form> | |
| <?php | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really? :)