OK… I implemented a shortcode to insert a switchback link.
You can call it from PHP like this, with or without a custom message:
<?php echo do_shortcode( "[switch_back_link]" ) ?>
<?php echo do_shortcode( "[switch_back_link]WARNING: Don't use the logout button![/switch_back_link]" ) ?>
To do this, I added this line in __construct():
add_shortcode('switch_back_link', array($this,'switch_back_link_shortcode'));
I initially wrote switch_back_link_shortcode() as a call to admin_notice() wrapped in ob_start() … ob_end_clean(), but I couldn’t get it to work. The script quietly aborted in or around admin_notice(). Probably something a more experienced PHP programmer would figure out, but it stumped me. So I just copied the guts of admin_notice() into my function. Not elegant, but gets me what I need.
function switch_back_link_shortcode ($atts, $content = null) {
global $user_identity, $user_login;
ob_start();
if ( $old_user = $this->get_old_user() ) {
?>
<div id="user_switching" class="updated">
<p><?php
if ( isset( $_GET['user_switched'] ) )
printf( __( 'Switched to %1$s (%2$s).', 'user_switching' ), $user_identity, $user_login );
printf( ' <a href="%s">%s</a>.%s', $this->switch_back_url(),
sprintf( __( 'Switch back to %1$s (%2$s)', 'user_switching' ),
$old_user->display_name, $old_user->user_login ),
(!is_null($content) ? " <span>$content</span>" : "" )
);
?></p>
</div>
<?php
} else if ( isset( $_GET['user_switched'] ) ) {
?>
<div id="user_switching" class="updated">
<p><?php
if ( isset( $_GET['switched_back'] ) )
printf( __( 'Switched back to %1$s (%2$s).', 'user_switching' ), $user_identity, $user_login );
else
printf( __( 'Switched to %1$s (%2$s).', 'user_switching' ), $user_identity, $user_login );
?></p>
</div>
<?php
}
$sb_link = ob_get_contents();
ob_end_clean();
return $sb_link;
}
I’ve not tested this extensively, but it does work in header.php while switched and not.
John – Feel free to use or adapt this in any way you would like in your module. I’d rather abandon my branch if you do.
-dave