Last active
September 5, 2024 01:51
-
-
Save tomjn/aa391d345f8de1ab3c02 to your computer and use it in GitHub Desktop.
Easy method of returning or echoing strings on filters and actions
This file contains 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 | |
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' ); | |
} | |
This file contains 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 | |
// 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