Skip to content

Instantly share code, notes, and snippets.

@eteubert
Created November 27, 2011 10:20
Show Gist options
  • Select an option

  • Save eteubert/1397353 to your computer and use it in GitHub Desktop.

Select an option

Save eteubert/1397353 to your computer and use it in GitHub Desktop.
WordPress: Template Helpers using Anonymous Functions
<?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...
}
}
<div class="postbox">
<h3><span><!-- postbox name --></span></h3>
<div class="inside">
<!-- postbox content -->
</div> <!-- .inside -->
</div>
<?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
});
@thefuxia

Copy link
Copy Markdown
call_user_func( $content );

Really? :)

@eteubert

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment