php - how to switch between two form for website visitor once admin logged in wordpress -


i facing problem: have 2 forms form1 , form2 now. want achive once admin logged in anywhere visitor (non logged in) wordpress site should see form1 on page.

else, should see form2? did below:

if(current_user_can('author' )|| current_user_can('administrator'))      {               show form1            } else {               show form2     } 

now, works fine admin screen not other user or visitor. want universal.

thanks in advance. using code in custom page template. please me, guys. tutorial me or if there other way achieve this. please suggest me.

to achieve have register admins logging in or out somewhere. use transient keep eye on whos logging in, , registering if admin (someone capable of managing settings).

the following not tested, should work. past of functions.php, or create plugin it:

//this function checks logging in, , adds database if admin.  add_action('wp', 'update_online_users_status'); //registeres function  function update_online_users_status(){     $lastactive = 5 * 60; //an admin vas last active 5 mins ago (5 x 60 secs)      if(is_user_logged_in()){             // online users list, if theres logged in                 if(($logged_in_users = get_transient('admins_online')) === false)    $logged_in_users = array();                 $current_user = wp_get_current_user();                     $current_user = $current_user->id;                       $current_time = current_time('timestamp');                      if (current_user_can( 'manage_options' )) { //checks if admin logging in                             if(!isset($logged_in_users || ($logged_in_users < ($current_time - ($lastactive)))){                                 $logged_in_users = $current_time;                                 set_transient(‘admins_online', $logged_in_users, $lastactive);                             }                     }             }     } }  //the actual check. returns true/false based on if theres been active admin past x minutes function is_there_an_admin_online() {    // online users list   $logged_in_users = get_transient('admins_online');    return isset($logged_in_users && ($logged_in_users > (current_time('timestamp') - (5 * 60))); } 

set whatever timeout want, replacing 5 * 60 - 1 time in each of above functions.

then able check so:

$isadminonline = is_there_an_admin_online(); if( $isadminonline == true )      {               show form1            } else {               show form2     } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -