Skip Navigation

[Resolved] How to check if the post author is logged in

This support ticket is created 5 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by ioannisM-2 5 years, 6 months ago.

Assigned support staff: Luo Yang.

Author
Posts
#526668

Hello,

I am trying to add a conditional in the "profile" post type and show sth if the post author is logged in. I tried to use the shortcode [wpv-user info="logged_in" id="[wpv-post-author format="meta" meta="ID"]"] but it returns just the author's name. The shortcode [wpv-current-user info="logged_in"] works only for the current user. Is there a way to show that information about another user or the post author?

Thank you!

#526840

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Dear Ioannis,

I assume you are going to check these:
If the current user is same as the post author of current post.

Please try the wpv-conditional shortcode like this:

[wpv-conditional if="( '[wpv-current-user info="login"]' eq '[wpv-post-author format="meta" meta="user_login"]' ) "]
current user is same as the post author of current post.
[/wpv-conditional]

More help:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-author
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional

#526905

Dear Luo,

thank you for your response! I propably didn't explain well. I do not need the information for the current user. I need to know if the post author is logged in or not and show that information in his post to everybody ( logged in users or visitors ). The issue is that [wpv-current-user info="login"] shows that info only for the current user. I need to to show the info if another user that is the post author is logged in.

I tried

[wpv-conditional if="( '[wpv-user info="login"]' eq '[wpv-post-author format="meta" meta="user_login"]' ) "]
Author is connected now[/wpv-conditional]

but it is not working.

#527036

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Thanks for the details, there isn't such a feature within Views plugin, the attribute value "user_login" of shortcode [wpv-user field="logged_in" ... ] is correspond to the database table wp_users, column "user_login", which is the username.

In your case, I suggest you try the solution in other website, for example:
https://wordpress.stackexchange.com/questions/34429/how-to-check-if-a-user-not-current-user-is-logged-in

For your reference.

#527069

Dear Luo,

I have found that thread 2 days before and I have tried all the solutions with no success. I need to know if the user is logged in or not, add a sign in the profile, create a list with the logged in users and if is possible to add this option to the parametric search. So I created the field of the post type provider wpcf-my-status that can take the values "0" and "1". Is there a way the value of the field been updated to 1 when the post author logs in and to 0 when he is logged out? I have found these 2 functions but I do not know how to combine them

function add_session_mitesh()
{
    global $wpdb;
    if(is_user_logged_in())
    {
        $user_id = get_current_user_id();
        $wpdb->query("UPDATE wp_users SET `status` = '1' WHERE `wp_users`.`ID` = $user_id;");
    }
     $user_id;
}
add_action('init','add_session_mitesh');


function delete_session_mitesh() {
global $wpdb;
    if(is_user_logged_in())
    {
        $user_id = get_current_user_id();
        $wpdb->query("UPDATE wp_users SET `status` = '0' WHERE `wp_users`.`ID` = $user_id;");
    }
 $user_id;
}
add_action('wp_logout', 'delete_session_mitesh');

and

add_filter('my_custom_actions', 'my_custom_post_actions', 10, 3);
function my_custom_post_actions ($custom_actions, $post_id, $form_data) {
 
    // if the post is of type 'provider'
    $postType = get_post_type( $post_id );
    if ( $postType == 'provider' )
    {
      // set the value to 1
      $custom_actions[] = array( 'meta_key' => 'wpcf-my-status', 'meta_value' =>'1' );
    }
    return $custom_actions;
}
#527070

Ooh, I am sorry! I accidentally marked the thread as resolved so I reopen it

#527283

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Please try this:
1) Add below codes into your theme/functions.php:

add_action('wp', 'update_online_users_status');
function update_online_users_status(){

  if(is_user_logged_in()){

    // get the online users list
    if(($logged_in_users = get_transient('users_online')) === false) $logged_in_users = array();

    $current_user = wp_get_current_user();
    $current_user = $current_user->ID;  
    $current_time = current_time('timestamp');

    if(!isset($logged_in_users[$current_user]) || ($logged_in_users[$current_user] < ($current_time - (15 * 60)))){
      $logged_in_users[$current_user] = $current_time;
      set_transient('users_online', $logged_in_users, 30 * 60);
    }

  }
}

add_shortcode('author_is_user_logged_in', 'author_is_user_logged_in');
function author_is_user_logged_in($user_id) {
	if(!$user_id){
		echo $user_id =  get_the_author_meta( 'ID' );
	}
	// get the online users list
	$logged_in_users = get_transient('users_online');
	print_r($logged_in_users);
	$res = 0;
	// online, if (s)he is in the list and last activity was less than 15 minutes ago
	if(isset($logged_in_users[$user_id]) && ($logged_in_users[$user_id] > (current_time('timestamp') - (15 * 60)))){
		$res= 1;
	};
	return $res;
}

2) Dashboard-> Toolset-> Front-end Content
section "Third-party shortcode arguments", add the custom shortcode name “author_is_user_logged_in”

3) Use above shortcode in wpv-conditional shortcode like this:

[wpv-conditional if="( '[author_is_user_logged_in]' eq '1' ) " debug="true"]
Author is connected now
[/wpv-conditional]
#527351

Dear Luo,

thank you so so much for your support!!! Your solution worked like a charm!!!

1)The only issue is that before the "Author is connected" appears that code:

1Array ( [1] => 1495437345 ) 

How could I fix this?

Also how could I achieve the following?

2) Show in a view all the "provider" posts of the logged in users.
3) Add a filter in the post type's parametric search so the visitors can choose to see posts of the authors that are logged in?

Thank you!

#527362

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Q1) Please modify the PHP codes as below:

add_action('wp', 'update_online_users_status');
function update_online_users_status(){
 
  if(is_user_logged_in()){
 
    // get the online users list
    if(($logged_in_users = get_transient('users_online')) === false) $logged_in_users = array();
 
    $current_user = wp_get_current_user();
    $current_user = $current_user->ID;  
    $current_time = current_time('timestamp');
 
    if(!isset($logged_in_users[$current_user]) || ($logged_in_users[$current_user] < ($current_time - (15 * 60)))){
      $logged_in_users[$current_user] = $current_time;
      set_transient('users_online', $logged_in_users, 30 * 60);
    }
 
  }
}
 
add_shortcode('author_is_user_logged_in', 'author_is_user_logged_in');
function author_is_user_logged_in($user_id) {
    if(!$user_id){
         $user_id =  get_the_author_meta( 'ID' );
    }
    // get the online users list
    $logged_in_users = get_transient('users_online');
    $res = 0;
    // online, if (s)he is in the list and last activity was less than 15 minutes ago
    if(isset($logged_in_users[$user_id]) && ($logged_in_users[$user_id] > (current_time('timestamp') - (15 * 60)))){
        $res= 1;
    };
    return $res;
}

Q2) and Q3) they are new questions.
Please create new thread for the new questions, you can assign it to me directly, thanks

#527365

Perfect, Luo thank you so so much!!!