Show user registration date to all users page – Learn WordPress from a to z

Tutorials 0 lượt xem

Assume that there is a spam attack on your site. There are almost 50 registered spammers, but your site has over 1000 users. How to delete the last 50 users without accessing the database?

Here is the solution:

display the user's registration date

On this screenshot, the Registration Date column is added. And when you click on the column header, the users in this table will be sorted by their subscription date – from newest or oldest.

I had a similar post but with the data showing the last login of the user . If you are interested in a similar topic, don’t skip that article.

Create a new column in the user page

Please add the code below to the file functions.php of the theme you are using.

add_filter( 'manage_users_columns', 'hk_modify_user_table' );
function hk_modify_user_table( $columns ) {

	$columns['registration_date'] = 'Ngày đăng ký';

	return $columns;

}

add_filter( 'manage_users_custom_column', 'hk_modify_user_table_row', 10, 3 );
function hk_modify_user_table_row( $row_output, $column_id_attr, $user ) {
	
	$date_format = 'j M, Y H:i';

	switch ( $column_id_attr ) {
		case 'registration_date' :
			return date( $date_format, strtotime( get_the_author_meta( 'registered', $user ) ) );
			break;
		default:
	}

	return $row_output;

}
<div><span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span></div>

In the data display part, I use the get_the_author_meta function and pass the registered parameter to get the registration time. You can also learn this function and display more different data depending on your needs!

Make the data column sortable

Leave the Registration Date column sortable. Please use the code below and continue adding it to the functions.php file .

add_filter( 'manage_users_sortable_columns', 'hk_make_registered_column_sortable' );

function hk_make_registered_column_sortable( $columns ) {
	return wp_parse_args( array( 'registration_date' => 'registered' ), $columns );
}
<div><span>1</span><span>2</span><span>3</span><span>4</span><span>5</span></div>

Epilogue

I hope this article will help you to manage users on the site more effectively. Especially for those of you who are developing websites related to community development.

If you found this article helpful, please comment and share this article. In addition, you can follow the  WordPress Tips section  and follow  Facebook  for more new knowledge.

Bài viết liên quan