Skip to content

Instantly share code, notes, and snippets.

@tomjn
Last active November 1, 2017 14:45
Show Gist options
  • Save tomjn/aa391d345f8de1ab3c02 to your computer and use it in GitHub Desktop.
Save tomjn/aa391d345f8de1ab3c02 to your computer and use it in GitHub Desktop.
Easy method of returning or echoing strings on filters and actions
<?php
class WP_Filter_Return {
private $val='';
public function __construct( $val ) {
$this->val = $val;
}
public function display() {
echo $this->val;
}
public function return_val() {
return $this->val;
}
}
function wp_filter_return( $val ) {
$obj = new WP_Filter_Return( $val );
return array( $obj, 'return_val' );
}
function wp_filter_echo( $val ) {
$obj = new WP_Filter_Return( $val );
return array( $obj, 'display' );
}
<?php
// output a string in the pages header
add_action( 'wp_head', wp_filter_echo( 'Hello World!' ) );
// replace the current posts content with "Hello World!"
add_action( 'the_content', wp_filter_return( 'Hello World!' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment