BuddyPress / Wordpress Get dynamic user ID - wordpress

I'm trying to make a function that overrides the current users avatar URL with a custom URL that is saved in their user meta. So far the function works except that I'm stumped trying to figure out how to have the function grab the user ID of the user buddypress/wordpress is getting an avatar for.
The code so far looks like this:
// return new URL for avatar
function wpse_49216_my_new_avatar_url() {
// get user_id of user bp/wp is getting avatar for
$user_id = bp_get_member_user_id();
// get avatar name from above user's meta
$avatar_choice = get_user_meta($user_id, "avatar_choice", true);
if($avatar_choice) {
return 'path_to_avatars/'.$avatar_choice.'.png';
} else {
return 'path_to_avatars/default-avatar.png';
}
}
add_filter( 'bp_core_fetch_avatar_url', 'wpse_49216_my_new_avatar_url' );
// Replace image src="" with the new avatar URL
function wpse_49216_filter_bp_avatar( $html ) {
return preg_replace( '/src=".+?"/', 'src="' . wpse_49216_my_new_avatar_url() . '"', $html );
}
add_filter( 'bp_core_fetch_avatar', 'wpse_49216_filter_bp_avatar' );
The main problem is if I set the $user_id to the current logged in user's ID, then all avatars will be load the avatar of the current logged in user. And bp_get_member_user_id() only works on member pages. I need something that works universally. Does any Wordpress/Buddypress expert have any idea how I can get the correct user ID?

I need something that works universally
If you look at the function you are filtering, you'll see that it depends on knowing 'where' you are in BP. Since it is driven by context, there is no universal approach.

You can user below functiona for the Global userID:
bp_displayed_user_id()

Related

How to add verified badge in front of author name across wordpress blog

Good Day,
I have been trying to add verification badge to WordPress users but I no success. I tried using administrator to do the testing by checking if user has role administrator and trying to enter code here update_user_meta (display_name) but I wasn't able to add icons to the display name.
I have as well tried creating a custom field in user profile called verify_user (text field) .... In which I entered the value "verified" and saved ... I have been searching for hooks to use but haven't seen one. I'm not sure which hook/filter to use here
Please is there any solution to adding this verification icon to author's display name in which when the word "verified" is entered into the custom field created in user profile or any other approach available (e.g if user has specific role). I don't mind if the little structure I wrote above would be changed.
Thanks
I was able to get a solution which worked exactly as i wanted. For anyone who might have same task to tackle;
function add_verification_bagdge_to_authors($display_name) {
global $authordata;
if (!is_object($authordata))
return $display_name;
$icon_roles = array(
'administrator',
'verified_author',
);
$found_role = false;
foreach ($authordata->roles as $role) {
if (in_array(strtolower($role), $icon_roles)) {
$found_role = true;
break;
}
}
if (!$found_role)
return $display_name;
$result = sprintf('%s <i title="%s" class="fa fa-check-circle"></i>',
$display_name,
__('This is a Verified Author', 'text-domain-here')
);
return $result;
}
add_filter( 'the_author', 'add_verification_bagdge_to_authors' );
The way i was able to tackle this was to create a user role called Verified Author (using add_role() function from Wordpress Codex ), which will be the role assigned to verified author across the website. All Users with Admin Role are automatically Verified while user role has to be switched from either contributor/Author role to Verified Author.
The above code was able to do 98% of the task but when a verified author / administrator comments, their display name doesn't show the verified badge. I was able to use the below code to fix that (combining the code with a shortcode).
function my_comment_author( $author = '' ) {
// Get the comment ID from WP_Query
$comment = get_comment( $comment_ID );
if ( ! empty($comment->comment_author) ) {
if (!empty($comment->user_id)){
$user=get_userdata($comment->user_id);
$author=$user->display_name.' '. do_shortcode('[this_is_verified-sc]'); // This is where i used the shortcode
} else {
$author = $comment->comment_author;
}
} else {
$author = $comment->comment_author;
}
return $author;
}
add_filter('get_comment_author', 'my_comment_author', 10, 1);
Shortcode to return the Verified Badge if user is admin or verified author
function add_verification_bagdge_to_authors_sc($display_name) {
global $authordata;
if (!is_object($authordata))
return $display_name;
$icon_roles = array(
'administrator',
'verified_author',
);
$found_role = false;
foreach ($authordata->roles as $role) {
if (in_array(strtolower($role), $icon_roles)) {
$found_role = true;
break;
}
}
if (!$found_role)
return $display_name;
$result = sprintf('%s <i title="%s" class="fa fa-check-circle"></i>', $display_name, __('This is a Verified Author', 'text-domain-here') );
return $result;
}
add_shortcode( 'this_is_verified-sc', 'add_verification_bagdge_to_authors_sc' );
Please Note: Not all Magazine theme are properly coded / have a hard coded Block and module elements, so the verified badge might not work on their block / module element. I experienced this during the whole website design process, so we had to change up theme and the only modification i had to make to the new theme was to remove the html escape added to their block/module code, so that the icon can be rendered. i.e. in their module/block code they had something like this esc_html( the_author() ) and i removed the esc_html to have just the_author() an it all worked.
Its not really a straight forward solution but that is how i was able to tackle the task without using a plugin. Just add the codes to your functions.php file and that's it. I hope it helps someone.
Thanks to Kero for pointing me in the right direction, providing the code i was able to work with and manipulate

Hide portion of a widget from author's own profile page - WordPress

I'm using a plugin called Widget Logic.
On every author's page I added a contact me form, where you can send the author a message, but I want to hide it if the author is visiting his own profile page.
I see examples like !is_author(), but I don't know how to make the comparison.
https://wordpress.org/plugins/widget-logic/
You can not do this check with the is_author() function because:
This Conditional Tag checks if an Author archive page is being displayed. This is a boolean function, meaning it returns either TRUE or FALSE.
Of course you can use the is_author() to do the check on a higher level other than the theme template file author.php. For instance when using hooks. Then you could use it.
But I asume you are doing things in the template file, so what you are looking for is something like this:
$current_author = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
var_dump($current_author->ID); // < just for you to confirm what value it returns for the current profile you are on
var_dump(get_current_user_id()); // < just for you so you can see what the current user ID is of the user who is logged in at this moment
if( $current_author->ID!=get_current_user_id() ) {
// Display the form...
}else{
// Maybe do something else or do nothing...
}
Update (deregister widget when author ID is same as logged in user ID)
In this example we deregister a Text widget by passing it's class WP_Widget_Text to the unregister_widget() function.
For a full list of WordPress widget class names refer to: https://codex.wordpress.org/Function_Reference/unregister_widget
Put this in your theme functions.php file.
function riseagainst_remove_text_widget() {
// Because `widget_init` is called early, we can not use `get_permalink()`
// neither can we use `home_url( $wp->request )`, neither can we get the
// current author ID at this level when we are on profile page. And we
// need to deregister the widget at this hook, and not at a later stage
// otherwise it will simply override our deregister call to wordpress.
// That's why we will use some of the `$_SERVER` array to obtain the current
// URL (which is provided by the server upon requesting the page).
// We only need the last part of the URL to check if we are on an author page.
$page_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
// The last part of the URL is the author slug in case we are on the author
// profile page. To get the last part of an URL we use `basename()`,
// this will return the slug of the author
$author_name = basename($page_url);
// Now we have the author slug, we can retrieve all author information
// We only need the author ID but this can also be used for other data
// When plain permalink is enabled, we can not search based on slug,
// Instead we will be provided with the author ID via the URL parameter
// named `author`. So do a check on that first
$current_author = ( isset($_GET['author']) ? get_user_by('id', absint($_GET['author'])) : get_user_by('slug', $author_name) );
// This check is important to only execute the code whenever we actually
// are on an author page, when no author was found, we are probably not
// on an author page. (note that if you have problems with it, you can
// also do a check on if the URL path contains `/author/`)
if( $current_author ) {
// Only display the widget whenever current author ID
// is not equal to current logged in user ID
if( $current_author->ID!=get_current_user_id() ) {
// Display the widget...
}else{
// Do not display the widget...
unregister_widget('WP_Widget_Text');
}
}
}
add_action( 'widgets_init', 'riseagainst_remove_text_widget' );

Change Review Display Name on Woo-commerce Store

I want to be able to change the name that shows up on the reviews on a woocommerce platform. I have found some information but I am new to WordPress and no one seems to have the answer. Here is a link that I was able to find something but no installation specifics.
I need to know where and how to change this please. Thanks.
Here is the link that I found.
https://silicondales.com/tutorials/woocommerce-tutorials/woocommerce-change-review-author-display-name-username/
I have added a plugin called Username Changer and it let me change the username of the account but it won't update the review username.
Here are some images as of now (3/6/2017)
Snapshot of how user is configured with edited username:
Ok I have figured it out. You will need to post the code into the functions.php file if you do not have a child theme. It is recommended that you set one up but I haven't yet and I just wanted to get this fixed first. Here is a snapshot of what you need to too.
Add the below block of text to the bottom or your custom-functions.php or functions.php page within Appearance > Editor
add_filter('get_comment_author', 'my_comment_author', 10, 1);
function my_comment_author( $author = '' ) {
// Get the comment ID from WP_Query
$comment = get_comment( $comment_ID );
if (!empty($comment->comment_author) ) {
if($comment->user_id > 0){
$user=get_userdata($comment->user_id);
$author=$user->first_name.' '.substr($user->last_name,0,1).'.'; // this is the actual line you want to change
} else {
$author = __('Anonymous');
}
} else {
$author = $comment->comment_author;
}
return $author;
}
Make sure to update the user info.
Missing Sections from the Original Question

How to hide media uploads by other users in the Media manager

i am using wordpress multisite and wan to hide medea which others have uploaded. Like if X User of that site have uploaded any media in the wordpress, Y User should not be able to see or access this from there login. Please help
You could try something like this.
/**
* Allow access to own content only
*/
function my_authored_content($query) {
//get current user info to see if they are allowed to access ANY posts and pages
$current_user = wp_get_current_user();
// set current user to $is_user
$is_user = $current_user->user_login;
//if is admin or 'is_user' does not equal #username
if (!current_user_can('manage_options')){
//if in the admin panel
if($query->is_admin) {
global $user_ID;
$query->set('author', $user_ID);
}
return $query;
}
return $query;
}
add_filter('pre_get_posts', 'my_authored_content');
This will only let admin and the author see the content.
You can either add it to the main functions file or turn it into a plugin.
To create it as a plugin:
Create a new file
add the code from here: http://pastebin.com/rfMLM0BU
save it as my-authored-content.php
upload it to your plugins directory.
Hope this helps you! :-)
For me works this:
function mymo_parse_query_useronly( $wp_query ) {
if(isset($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] == 'attachment'){
if ( !current_user_can( 'level_5' ) ) {
$wp_query->set( 'author', get_current_user_id() );
}
}
}
add_filter('parse_query', 'mymo_parse_query_useronly' );
I use this for uploaded profile picture for the user profile in front end

Wordpress : Page that can be viewed only by members and if not a member than show them a registration page

I am trying to create a sampling process , where a user need to register or login to view my sample pdf documents , and once they register the user should be given a role as a sampler .
I am not a pro at wordpress but am learning . Can u guys suggest me some plugin or ideas on how to do it , i am using wordpress WordPress 3.3.1 .Thanks
http://codex.wordpress.org/Function_Reference/is_user_logged_in
http://codex.wordpress.org/Function_Reference/wp_login_form
As per your latest question, you will need to use these in tandem with each other, and make sure to define your redirect within your argument array to make sure the user is directed back to the page from which they logged in.
FINAL UPDATE: Enter this in your functions.php file and utilize as you see fit. This will allow you to output information according to the actual role of your user. This is also assuming that your plugin is creating actual custom roles. If not, you will have to utilize the Add Role Function:
function get_user_role()
{
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
Then in the template of the page you want:
<?php
if(is_user_logged_in())
{
switch(get_user_role())
{
case 'customer' :
//CUSTOMER DISPLAY
break;
case 'subscriber' :
//SUBSCRIBER DISPLAY
break;
default :
//OTHER TYPE OF USER DISPLAY
}
}
else
{
//NO LOGGED USER DISPLAY
}
?>

Resources