Instructions to get user and custom user field in wordpress

Tutorials 0 lượt xem

Continuing with the series of wordpress theme programming tutorials , today I will show you how to customize the user field as well as how to get the list of users to the frontend.

When making a wordpress website, customers rarely pay attention to the user part. But if you touch websites with member management, coders are often confused and very time consuming.

So today I will summarize some knowledge related to users in wordpress.

Some user-related functions in wordpress

  • is_user_logged_in() -> This function checks if the user is logged in to the web or not
  • get_current_user_id() -> Get id of logged in user
  • wp_get_current_user() -> Get information of currently logged in user
  • get_user_by(‘id’, 1) -> Get user user when id is known
  • get_users($args) -> Get list users with parameter
  • get_avatar(1, 32 ) -> Get user avatar: 1 is the user id, 32 is the avatar size (32px)
  • get_user_meta($id, $key, true) -> Get user meta field

Custom user field in wordpress

What are custom user fields?

By default, each user in wordpress will have fields such as: Username, email, display name, avatar, description … And like the other custom functions I have introduced, wordpress also provides custom functions or is add a new user field.

Adding a new user field is called a custom user field.

Create custom user fields

For ease of use, I will have an example: Create an additional field to store the user’s address

To create a custom user field in wordpress, add this code to the functions.php file of the theme you are using.

*Add an address entry area for users

<?php add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?><h3>Thông tin cá nhân</h3>
 

Nhập địa chỉ

123456789101112131415

Explain:

  • my_show_extra_profile_fields  is the display function from entering the address, this function you can name as you like. This form is like a normal input form, you can enter many different fields. This function is hooked into 2 actions  , show_user_profile, edit_user_profile
  • Action  show_user_profile  is the action that allows to display the address input form in the viewing area of ​​a user’s profile page.
  • The action  edit_user_profile  is an action that allows to display the address input form in the personal information editing area of ​​a certain user.
  • Name = ” address “: address is the key of the meta field, this key is used to get the data out

*Save address information in database

<?php add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    function my_save_extra_profile_fields( $user_id ) {
        if ( !current_user_can( 'edit_user', $user_id ) )
        return false;
        update_usermeta( $user_id, 'address', $_POST&#91;'address'&#93; );
    } 
?><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>
</div>

Explain:

  • my_save_extra_profile_fields  is the function that saves the address to the database. This function is hooked into two actions:  personal_options_update  and  edit_user_profile_update
  • personal_options_update:  An action that saves information in the user information viewing area.
  • edit_user_profile_update:  An action that saves information in the edit user area.

=> To create a custom user field you have to use both of the above codes, the results are as follows

Instructions to get user and custom user field in wordpress

Create a custom user field with a plugin

Like all other functions in wordpress, this custom user field also has dozens of plugins to support such as:

I have shared acf pro plugin here, if you need it, download it:

Get a list of users in wordpress

I will get the list of 20 newest users of the website. At the same time display the name, address, avatar of the user, you insert the following code into the area to display the list of users.

<?php $array = array(
		'number' => 20,
		'orderby' =&gt; 'id',
		'order' =&gt; 'DESC'
	);
	$listUser = get_users($array);
?&gt;	
<div class="sidebar-child">
	<div class="list-user">
		<ul>
<?php foreach ($listUser as $key => $value) { ?&gt;
			<li data-toggle="tooltip" data-placement="left" title="&lt;?php echo $value-&gt;display_name; ?&gt;">
				<p><?php echo $value->display_name; ?&gt;</p>
                                <p><?php echo <span class="crayon-e">the_author_meta<span class="crayon-sy">(</span> 'address'<span class="crayon-sy">,</span> $value-&gt;ID <span class="crayon-sy">)</span><span class="crayon-sy">; </span>?&gt;</p>
				<a class="ico-brod">
					<?php echo get_avatar( $value->user_email, 40 ); ?&gt;
				</a>
			</li>
			<?php } ?>
</ul>
</div>
</div>
<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>
</div>

Epilogue:

So today I have guided you to the custom user field function, hope it will be useful in your theme programming learning process.

See you in the next posts.

Bài viết liên quan