I am trying to create my custom front-end upload of users avatars.
I created form and created custom fields/meta data for it but I have problems with overriding get_avatar() wordpress function and I occurred with next problems.
My function can't get userId and size, I tried multiple solutions but I can't find solution why my get_avatar wont accept size and userID or userEmail
Code in functions.php
add_filter( 'get_avatar' , 'my_get_avatar' );
function my_get_avatar( $user_id, $size = '', $default = '', $alt = false ) {
var_dump($size);
if ( empty( $size ) || ! is_numeric( $size ) ) {
$size = 96;
}
$custom_avatar = get_user_meta( $user_id, '_user_avatar', true );
if(!($custom_avatar))
{
$url = "https://webkit.org/demos/srcset/image-src.png";
}
var_dump($custom_avatar);
$html = sprintf('<img src="%s" width="%s" height="%s" class="Avatarmss">', $url, $size,$size);
return $html;
}
When I put var_dump of $userID i get string with img tag and gavatar source
<img alt='' src='http://1.gravatar.com/avatar/d10ca8d11301c2f4993ac2279ce4b930?s=32&d=mm&r=g' .....
For dumping of size I get empty string.
I found solution.. Every core function is called before functions.php so I created custom plugin for this
try
add_filter( 'get_avatar','my_get_avatar' , 10, 5 );
Related
I have a feature that allows users to upload their own images as avatars. The uploaded images are stored correctly in the meta_key named image. So it works.
The point is that uploaded images don't show up in the comments section, admin panel, or anywhere on the site. This is because wordpress does not use the image meta_key to display images, but it does with the get_avatar filter.
What I am trying to do is set the meta_key image in the get_avatar filter, this way the user avatars should be visible in the comments section, reviews and everywhere on the site.
I'm trying to run the code below and it only works for the admin section, so in the backend I can actually see the avatars uploaded by each user. But gravatar's default images still appear everywhere in the front-end of the site.
What am I doing wrong ?
// Replace default Gravatar Image used in WordPress
add_filter( 'get_avatar', 'filter_get_avatar', 10, 5 );
function filter_get_avatar( $avatar, $current_user, $size, $default, $alt ) {
// If is email, try and find user ID
if ( ! is_numeric( $current_user ) && is_email( $current_user->comment_author_email ) ) {
$user = get_user_by( 'email', $current_user );
if ( $user ) {
$current_user = get_current_user_id();
}
}
// If not user ID, return
if( ! is_numeric( $current_user ) ) {
return $avatar;
}
// Get attachment id
$attachment_id = get_user_meta( $current_user, 'image', true );
// NOT empty
if ( ! empty ( $attachment_id ) ) {
// Return saved image
return wp_get_attachment_image( $attachment_id, [ $size, $size ], false, ['alt' => $alt] );
}
return $avatar;
}
Update:
In this way the images are displayed in the comments section, admin and I believe also elsewhere on the site. But the problem is that all users get the same image. What am I doing wrong ?
function wdoc_filter_get_avatar_url( $url, $id_or_email, $args ) {
$current_user_id = get_current_user_id();
$file_id = get_user_meta( $current_user_id, 'image' , true );
return wp_get_attachment_image_url($file_id);
}
add_filter( 'get_avatar_url', 'wdoc_filter_get_avatar_url', 10, 3 );
I'm posting this answer because the solution below is working for me. I'm not a developer, but a fan working on their own website. For this reason, the code below may contain errors. If anyone wants to make improvements corrections are welcome, I would appreciate it very much. For now it works for me.
It helped me solve the problem: https://rocketgeek.com/code-snippets/custom-avatar-image-based-on-image-field-in-wp-members/
Thanks to CBroe for the tips he suggested to me.
add_filter( 'get_avatar_url', 'my_custom_avatar_author', 10, 3 );
function my_custom_avatar_author( $avatar, $id_or_email, $size ) {
// What is the custom image field's meta key?
// Set this value to match the meta key of your custom image field.
$meta_key = "image";
// Nothing really to change below here, unless
// you want to change the <img> tag HTML.
$user = false;
if ( is_numeric( $id_or_email ) ) {
$user = get_user_by( 'id' , (int)$id_or_email );
}
elseif ( is_object( $id_or_email ) ) {
if ( ! empty( $id_or_email->user_id ) ) {
$id = (int)$id_or_email->user_id;
$user = get_user_by( 'id' , $id );
}
} else {
$user = get_user_by( 'email', $id_or_email );
}
if ( $user && is_object( $user ) ) {
$post_id = get_user_meta( $user->ID, $meta_key, true );
if ( $post_id ) {
$attachment_url = wp_get_attachment_url( $post_id );
$avatar = wp_get_attachment_image_url($post_id, $size = array('50', '50')); // HTML for the avatar <img> tag. This is WP default.
}
}
return $avatar;
}
I have an issue related save_post_($post-type) hook and ACF gallery field.
I got this code snippet from somewhere. Let me explain: I created an ACF gallery field (acf-gallery is the field name) shown in Custom Post Type (cpt1 is the slug) then use this snippet to set the first image of this gallery as the featured image when saving just like what Woocommerce do.
But what if I want it to work with another Custom Post Type (let's say the slug is cpt2)? Can I use array( 'cpt1', 'cpt2' ) to replace cpt1? Is there a way to include multiple custom post types?
/* Set the first image generated by ACF gallery field as featured image */
add_action( 'save_post_cpt1', 'set_featured_image_from_gallery' );
function set_featured_image_from_gallery() {
global $post;
$post_id = $post->ID;
$images = get_field('acf_gallery', $post_id, false);
$image_id = $images[0];
if ( $image_id ) {
set_post_thumbnail( $post_id, $image_id );
}
}
I edited this snippet using save-post hook according to comments below. But I don't know if it's valid. Can someone help?
/* Set the first image generated by ACF gallery field as featured image */
add_action( 'save_post', 'set_featured_image_from_gallery' );
function set_featured_image_from_gallery($post_id) {
if (get_post_type($post_id) != array( 'cpt1', 'cpt2')) {
return;
}
$has_thumbnail = get_the_post_thumbnail($post_id);
if ( !$has_thumbnail ) {
$images = get_field('acf_gallery', $post_id, false);
$image_id = $images[0];
if ( $image_id ) {
set_post_thumbnail( $post_id, $image_id );
}
}
}
I usually use save_post hook, $post_id variable, get_post_type and in_array functions.
function set_featured_image_from_gallery($post_id)
{
$included_cpts = array('cpt1', 'cpt2', 'cpt3');
if (in_array(get_post_type($post_id), $included_cpts)) {
$has_thumbnail = get_the_post_thumbnail($post_id);
if (!$has_thumbnail) {
$images = get_field('acf_gallery', $post_id, false);
$image_id = $images[0];
if ($image_id) {
set_post_thumbnail($post_id, $image_id);
}
}
}
}
add_action('save_post', 'set_featured_image_from_gallery');
Some of our products are using an external image instead of the post thumbnail, I have an acf set up for this url. It appears that you can filter the woocommerce get_image() function, but I can't find a way to get the current product's id to get the field.
Here's what I have so far:
function offsite_product_images($image_url, //variable $this wont work here){
//need some way to get the current product's id
if(get_field('thumbnail_url', $id)){
$image_url = get_field('thumbnail_url', $id);
}
return $image_url;
}
add_filter( 'woocommerce_product_get_image', 'offsite_product_images');
the woocommerce get_image() function:
public function get_image( $size = 'woocommerce_thumbnail', $attr = array(), $placeholder = true ) {
if ( has_post_thumbnail( $this->get_id() ) ) {
$image = get_the_post_thumbnail( $this->get_id(), $size, $attr );
} elseif ( ( $parent_id = wp_get_post_parent_id( $this->get_id() ) ) && has_post_thumbnail( $parent_id ) ) {
$image = get_the_post_thumbnail( $parent_id, $size, $attr );
} elseif ( $placeholder ) {
$image = wc_placeholder_img( $size );
} else {
$image = '';
}
return apply_filters( 'woocommerce_product_get_image', wc_get_relative_url( $image ), $this, $size, $attr, $placeholder, $image );
}
I even tried changing the function to pass the id directly, but it wouldn't do it.
Any help on getting the product or it's id passed to my filter would be greatly appreciated.
Your function code is incomplete, there is some missing arguments as the WC_Product object, that you need to get the product ID. Try the following:
add_filter( 'woocommerce_product_get_image', 'offsite_product_images', 10, 5 );
function offsite_product_images( $image, $product, $size, $attr, $placeholder ){
if( get_field('thumbnail_url', $product->get_id() ) ){
$image = get_field('thumbnail_url', $product->get_id() );
}
return $image;
}
Code goes in functions.php file of your active child theme (or active theme). It should work.
I'm very inexperienced with coding, so to anyone who bears with me a huge thank you in advance!
Here's what I'm trying to do: I want to make my website - that's currently displaying a logo that I've picked - display randomly one of the images that I've uploaded as the logo every time the page reloads.
Now this would be relatively easy if the theme was coded with a header - but for logos I can't find a plugin that does that and also can't manage to add this feature from inside the theme.
This is how the wordpress theme of my website calls for a logo:
<?php
if( $show_logo && has_custom_logo() ) {
the_custom_logo();
Here's the the_custom_logo() functions code:
function the_custom_logo( $blog_id = 0 ) {
echo get_custom_logo( $blog_id );
}
Now my thought for a solution was the following: I change the way wordpress returns an image through get_custom_logo() (I'm prepared to code that in every time there's a WP update).
Here's the functions code:
function get_custom_logo( $blog_id = 0 ) {
$html = '';
$switched_blog = false;
if ( is_multisite() && ! empty( $blog_id ) && (int) $blog_id !== get_current_blog_id() ) {
switch_to_blog( $blog_id );
$switched_blog = true;
}
$custom_logo_id = get_theme_mod( 'custom_logo' );
// We have a logo. Logo is go.
if ( $custom_logo_id ) {
$html = sprintf( '%2$s',
esc_url( home_url( '/' ) ),
wp_get_attachment_image( $custom_logo_id, 'full', false, array(
'class' => 'custom-logo',
'itemprop' => 'logo',
) )
);
}
// If no logo is set but we're in the Customizer, leave a placeholder (needed for the live preview).
elseif ( is_customize_preview() ) {
$html = sprintf( '<img class="custom-logo"/>',
esc_url( home_url( '/' ) )
);
}
if ( $switched_blog ) {
restore_current_blog();
}
return apply_filters( 'get_custom_logo', $html, $blog_id );
}
Am I wrong in thinking that if I change $custom_logo_id = get_theme_mod( 'custom_logo' ); to acquire a random image out of my logo folder that it's gonna work? And if I'm not wrong, can anybody help me with how to write that piece of code?
I apologise if this is a dumb question. Have a great week!
I'm trying to rename files while uploading them to WordPress and I want them to get the name of sanitized post title.
Basically I want to do same thing as here, but unfortunately when I use the code from this answer - I don't get the value of $post variable.
The only thing I get is "empty" name with some numbers at the end and the file extension, e.g. "-5263.png", which increses with every new file.
For some reason I don't get the $post value which would give me the post title and it just changes the file name to... well, nothing and just adding some numbers at the end, so it doesn't overrite any other file.
I really would like to know what's wrong with my code:
function new_filename( $filename, $filename_raw ) {
global $post;
$info = pathinfo( $filename );
$ext = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
$new = $post->post_title;
if ( $new != $filename_raw ) {
$new = sanitize_file_name( $new );
}
return $new . $ext;
}
add_filter( 'sanitize_file_name', 'new_filename', 10 );
Thank you in advance for your help.
I've made a plugin a long time ago called File Renaming on Upload that can help you on this, but if you are searching for help with your code i can say that you can try a different approach to get the post variable. Try this instead:
function get_post() {
global $post;
$post_obj = null;
if( !$post ){
$post_id = isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : false;
if ( $post_id && is_numeric( $post_id ) ) {
$post_obj = get_post( $post_id );
}
}else{
$post_obj = $post;
}
return $post_obj;
}
Once you get your post variable, you don't need to use the post_title like that. You can use
$post->post_name
And then you don't need to use sanitize_file_name() function