Skip to content

Instantly share code, notes, and snippets.

@juice49
Last active February 1, 2016 15:01
Show Gist options
  • Save juice49/50205b9e3da3e0d49541 to your computer and use it in GitHub Desktop.
Save juice49/50205b9e3da3e0d49541 to your computer and use it in GitHub Desktop.
WordPress "as blog" function
<?php
function asBlog($blogId, $function) {
return function(...$arguments) use($blogId, $function) {
switch_to_blog($blogId);
$output = $function(...$arguments);
restore_current_blog();
return $output;
};
}
// For PHP < 5.6:
function asBlog($blogId, $function) {
return function() use($blogId, $function) {
switch_to_blog($blogId);
$output = call_user_func_array($function, func_get_args());
restore_current_blog();
return $output;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment