Tips and Tricks HQ Support Portal › Forums › WP eMember › eMember – Show First name and Subscription level on comments
- This topic has 10 replies, 3 voices, and was last updated 8 years, 8 months ago by Chucho.
-
AuthorPosts
-
February 23, 2016 at 1:18 pm #12907ChuchoMember
Hi,
I’d like to modify my comments.php template so instead of ‘user_name’, my theme shows ‘first_name’ and ‘user_membership_level_name’ for each one of the users who commented an article.
I’ve been trying -without much luck- to find a way to do it. Could you give me a hand with this piece of code?
Many thanks in advance!
February 23, 2016 at 2:43 pm #72607wzpModeratorThe following should be of help:
https://www.tipsandtricks-hq.com/wordpress-membership/wp-emember-miscellaneous-tweaks-248
February 23, 2016 at 4:10 pm #72608ChuchoMemberThanks for the quick reply Wzp.
Following your link I arrived to a piece of code which could work:
Code:<?php
$field_name = ‘first_name’;
$member_id = ‘1’;
$value = wp_emember_get_user_details_by_id($field_name, $id);
echo $value;
?>But my problem is:
How do I find out the eMember user-ID for the author of each comment?
Code:get_comment(get_comment_ID())->user_id;returns the WordPress user ID, but the numbers are not correlative between eMember and WP.
Note: In case it helps, the ‘WordPress User Integration functionality’ is active, so users are created in both tables of the DDBB.
February 23, 2016 at 9:06 pm #72609wzpModeratorFebruary 24, 2016 at 9:06 am #72610ChuchoMemberHi Wzp, and thanks again.
That are exactly the instructions I followed, unfortunately they rely in knowing the eMember user ID (which is what I don’t know how to get from each one of the comments)
February 24, 2016 at 9:44 pm #72611adminKeymasterThe eMember ID is a key for looking up the details of the user. So you need to tell the function which member’s info you want to retrieve.
I think you are trying to retrieve the logged-in user’s info. You should use the following option which will retrieve and show the currently logged-in member’s details:
February 25, 2016 at 9:10 am #72612ChuchoMemberHi, thanks for taking the time to reply.
I’ll try to clarify my question:
Please look at this image, it’s a screenshot of the comments section of my website: [https://k60.kn3.net/DDD3ABE4E.png]
It currently shows ‘Test1’, which is the eMember username of the person who made the comment. What I need is to show his ‘first_name’ and ‘user_membership_level_name’ instead.
I already know how to retrieve the logged-in user’s info, what I need in the comments section is to retrieve the details of the users who published the specific comments.
Hope it’s clearer now
Thanks again for your help!
February 25, 2016 at 10:54 pm #72613adminKeymasterI don’t know the answer to that specific question because I will need to look at the code of your theme (that comment area section’s code) to understand what has been done there.
What I can do is tell you what functions we have available in the plugin to retrieve info of a user. How you use it in your custom code is entirely upto you. If you want us to create the custom solution for you (inspect your code and apply new code) then it needs to be handled as custom job:
March 2, 2016 at 12:14 pm #72614ChuchoMemberHi again,
I finally managed to solve my problem, so here are some clues if someone is looking to do the same.
(Note: The code still needs some polishing, but it already works and could serve well as an starting point)
My requirements:
– Show first and last name of the user who commented a post (easy task).
– Show the membership level of the user who placed the comment, only if it’s a paid membership (slightly more tricky).
My solution:
– Given that I couldn’t find a way to retrieve the details I needed from the eMember system, I used the ‘WordPress User Integration functionality’ to replicate my members in the regular WP tables.
– Also, for my second requirement, I had to create two new user roles in WP (please google how to do it) and assign them to my free and my paid eMember membership levels (you can define it on the ‘Membership level’ panel).
Now, in your template ‘comments.php’, add the following code (also works on child themes) to define the variables:
Code:// Find out the commenter First and Last name
$commenter_id = $comment->user_id;
$commenter_fname = get_usermeta($commenter_id,’first_name’);
$commenter_lname = get_usermeta($commenter_id,’last_name’);// Find out the commenter role
$user = new WP_User( $commenter_id );
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role );
if ($role == ‘XXX’) { // Check if the role is the one you defined as ‘paid’
$commenter_role = ZZZ; // Assign a public name to that role
}
}And the following code wherever you want to display the details:
Code:<?php
// Show user First and Last name. Otherwise, show username
if ($commenter_fname != ”){
echo $commenter_fname .’ ‘;
echo $commenter_lname;
}else{
comment_author_link();
}// Show user role public name
echo $commenter_role;
?>Last, if you want to go even further and change the sentence “Connected as USERNAME” plus adding your own “Not connected, connect here” text, you would have to apply kind of a ‘dirty hack’, hiding the default comment form through CSS and adding a new one using the following code (at the bottom of comments.php):
Code:<?php
global $current_user;
get_currentuserinfo();$comments_args = array(
‘logged_in_as’ => ‘<p class=”logged-in-as”>Logged in as ‘ .$current_user->user_firstname.’ ‘.$current_user->user_lastname. ‘</p>’,
‘must_log_in’ => ‘<p class=”must-log-in”>Login here</p>’,
// More parameters: https://codex.wordpress.org/Function_Reference/comment_form
);comment_form($comments_args);
?>Hope it helps! As I said, it is not the cleanest way of doing it, but it works
[Any comment on how to improve this code would be appreciated!]
March 3, 2016 at 12:10 am #72615adminKeymasterWhat you did looks good.
I can see your code, so I want to suggest another potential solution. I think you could just do the following (you won’t need to rely on WP User integration for this).
The following is what I want to do (roughly):
1) We are going to get the currently logged in member’s ID from the system. That means this is the user who is viewing the comment area now.
2) We then show that member’s name and details in the comment area.
<?php
if(wp_emember_is_member_logged_in()){
//The visitor is a member and logged into the site
//Lets get the member ID
$emember_auth = Emember_Auth::getInstance();
$emember_user_id = $emember_auth->getUserInfo('member_id');
//Now you can use this member ID to retrieve the details and show it
//(using the functions we discussed earlier)
}
?>March 3, 2016 at 4:05 pm #72616ChuchoMemberThanks for the new code! I’ll give it a try to see if I can replace it, or combine both of them
-
AuthorPosts
- You must be logged in to reply to this topic.