webappguides
Create Wordpress Theme From Scratch! Build Your Own

How to get author email in WordPress For Email Button

In this post, you will come across with get_author_meta function.

It is a WordPress function that can give author email.

When get_author_meta is called it should pass a parameter value special for fetching the email.

It is worth knowing the kind of values the function can process since there are 27 field values for this function to deal with.

To get the author’s email, get_author_meta uses the user_email variable.

In fact.

That brings the email address of any author of the posts blog created.

Although get_the_author_email can provide a user email it is deprecated.

It is better to avoid it but the post will retrieve the author’s email using get_the_author_email as well.

WordPress Author Email Snippet Code

Open index.php and place the code to any section where the email is displayed.

First, know the way email can be obtained in WordPress.

get_author_meta('user_email');

Then, put the code in the anchor tag.

<a class="userEmail" href = "mailto:<?php echo get_the_author_meta('user_email');?>">Email A Post Author</a>

What the above snippet does.

It posts the email in a quoted mark.

Assume the email is myauthoremail@gmail.com

The output of anchor tag after the href attribute is

mailto:myauthoremail@gmail.com

Design a writer email button.

At first, the email of a writer is just a text with a message Email a Post Author.

For a better user experience creates a button around the user email.

The email anchor tag has userEmail class.

The class will set a button color and the size in CSS.

.userEmail{

text-decoration : none;

color : #fff;

background : #00a2e8;

padding : 10px 10px;

}

 

.userEmail:hover{

background : #0080 b7;

}

A little bit of description on userEmail CSS code.

The first line after opening the curl bracket removes the underline when a user hovers the email.

The next line after text-decoration sets a text a white color.

The line after a color property sets light blue background color.

The padding property specifies the space of text and its bottom edge as well as the upper edge.

You need to make the user aware when the button is active.

The property hover is provoked when the email button element is touched.

The line with attribute background changes color from light blue to dark blue when the user touches(hovers) a button.

Author email using the_author_meta.

the_author_meta can also output the email of the author.

It works a similar way with a get_the_author_meta.

<?php the_author_meta('user_email');?>

the_author_meta doesn’t echo while the other one requires the echo keyword to print a user email.

Hence, this function can be the alternative as it retrieves the current post author details including the email.

You can combine the_author_meta with the HTML anchor tag as follow.

<a class="userEmail" href = "mailto:<?php the_author_meta('user_email');?>">Email A Post Author</a>

get_the_author_meta vs the_author_meta

the_author_meta use get_the_author_meta to show the user’s email.

This means the_author_meta is a dependant function.

The raw source of the_author_meta takes the value from the get_the_author_meta.

The returned values are held by a variable $author_meta.

Then apply_filter function takes $author_meta to print the email.

That is why the_author_meta can output the returned value without echoing.

Load the user email by id

Both functions are able to take users’ emails using the id.

This doesn’t need to be called inside the while loop.

A single author email can repeat multiple when you pass the id in one function inside a while loop.

The requirement to get the email address by author id is to know the ID of the specific author.

the_author_meta('user_email',342);

And the second method.

get_the_author_meta('user_email', 342);

How can you find the author’s id?

You can use the get_post_field method to get the ID of the author outside the loop.

$authorID = get_post_field('post_author', $idPost);

get_post_field also requires getting the ID of the specific post.

Learn here to get more details get_the_ID.

The id of a certain post can be fetched this way.

$idPost = get_the_ID();

Wrap-Up

To sum up, WordPress offers two functions you can to get the author’s email.

These functions are the_author_meta and get_the_author_meta.

The field value that returns an email is ‘user_email’.

The user email can be obtained by using the author’s id.

It is possible to add the email button as a submenu.

 


Write a Reply or Comment

Your email address will not be published. Required fields are marked *