{"id":30288,"date":"2021-09-28T15:00:58","date_gmt":"2021-09-28T13:00:58","guid":{"rendered":"https:\/\/qodeinteractive.com\/magazine\/?p=30288"},"modified":"2021-09-28T09:39:36","modified_gmt":"2021-09-28T07:39:36","slug":"how-to-show-users-last-login-date-in-wordpress","status":"publish","type":"post","link":"https:\/\/qodeinteractive.com\/magazine\/how-to-show-users-last-login-date-in-wordpress\/","title":{"rendered":"How to Show a User&#8217;s Last Login Date in WordPress"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><p>[vc_row][vc_column][vc_column_text]With all manner of commercial and social activity moving online with growing volume and intensity, it should come as no surprise that online security is a growing concern. It is also no surprise <strong>creating high-quality content for the internet is growing ever more complex<\/strong>. Even a small website might have several people behind it, working diligently towards ensuring a good user experience.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]This is why <strong>you may be interested in knowing the exact time each user was logged into your website<\/strong> and in ways to display a user\u2019s last login date and time. This is how both you and the user can <strong>keep track of any suspicious activity<\/strong> and make corrections and changes when needed. It will also make it <strong>easier for you to monitor the time your admins and contributors spend working on the website<\/strong>. There are two easy ways to make that happen: using a plugin and editing your theme files.[\/vc_column_text][vc_empty_space height=&#8221;80px&#8221;][vc_widget_sidebar sidebar_id=&#8221;new-top-picks-banner&#8221;][vc_empty_space height=&#8221;80px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\">How to Show a User\u2019s Last Login Date Using a Plugin<\/h2>\n<p>[\/vc_column_text][vc_column_text]The simplest way to show a user\u2019s last login date is using a plugin. We will be using a free plugin called <a href=\"https:\/\/wordpress.org\/plugins\/when-last-login\/\" target=\"_blank\" rel=\"noopener\">When Last Login<\/a> in order to display a user\u2019s last login time in the back end section of your website: visible to admins and other users with back end access. In order to use it to show the last login date of any of your users, you need to install and activate this plugin.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Having done that, you need to navigate to the plugin\u2019s settings in order to configure it. You will find the settings on the left hand-side menu of your WordPress dashboard, under <strong>When Last Login\/Settings.<\/strong>[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"518\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/When-Last-Login-Settings.jpg\" class=\"attachment-full size-full\" alt=\"When Last Login Settings\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/When-Last-Login-Settings.jpg 969w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/When-Last-Login-Settings-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/When-Last-Login-Settings-768x411.jpg 768w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/When-Last-Login-Settings-620x331.jpg 620w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]You will find that the plugin\u2019s settings have two sections: <strong>Options<\/strong> and <strong>Tools<\/strong>. Under <strong>Options<\/strong>, you will find two checkboxes. The <strong>Record user\u2019s IP address<\/strong> checkbox will, when checked, cause your website to record users\u2019 IP addresses. To avoid falling afoul of <a href=\"https:\/\/qodeinteractive.com\/magazine\/wordpres-gdpr-compliance\/\">GDPR<\/a>, it will also anonymize them. Checking<strong> Enable \u201cAll Login Records\u201d<\/strong> will record all logins. Check them both.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Insofar as the <strong>Tools<\/strong> sections are concerned, you will find the options to <strong>Clear old logs, Clear all logs<\/strong>, and <strong>Clear all IP Addresses.<\/strong> You can use them in order to clear the logs in case you no longer need them. Once you have everything set up, click the <strong>Save Settings<\/strong> button.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"620\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Options-and-Tools.jpg\" class=\"attachment-full size-full\" alt=\"Options and Tools\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Options-and-Tools.jpg 969w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Options-and-Tools-300x192.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Options-and-Tools-768x491.jpg 768w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Options-and-Tools-620x397.jpg 620w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]In order to see when your users have last logged in, navigate to the <strong>Users\/All Users<\/strong> panel from your WordPress dashboard.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"518\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/All-Users.jpg\" class=\"attachment-full size-full\" alt=\"All Users\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/All-Users.jpg 969w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/All-Users-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/All-Users-768x411.jpg 768w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/All-Users-620x331.jpg 620w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]You will find that the plugin is now displaying the last login time for each user. Since we only have one user (<strong>admin<\/strong>), that\u2019s what we are seeing.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"518\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/View-Last-Login.jpg\" class=\"attachment-full size-full\" alt=\"View Last Login\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/View-Last-Login.jpg 969w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/View-Last-Login-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/View-Last-Login-768x411.jpg 768w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/View-Last-Login-620x331.jpg 620w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;78px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\">How to Show a User\u2019s Last Login Date Using Code<\/h2>\n<p>[\/vc_column_text][vc_column_text]You can also use code to display the last login time for each user on the front end side. If you do not come from a technical background, <strong>we strongly suggest you use the plugin method<\/strong> as described above. We also strongly suggest you <a href=\"https:\/\/qodeinteractive.com\/magazine\/how-to-manually-backup-wordpress-website\/\">backup everything<\/a> before making any changes to your theme files.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]In order to show a user\u2019s last login you will need to change your theme\u2019s <em>functions.php<\/em> file. To access it, navigate to <strong>Appearance\/Theme Editor<\/strong> from your WordPress dashboard.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"518\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Theme-Editor-1.jpg\" class=\"attachment-full size-full\" alt=\"Theme Editor\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Theme-Editor-1.jpg 969w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Theme-Editor-1-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Theme-Editor-1-768x411.jpg 768w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Theme-Editor-1-620x331.jpg 620w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]You also need to select the <em>functions.php<\/em> file from the right hand-side menu.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"518\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Functions-PHP.jpg\" class=\"attachment-full size-full\" alt=\"Functions PHP\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Functions-PHP.jpg 969w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Functions-PHP-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Functions-PHP-768x411.jpg 768w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Functions-PHP-620x331.jpg 620w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]Next, you need to copy the following code and paste it into the file, making sure not to disturb any other code:[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/**\r\n* Capture user login and add it as timestamp in user meta data\r\n*\r\n*\/\r\nfunction user_last_login( $user_login, $user ) {\r\nupdate_user_meta( $user-&gt;ID, 'last_login', time() );\r\n}\r\nadd_action( 'wp_login', 'user_last_login', 10, 2 );\r\n\/**\r\n* Display last login time\r\n*\r\n*\/\r\nfunction wpb_lastlogin() {\r\n$last_login = get_the_author_meta('last_login');\r\n$the_login_date = human_time_diff($last_login);\r\nreturn $the_login_date;\r\n}\r\n\/**\r\n* Add Shortcode lastlogin\r\n*\r\n*\/\r\nadd_shortcode('lastlogin','wpb_lastlogin');<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]When you have done that, click the <strong>Update File<\/strong> button.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"668\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Paste-Code.jpg\" class=\"attachment-full size-full\" alt=\"Paste Code\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Paste-Code.jpg 969w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Paste-Code-300x207.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Paste-Code-768x529.jpg 768w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Paste-Code-620x427.jpg 620w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]The next thing you need to do is log out and log back in. The changes in code will only then take effect.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Once you have done that, you will have gained the use of the <em>[lastlogin]<\/em> shortcode. We will show you how to use it as part of a post, but you can use it anywhere you want on your website.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Say we want to show when a particular user was last seen on the website, and say we want to incorporate it into the text of a post, or a byline. We will begin by creating a post and giving it a title. Next, to add the body of the post, we will need a <strong>Classic<\/strong> block. To add a <strong>Classic<\/strong> block, click the <strong>plus<\/strong> icon and select the block from the menu.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"518\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Classic-Block.jpg\" class=\"attachment-full size-full\" alt=\"Classic Block\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Classic-Block.jpg 969w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Classic-Block-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Classic-Block-768x411.jpg 768w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Classic-Block-620x331.jpg 620w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]We will now add some <a href=\"https:\/\/qodeinteractive.com\/magazine\/lorem-ipsum\/\">lorem ipsum<\/a> text and the Admin\u2018s signature. The <em>[lastlogin]<\/em> shortcode is in the place where we want to show the last login time.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"518\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Add-Text.jpg\" class=\"attachment-full size-full\" alt=\"Add Text\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Add-Text.jpg 969w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Add-Text-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Add-Text-768x411.jpg 768w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Add-Text-620x331.jpg 620w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]Once you have everything just right, click <strong>Publish<\/strong>. The shortcode will now display the time of your last login.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]You may not want this particular datum to be accessible to all visitors of your website at all times. Not to worry: you can always <a href=\"https:\/\/qodeinteractive.com\/magazine\/create-wordpress-private-page\/\">turn your post or page into a private page<\/a>.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"469\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Last-Login-Preview.jpg\" class=\"attachment-full size-full\" alt=\"Last Login Preview\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Last-Login-Preview.jpg 969w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Last-Login-Preview-300x145.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Last-Login-Preview-768x372.jpg 768w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/09\/Last-Login-Preview-620x300.jpg 620w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;78px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\">In Conclusion<\/h2>\n<p>[\/vc_column_text][vc_column_text]As you can see, displaying a user\u2019s last login date is not a big deal and it can save you a lot of trouble when determining who accessed what and when or whether a user account has been hacked or not, or maybe if you just want to monitor the time your administrators and contributors spend working on your website. Regardless of your motivation, now you know how it\u2019s done.<br \/>\n[\/vc_column_text][\/vc_column][\/vc_row]<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Need an extra layer of security and monitoring? Log a user&#8217;s last login date on your WordPress website.<\/p>\n","protected":false},"author":16990,"featured_media":30308,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[34,4,13],"class_list":["post-30288","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-security","tag-tips","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts\/30288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/users\/16990"}],"replies":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/comments?post=30288"}],"version-history":[{"count":0,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts\/30288\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/media\/30308"}],"wp:attachment":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/media?parent=30288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/categories?post=30288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/tags?post=30288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}