Hide portion of a widget from author's own profile page - WordPress - 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' );

Related

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

BuddyPress / Wordpress Get dynamic user ID

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()

Redirecting from Custom Admin Page Produces "Headers already sent"

I've registered a custom Admin page in my plugin through add_submenu_page. In the callback function (the one that generates the contents of the admin page), I have the following code:
wp_redirect('http://google.com');
exit;
However, when I visit the admin page I get an error:
Warning: Cannot modify header information - headers already sent by (output started at ..\wp-admin\includes\template.php:1637) in ..\wp-includes\pluggable.php on line 878
The callback from add_submenu_page happens too late (after the admin sidebar and header are rendered), this is why the location header can not be sent anymore.
To accomplish this, we need to hook a function a bit earlier in the WordPress admin area, before the headers are sent (e.g. admin_init).
A good way:
function myplugin_preprocess_pages($value){
global $pagenow;
$page = (isset($_REQUEST['page']) ? $_REQUEST['page'] : false);
if($pagenow=='admin.php' && $page=='myplugin-custom-page-slug'){
wp_redirect('http://google.com');
exit;
}
}
add_action('admin_init', 'myplugin_preprocess_pages');
The above code will redirect you to Google whenever you try to view wp-admin/admin.php?page=myplugin-custom-page-slug.
In my case, I've attached the custom page via add_submenu_page to the default (admin.php) parent in the Admin area and I've set the custom page's slug to myplugin-custom-page-slug. Feel free to replace the values in the code above or even add a PHP switch if you have a lot of custom admin pages.
This way we have hooked early enough to do a redirection whenever our custom admin page is viewed.
Update: (A different approach)
Thanks to this post, I've learned that WordPress creates a unique action that you can hook to for each custom admin page (load-{parent_page_slug}_page_{plugin_subpage_slug}). For example, if you've added a custom admin page with parent admin.php and slug myplugin-custom-page, you can hook to its "load" action in the following manner:
add_action( 'load-admin_page_myplugin-custom-page', 'myplugin_custom_page_redirect' );
function myplugin_custom_page_redirect() {
if ( 'myplugin-custom-page' == filter_input( INPUT_GET, 'page' ) ) {
wp_redirect( 'http://google.com' );
exit;
}
}
Note that the action name has some things to consider. It's a mixture of underscores and dashes and make sure you only include the parent page's name without the extension (so "admin" instead of "admin.php")

Getting hold of metadata when creating a post in WordPress

I am using the save_post action to inspect a metadata field in a custom post and take some action on that value. This is the essential guts of how I am doing it:
add_action('save_post', 'my_save_post');
function my_save_post($post_id)
{
// Check if not autosaving, processing correct post type etc.
// ...
// Get the custom field value.
$my_field_value = get_post_meta($post_id, 'my_field', true);
// Do some action
// ...
}
This works fine when updating the post through the admin page. However, when first creating the post, the my_field_value is always empty. The field does get saved correctly, but this action trigger does not seem to be able to see it, nor any other custom field values.
I would like the action to be performed on all posts of this type created, and I will be importing many through the CSV Imported plugin. Even then, the custom fields do get imported correctly, and the action trigger does get fired for each row imported, but the save_post action still cannot see the custom field value.
So far as I can see from documentation, the post has already been created by the time this action fires, so I should always be able to see that custom metafield.
The answer, it seems, is in the order in which things happen. When creating a post from a form, the custom fields are all collected by the appropriate actions and added to the post before my save_post action fires. This means my trigger is able to see those custom field values.
When importing from CSV, the basic post is created first, and then the custom metafields are added. The save_post trigger fires on the first creation, before the metafields are added, and so the custom field data is not visible to the save_post action.
My solution was to catch the updates of the metadata using the updated_post_meta and added_post_meta actions as well as the save_post action:
add_action('updated_post_meta', 'my_updated_post_meta', 10, 4);
add_action('added_post_meta', 'my_updated_post_meta', 10, 4);
function my_updated_post_meta($meta_id, $post_id, $meta_key, $meta_value)
{
// Make sure we are handling just the meta field we are interested in.
if ($meta_key != 'my_custom_field') return;
if (wp_is_post_revision($post_id)) return;
if (get_post_type($post_id) != 'my_post_type') return;
if (trim($meta_value) == '') return;
// Do my custom task (linking this post to a parent post in a different
// post type). This is the same task performed by the save_post action.
my_link_product_track($post_id, trim($meta_value));
}
That is essentially what I do, and it seems to work well. I do encapsulate all the above into a custom class in the theme, and don't recommend using global scope variables as shown here, but this is just to show the method.
You should look at using $post->ID instead of $post_id -
$my_field_value = get_post_meta($post->ID, 'my_field', true);
get_post_meta in the Codex
EDIT:
Could you do something like this?
if($post->ID == ''){
$pid = $post_id;
} else {
$pid = $post->ID;
}
//$pid = $post->ID or $post_id, whichever contains a value
$my_field_value = get_post_meta($pid, 'my_field', true);
something that looks for a value in $post->ID and $post_id, and uses whichever one isn't blank?

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