[Plugin: User Switching] Template tag show switch back link
-
Hi. I’m trying to display a simple switch back link in my themes, is there a template tag to achieve this?
Thanks
-
Hi ctekmusic,
There are no template tags for the switching links. You can achieve it with something simple like this though:
global $user_switching; if ( $user_switching->get_old_user() ) echo '<a href="' . $user_switching->switch_back_url() . '">Switch Back</a>';John
+1 for a shortcode to insert a switch back link.
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
Hi John and Dave.
Thank you both so much for the help. This is exactly what I was looking for!I needed to switch to one user account easily for every admin user.
I searched how to display a “switch to” link in admin notice and i found.I don’t know if someone need this but i share my code, insert to line 225 juste after
<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 }i added this lines
else { //switch to timothee <a href="' . $this->switch_to_url( $user->ID ) . '">'. _e( 'Switch To', 'user_switching' ).'</a> ?> <div id="user_switching" class="updated"> <p><?php if ( current_user_can( 'switch_to_user', 1 ) ) printf( '<a href="' . $this->switch_to_url( 1 ) . '">' . __( 'Switch To', 'user_switching' ) . ': Timothée</a>'); ?></p> </div> <?//end of switch to timothee }The number 1 is the ID account of the user i want to switch. It appear two times you have to change it if needed.
The username of this account is Timothée so i wrote Timothée you have to change it too, or simply erase it.I think the code is without security problem can someone confirm that ?
I’m sorry for my English, especially if there are mistakes.
The topic ‘[Plugin: User Switching] Template tag show switch back link’ is closed to new replies.