Skip to content

Instantly share code, notes, and snippets.

@dmitry-korolev
Created September 22, 2015 17:34
Show Gist options
  • Save dmitry-korolev/fda3ce4e2d101f5deea3 to your computer and use it in GitHub Desktop.
Save dmitry-korolev/fda3ce4e2d101f5deea3 to your computer and use it in GitHub Desktop.
Return instead of echoing.
<?php
/**
* This function can be used to return (instead of echoing) results
* of build-in functions, that cannot return, just echo.
* @return mixed Results of called function.
*/
function md_rioe() {
if (func_num_args() === 0) {
trigger_error('Nothing to call', E_USER_WARNING);
return;
}
ob_start();
if (func_num_args() === 1) {
call_user_func(func_get_arg(0));
} elseif (func_num_args() > 1) {
$args = array_slice(func_get_args(), 1);
call_user_func_array(func_get_arg(0), $args);
}
return ob_get_clean();
}
// Examples.
// wp_head();
$wp_head = md_rioe('wp_head');
// body_class('additional-class');
$body_class = md_rioe('body_class', 'additional-class');
// wp_editor('content', 'editor_id', [
// 'wpautop' => false
// ]);
$wp_editor = md_rioe('wp_editor', 'content', 'editor_id', [
'wpautop' => false
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment