Using a function written in my functions.php file within the header.php file The Next CEO of Stack OverflowHelp with accessing wp-admin page and resolving error messagesWarning: array_pop() expects parameter 1 to be array, boolean givenWp Debug Enabled True Notices and WarningMy wordpress site wont load, it gives header error warningswoocommerce plugin bugsNoindex subscriber author pageCan I use require() function in a template file?Register a menu - Error Headerif ( ! function_existsPHP illegal string offset - page/portfolio taxonomy
How to start emacs in "nothing" mode (`fundamental-mode`)
Should I tutor a student who I know has cheated on their homework?
Science fiction novels about a solar system spanning civilisation where people change their bodies at will
Why were Madagascar and New Zealand discovered so late?
Does the Brexit deal have to be agreed by both Houses?
I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin
Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis
How to safely derail a train during transit?
How to write papers efficiently when English isn't my first language?
Why is there a PLL in CPU?
% symbol leads to superlong (forever?) compilations
Can a caster that cast Polymorph on themselves stop concentrating at any point even if their Int is low?
Are there languages with no euphemisms?
Apart from "berlinern", do any other German dialects have a corresponding verb?
Grabbing quick drinks
WOW air has ceased operation, can I get my tickets refunded?
Can I equip Skullclamp on a creature I am sacrificing?
Inappropriate reference requests from Journal reviewers
Why didn't Khan get resurrected in the Genesis Explosion?
What does this shorthand mean?
How to count occurrences of text in a file?
Implement the Thanos sorting algorithm
Why does standard notation not preserve intervals (visually)
Is the concept of a "numerable" fiber bundle really useful or an empty generalization?
Using a function written in my functions.php file within the header.php file
The Next CEO of Stack OverflowHelp with accessing wp-admin page and resolving error messagesWarning: array_pop() expects parameter 1 to be array, boolean givenWp Debug Enabled True Notices and WarningMy wordpress site wont load, it gives header error warningswoocommerce plugin bugsNoindex subscriber author pageCan I use require() function in a template file?Register a menu - Error Headerif ( ! function_existsPHP illegal string offset - page/portfolio taxonomy
I have been asked to do some work on a site where the old developer has written this function (create a variable that stores the users avatar into an img tag) into the functions.php:
add_filter('get_avatar', 'lb_acf_profile_avatar', 10, 5);
function lb_acf_profile_avatar($avatar, $id_or_email, $size, $default, $alt)
$user = '';
// Get user by id or email
if (is_numeric($id_or_email))
$id = (int) $id_or_email;
$user = get_user_by('id', $id);
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)
return $avatar;
// Get the user id
$user_id = $user->ID;
//$user_info = get_userdata($user_id)
// Get the file id
$avatar_url = $user->get('user_url'); //'https://dojo.nearsoft.com/wp-content/uploads/2017/02/Eric-Wroolie-per-template.jpg';
if ($avatar_url == '')
return $avatar;
$avatar = '<img alt="' . $alt . '" src="' . $avatar_url . '" class="avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>';
// Return our new avatar
return $avatar;
I know the function works as the app he built uses the code to generate the avatar for each user. I just don't know how to use it and cannot reach out to him to help me utilise it.
My efforts so far have failed, using code that looks a little like this:
<?php lb_acf_profile_avatar() ?>
<?php if ($avatar != '') : ?>
<div>Hellow World</div>
<?php endif; ?>
Where I have tried calling the function then assuming the returned variable (the avatar image) would be usable from that point. That doesn't appear to be the case.
The error message is 5 of these one for each argument:
Warning: Missing argument 5 for lb_acf_profile_avatar(), called in /home/materialshub/public_html/development/wp-content/themes/bolt/header.php on line 238 and defined in /home/materialshub/public_html/development/wp-content/themes/bolt/functions.php on line 663
Is there a way to tailor this so I get the avatar_url without the img tag returned, but I need the original code to function as it should as it is also used in the app and is functioning correctly.
I don't have access to the app. Or the old developer. Any help you can provide is great. If you want further info just let me know.
I think I want a new function that gets the avatar_url like the function above but without any of the img tag. A simple url is all I need.
I need this to be dynamic as well so it works for all users automatically, generating the avatar_url. How can I pass the arguments in this manor?
I cannot just use the inbuilt get_avatar() WordPress function before we try go down that route as the app has made use of an empty field in the database 'user_url'.
I appreciate this is quite an annoying question, but I appreciate the kindness.
EDIT: I have tried reverting back to the get_avatar() function and that then returns this warning:
Warning: Missing argument 1 for get_avatar(), called in /home/materialshub/public_html/development/wp-content/themes/bolt/header.php on line 239 and defined in /home/materialshub/public_html/development/wp-includes/pluggable.php on line 2450
Thanks, Jason.
php functions
|
show 2 more comments
I have been asked to do some work on a site where the old developer has written this function (create a variable that stores the users avatar into an img tag) into the functions.php:
add_filter('get_avatar', 'lb_acf_profile_avatar', 10, 5);
function lb_acf_profile_avatar($avatar, $id_or_email, $size, $default, $alt)
$user = '';
// Get user by id or email
if (is_numeric($id_or_email))
$id = (int) $id_or_email;
$user = get_user_by('id', $id);
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)
return $avatar;
// Get the user id
$user_id = $user->ID;
//$user_info = get_userdata($user_id)
// Get the file id
$avatar_url = $user->get('user_url'); //'https://dojo.nearsoft.com/wp-content/uploads/2017/02/Eric-Wroolie-per-template.jpg';
if ($avatar_url == '')
return $avatar;
$avatar = '<img alt="' . $alt . '" src="' . $avatar_url . '" class="avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>';
// Return our new avatar
return $avatar;
I know the function works as the app he built uses the code to generate the avatar for each user. I just don't know how to use it and cannot reach out to him to help me utilise it.
My efforts so far have failed, using code that looks a little like this:
<?php lb_acf_profile_avatar() ?>
<?php if ($avatar != '') : ?>
<div>Hellow World</div>
<?php endif; ?>
Where I have tried calling the function then assuming the returned variable (the avatar image) would be usable from that point. That doesn't appear to be the case.
The error message is 5 of these one for each argument:
Warning: Missing argument 5 for lb_acf_profile_avatar(), called in /home/materialshub/public_html/development/wp-content/themes/bolt/header.php on line 238 and defined in /home/materialshub/public_html/development/wp-content/themes/bolt/functions.php on line 663
Is there a way to tailor this so I get the avatar_url without the img tag returned, but I need the original code to function as it should as it is also used in the app and is functioning correctly.
I don't have access to the app. Or the old developer. Any help you can provide is great. If you want further info just let me know.
I think I want a new function that gets the avatar_url like the function above but without any of the img tag. A simple url is all I need.
I need this to be dynamic as well so it works for all users automatically, generating the avatar_url. How can I pass the arguments in this manor?
I cannot just use the inbuilt get_avatar() WordPress function before we try go down that route as the app has made use of an empty field in the database 'user_url'.
I appreciate this is quite an annoying question, but I appreciate the kindness.
EDIT: I have tried reverting back to the get_avatar() function and that then returns this warning:
Warning: Missing argument 1 for get_avatar(), called in /home/materialshub/public_html/development/wp-content/themes/bolt/header.php on line 239 and defined in /home/materialshub/public_html/development/wp-includes/pluggable.php on line 2450
Thanks, Jason.
php functions
1
"I cannot just use the inbuilt get_avatar()" - I think you can? get_avatar just fills in missing arguments then calls the get_avatar filter, and your function is hooked into that filter. So if you call the inbuilt one it should call this code.
– Rup
14 hours ago
How can you tell it links to the basic wordpress function? - I will try to do it now and let you know if it fixes. I originally built it like that but then realised it didnt pull the image through that the user selected in the app. It would always be the basic gravatar avatar.
– Jason Is My Name
14 hours ago
1
Theadd_filter('get_avatar'
line at the top. Here's the built-in get_avatar - you can see that both return statements go throughapply_filters( 'get_avatar' ...)
.
– Rup
14 hours ago
To use this function, you need write something like that:$my_avatar = apply_filter( 'get_avatar', $my_avatar, $user, /* other args */ );
. "apply_filters" on Codex
– nmr
14 hours ago
@Rup - I think youre on to something here - thanks for identifying that. I have tried to call the get_avatar() and now I get the error I posted in my question...
– Jason Is My Name
14 hours ago
|
show 2 more comments
I have been asked to do some work on a site where the old developer has written this function (create a variable that stores the users avatar into an img tag) into the functions.php:
add_filter('get_avatar', 'lb_acf_profile_avatar', 10, 5);
function lb_acf_profile_avatar($avatar, $id_or_email, $size, $default, $alt)
$user = '';
// Get user by id or email
if (is_numeric($id_or_email))
$id = (int) $id_or_email;
$user = get_user_by('id', $id);
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)
return $avatar;
// Get the user id
$user_id = $user->ID;
//$user_info = get_userdata($user_id)
// Get the file id
$avatar_url = $user->get('user_url'); //'https://dojo.nearsoft.com/wp-content/uploads/2017/02/Eric-Wroolie-per-template.jpg';
if ($avatar_url == '')
return $avatar;
$avatar = '<img alt="' . $alt . '" src="' . $avatar_url . '" class="avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>';
// Return our new avatar
return $avatar;
I know the function works as the app he built uses the code to generate the avatar for each user. I just don't know how to use it and cannot reach out to him to help me utilise it.
My efforts so far have failed, using code that looks a little like this:
<?php lb_acf_profile_avatar() ?>
<?php if ($avatar != '') : ?>
<div>Hellow World</div>
<?php endif; ?>
Where I have tried calling the function then assuming the returned variable (the avatar image) would be usable from that point. That doesn't appear to be the case.
The error message is 5 of these one for each argument:
Warning: Missing argument 5 for lb_acf_profile_avatar(), called in /home/materialshub/public_html/development/wp-content/themes/bolt/header.php on line 238 and defined in /home/materialshub/public_html/development/wp-content/themes/bolt/functions.php on line 663
Is there a way to tailor this so I get the avatar_url without the img tag returned, but I need the original code to function as it should as it is also used in the app and is functioning correctly.
I don't have access to the app. Or the old developer. Any help you can provide is great. If you want further info just let me know.
I think I want a new function that gets the avatar_url like the function above but without any of the img tag. A simple url is all I need.
I need this to be dynamic as well so it works for all users automatically, generating the avatar_url. How can I pass the arguments in this manor?
I cannot just use the inbuilt get_avatar() WordPress function before we try go down that route as the app has made use of an empty field in the database 'user_url'.
I appreciate this is quite an annoying question, but I appreciate the kindness.
EDIT: I have tried reverting back to the get_avatar() function and that then returns this warning:
Warning: Missing argument 1 for get_avatar(), called in /home/materialshub/public_html/development/wp-content/themes/bolt/header.php on line 239 and defined in /home/materialshub/public_html/development/wp-includes/pluggable.php on line 2450
Thanks, Jason.
php functions
I have been asked to do some work on a site where the old developer has written this function (create a variable that stores the users avatar into an img tag) into the functions.php:
add_filter('get_avatar', 'lb_acf_profile_avatar', 10, 5);
function lb_acf_profile_avatar($avatar, $id_or_email, $size, $default, $alt)
$user = '';
// Get user by id or email
if (is_numeric($id_or_email))
$id = (int) $id_or_email;
$user = get_user_by('id', $id);
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)
return $avatar;
// Get the user id
$user_id = $user->ID;
//$user_info = get_userdata($user_id)
// Get the file id
$avatar_url = $user->get('user_url'); //'https://dojo.nearsoft.com/wp-content/uploads/2017/02/Eric-Wroolie-per-template.jpg';
if ($avatar_url == '')
return $avatar;
$avatar = '<img alt="' . $alt . '" src="' . $avatar_url . '" class="avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>';
// Return our new avatar
return $avatar;
I know the function works as the app he built uses the code to generate the avatar for each user. I just don't know how to use it and cannot reach out to him to help me utilise it.
My efforts so far have failed, using code that looks a little like this:
<?php lb_acf_profile_avatar() ?>
<?php if ($avatar != '') : ?>
<div>Hellow World</div>
<?php endif; ?>
Where I have tried calling the function then assuming the returned variable (the avatar image) would be usable from that point. That doesn't appear to be the case.
The error message is 5 of these one for each argument:
Warning: Missing argument 5 for lb_acf_profile_avatar(), called in /home/materialshub/public_html/development/wp-content/themes/bolt/header.php on line 238 and defined in /home/materialshub/public_html/development/wp-content/themes/bolt/functions.php on line 663
Is there a way to tailor this so I get the avatar_url without the img tag returned, but I need the original code to function as it should as it is also used in the app and is functioning correctly.
I don't have access to the app. Or the old developer. Any help you can provide is great. If you want further info just let me know.
I think I want a new function that gets the avatar_url like the function above but without any of the img tag. A simple url is all I need.
I need this to be dynamic as well so it works for all users automatically, generating the avatar_url. How can I pass the arguments in this manor?
I cannot just use the inbuilt get_avatar() WordPress function before we try go down that route as the app has made use of an empty field in the database 'user_url'.
I appreciate this is quite an annoying question, but I appreciate the kindness.
EDIT: I have tried reverting back to the get_avatar() function and that then returns this warning:
Warning: Missing argument 1 for get_avatar(), called in /home/materialshub/public_html/development/wp-content/themes/bolt/header.php on line 239 and defined in /home/materialshub/public_html/development/wp-includes/pluggable.php on line 2450
Thanks, Jason.
php functions
php functions
edited 14 hours ago
Jason Is My Name
asked 15 hours ago
Jason Is My NameJason Is My Name
507
507
1
"I cannot just use the inbuilt get_avatar()" - I think you can? get_avatar just fills in missing arguments then calls the get_avatar filter, and your function is hooked into that filter. So if you call the inbuilt one it should call this code.
– Rup
14 hours ago
How can you tell it links to the basic wordpress function? - I will try to do it now and let you know if it fixes. I originally built it like that but then realised it didnt pull the image through that the user selected in the app. It would always be the basic gravatar avatar.
– Jason Is My Name
14 hours ago
1
Theadd_filter('get_avatar'
line at the top. Here's the built-in get_avatar - you can see that both return statements go throughapply_filters( 'get_avatar' ...)
.
– Rup
14 hours ago
To use this function, you need write something like that:$my_avatar = apply_filter( 'get_avatar', $my_avatar, $user, /* other args */ );
. "apply_filters" on Codex
– nmr
14 hours ago
@Rup - I think youre on to something here - thanks for identifying that. I have tried to call the get_avatar() and now I get the error I posted in my question...
– Jason Is My Name
14 hours ago
|
show 2 more comments
1
"I cannot just use the inbuilt get_avatar()" - I think you can? get_avatar just fills in missing arguments then calls the get_avatar filter, and your function is hooked into that filter. So if you call the inbuilt one it should call this code.
– Rup
14 hours ago
How can you tell it links to the basic wordpress function? - I will try to do it now and let you know if it fixes. I originally built it like that but then realised it didnt pull the image through that the user selected in the app. It would always be the basic gravatar avatar.
– Jason Is My Name
14 hours ago
1
Theadd_filter('get_avatar'
line at the top. Here's the built-in get_avatar - you can see that both return statements go throughapply_filters( 'get_avatar' ...)
.
– Rup
14 hours ago
To use this function, you need write something like that:$my_avatar = apply_filter( 'get_avatar', $my_avatar, $user, /* other args */ );
. "apply_filters" on Codex
– nmr
14 hours ago
@Rup - I think youre on to something here - thanks for identifying that. I have tried to call the get_avatar() and now I get the error I posted in my question...
– Jason Is My Name
14 hours ago
1
1
"I cannot just use the inbuilt get_avatar()" - I think you can? get_avatar just fills in missing arguments then calls the get_avatar filter, and your function is hooked into that filter. So if you call the inbuilt one it should call this code.
– Rup
14 hours ago
"I cannot just use the inbuilt get_avatar()" - I think you can? get_avatar just fills in missing arguments then calls the get_avatar filter, and your function is hooked into that filter. So if you call the inbuilt one it should call this code.
– Rup
14 hours ago
How can you tell it links to the basic wordpress function? - I will try to do it now and let you know if it fixes. I originally built it like that but then realised it didnt pull the image through that the user selected in the app. It would always be the basic gravatar avatar.
– Jason Is My Name
14 hours ago
How can you tell it links to the basic wordpress function? - I will try to do it now and let you know if it fixes. I originally built it like that but then realised it didnt pull the image through that the user selected in the app. It would always be the basic gravatar avatar.
– Jason Is My Name
14 hours ago
1
1
The
add_filter('get_avatar'
line at the top. Here's the built-in get_avatar - you can see that both return statements go through apply_filters( 'get_avatar' ...)
.– Rup
14 hours ago
The
add_filter('get_avatar'
line at the top. Here's the built-in get_avatar - you can see that both return statements go through apply_filters( 'get_avatar' ...)
.– Rup
14 hours ago
To use this function, you need write something like that:
$my_avatar = apply_filter( 'get_avatar', $my_avatar, $user, /* other args */ );
. "apply_filters" on Codex– nmr
14 hours ago
To use this function, you need write something like that:
$my_avatar = apply_filter( 'get_avatar', $my_avatar, $user, /* other args */ );
. "apply_filters" on Codex– nmr
14 hours ago
@Rup - I think youre on to something here - thanks for identifying that. I have tried to call the get_avatar() and now I get the error I posted in my question...
– Jason Is My Name
14 hours ago
@Rup - I think youre on to something here - thanks for identifying that. I have tried to call the get_avatar() and now I get the error I posted in my question...
– Jason Is My Name
14 hours ago
|
show 2 more comments
2 Answers
2
active
oldest
votes
As discussed in comments you actually just want the URL. Here's your code modified to be a get_avatar_url hook instead:
add_filter('get_avatar_url', 'lb_acf_profile_avatar_url', 10, 5);
function lb_acf_profile_avatar_url($url, $id_or_email, $args)
$user = '';
// Get user by id or email
if (is_numeric($id_or_email))
$id = (int) $id_or_email;
$user = get_user_by('id', $id);
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)
return $url;
$avatar_url = $user->get('user_url');
if ($avatar_url == '')
return $url;
return $avatar_url;
Untested, sorry. It uses the same logic as the existing code to resolve the $id_or_email parameter into a user object: there's probably some room for improvement here e.g. since the $id_or_email might already be the user object, and I'm a little nervous about the empty string checks but that's what the existing (presumably working) code does.
You can then call WordPress's get_avatar_url with a user ID and it should return user_url where available. I'd expect get_avatar to still work too with just this filter.
This is brilliant. Thanks so much!
– Jason Is My Name
11 hours ago
add a comment |
You're trying to call this custom function without any parameters:
lb_acf_profile_avatar()
But it's not meant to work like that. It's not a function that you should call. It's a filter callback and it has to get 5 parameters:
- $avatar,
- $id_or_email,
- $size,
- $default,
- $alt
So yes - you will get errors if you won't pass them.
But, as I've already said, this custom function is a filter callback. It means, that whenever get_avatar
function is called, the custom function will be run and it will modify the default behavior of get_avatar
function. (It will change/filter its result)
This means that you should call default get_avatar
function in your header.
You still have to pass some arguments to it. At least one of them:
- $id_or_email
You can use it like so:
echo get_avatar( get_current_user_id() );
And you can pass size
as second parameter.
1
I think you may have hit the nail on the head here pal! Beautifully worded too.
– Jason Is My Name
14 hours ago
We are super close. You're correct in how it works and how to call it. However, I need to return just the url, I currently if echo'ing it out will get the full img tag. Are we able to cut just the url for the avatar out please?
– Jason Is My Name
14 hours ago
This part won't be so easy... The custom function doesn't change the URL and it doesn't return the URL itself. So you would have to write your custom function that will do that.
– Krzysiek Dróżdż
14 hours ago
I can get JS to do this. I would have just liked it to all be handled by php ideally
– Jason Is My Name
14 hours ago
You could modify the existing code to be a get_avatar_data or get_avatar_url filter instead, and call get_avatar_url in PHP. (Just to be clear: this is more work than just changing the add_filter call.) Or just wrap a call to get_avatar_url with a little of your own code: all you're doing is fetching the user object and checking if it has a non-empty user_url property, and if it does using that instead of the default URL.
– Rup
13 hours ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "110"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f332828%2fusing-a-function-written-in-my-functions-php-file-within-the-header-php-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As discussed in comments you actually just want the URL. Here's your code modified to be a get_avatar_url hook instead:
add_filter('get_avatar_url', 'lb_acf_profile_avatar_url', 10, 5);
function lb_acf_profile_avatar_url($url, $id_or_email, $args)
$user = '';
// Get user by id or email
if (is_numeric($id_or_email))
$id = (int) $id_or_email;
$user = get_user_by('id', $id);
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)
return $url;
$avatar_url = $user->get('user_url');
if ($avatar_url == '')
return $url;
return $avatar_url;
Untested, sorry. It uses the same logic as the existing code to resolve the $id_or_email parameter into a user object: there's probably some room for improvement here e.g. since the $id_or_email might already be the user object, and I'm a little nervous about the empty string checks but that's what the existing (presumably working) code does.
You can then call WordPress's get_avatar_url with a user ID and it should return user_url where available. I'd expect get_avatar to still work too with just this filter.
This is brilliant. Thanks so much!
– Jason Is My Name
11 hours ago
add a comment |
As discussed in comments you actually just want the URL. Here's your code modified to be a get_avatar_url hook instead:
add_filter('get_avatar_url', 'lb_acf_profile_avatar_url', 10, 5);
function lb_acf_profile_avatar_url($url, $id_or_email, $args)
$user = '';
// Get user by id or email
if (is_numeric($id_or_email))
$id = (int) $id_or_email;
$user = get_user_by('id', $id);
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)
return $url;
$avatar_url = $user->get('user_url');
if ($avatar_url == '')
return $url;
return $avatar_url;
Untested, sorry. It uses the same logic as the existing code to resolve the $id_or_email parameter into a user object: there's probably some room for improvement here e.g. since the $id_or_email might already be the user object, and I'm a little nervous about the empty string checks but that's what the existing (presumably working) code does.
You can then call WordPress's get_avatar_url with a user ID and it should return user_url where available. I'd expect get_avatar to still work too with just this filter.
This is brilliant. Thanks so much!
– Jason Is My Name
11 hours ago
add a comment |
As discussed in comments you actually just want the URL. Here's your code modified to be a get_avatar_url hook instead:
add_filter('get_avatar_url', 'lb_acf_profile_avatar_url', 10, 5);
function lb_acf_profile_avatar_url($url, $id_or_email, $args)
$user = '';
// Get user by id or email
if (is_numeric($id_or_email))
$id = (int) $id_or_email;
$user = get_user_by('id', $id);
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)
return $url;
$avatar_url = $user->get('user_url');
if ($avatar_url == '')
return $url;
return $avatar_url;
Untested, sorry. It uses the same logic as the existing code to resolve the $id_or_email parameter into a user object: there's probably some room for improvement here e.g. since the $id_or_email might already be the user object, and I'm a little nervous about the empty string checks but that's what the existing (presumably working) code does.
You can then call WordPress's get_avatar_url with a user ID and it should return user_url where available. I'd expect get_avatar to still work too with just this filter.
As discussed in comments you actually just want the URL. Here's your code modified to be a get_avatar_url hook instead:
add_filter('get_avatar_url', 'lb_acf_profile_avatar_url', 10, 5);
function lb_acf_profile_avatar_url($url, $id_or_email, $args)
$user = '';
// Get user by id or email
if (is_numeric($id_or_email))
$id = (int) $id_or_email;
$user = get_user_by('id', $id);
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)
return $url;
$avatar_url = $user->get('user_url');
if ($avatar_url == '')
return $url;
return $avatar_url;
Untested, sorry. It uses the same logic as the existing code to resolve the $id_or_email parameter into a user object: there's probably some room for improvement here e.g. since the $id_or_email might already be the user object, and I'm a little nervous about the empty string checks but that's what the existing (presumably working) code does.
You can then call WordPress's get_avatar_url with a user ID and it should return user_url where available. I'd expect get_avatar to still work too with just this filter.
answered 13 hours ago
RupRup
681614
681614
This is brilliant. Thanks so much!
– Jason Is My Name
11 hours ago
add a comment |
This is brilliant. Thanks so much!
– Jason Is My Name
11 hours ago
This is brilliant. Thanks so much!
– Jason Is My Name
11 hours ago
This is brilliant. Thanks so much!
– Jason Is My Name
11 hours ago
add a comment |
You're trying to call this custom function without any parameters:
lb_acf_profile_avatar()
But it's not meant to work like that. It's not a function that you should call. It's a filter callback and it has to get 5 parameters:
- $avatar,
- $id_or_email,
- $size,
- $default,
- $alt
So yes - you will get errors if you won't pass them.
But, as I've already said, this custom function is a filter callback. It means, that whenever get_avatar
function is called, the custom function will be run and it will modify the default behavior of get_avatar
function. (It will change/filter its result)
This means that you should call default get_avatar
function in your header.
You still have to pass some arguments to it. At least one of them:
- $id_or_email
You can use it like so:
echo get_avatar( get_current_user_id() );
And you can pass size
as second parameter.
1
I think you may have hit the nail on the head here pal! Beautifully worded too.
– Jason Is My Name
14 hours ago
We are super close. You're correct in how it works and how to call it. However, I need to return just the url, I currently if echo'ing it out will get the full img tag. Are we able to cut just the url for the avatar out please?
– Jason Is My Name
14 hours ago
This part won't be so easy... The custom function doesn't change the URL and it doesn't return the URL itself. So you would have to write your custom function that will do that.
– Krzysiek Dróżdż
14 hours ago
I can get JS to do this. I would have just liked it to all be handled by php ideally
– Jason Is My Name
14 hours ago
You could modify the existing code to be a get_avatar_data or get_avatar_url filter instead, and call get_avatar_url in PHP. (Just to be clear: this is more work than just changing the add_filter call.) Or just wrap a call to get_avatar_url with a little of your own code: all you're doing is fetching the user object and checking if it has a non-empty user_url property, and if it does using that instead of the default URL.
– Rup
13 hours ago
add a comment |
You're trying to call this custom function without any parameters:
lb_acf_profile_avatar()
But it's not meant to work like that. It's not a function that you should call. It's a filter callback and it has to get 5 parameters:
- $avatar,
- $id_or_email,
- $size,
- $default,
- $alt
So yes - you will get errors if you won't pass them.
But, as I've already said, this custom function is a filter callback. It means, that whenever get_avatar
function is called, the custom function will be run and it will modify the default behavior of get_avatar
function. (It will change/filter its result)
This means that you should call default get_avatar
function in your header.
You still have to pass some arguments to it. At least one of them:
- $id_or_email
You can use it like so:
echo get_avatar( get_current_user_id() );
And you can pass size
as second parameter.
1
I think you may have hit the nail on the head here pal! Beautifully worded too.
– Jason Is My Name
14 hours ago
We are super close. You're correct in how it works and how to call it. However, I need to return just the url, I currently if echo'ing it out will get the full img tag. Are we able to cut just the url for the avatar out please?
– Jason Is My Name
14 hours ago
This part won't be so easy... The custom function doesn't change the URL and it doesn't return the URL itself. So you would have to write your custom function that will do that.
– Krzysiek Dróżdż
14 hours ago
I can get JS to do this. I would have just liked it to all be handled by php ideally
– Jason Is My Name
14 hours ago
You could modify the existing code to be a get_avatar_data or get_avatar_url filter instead, and call get_avatar_url in PHP. (Just to be clear: this is more work than just changing the add_filter call.) Or just wrap a call to get_avatar_url with a little of your own code: all you're doing is fetching the user object and checking if it has a non-empty user_url property, and if it does using that instead of the default URL.
– Rup
13 hours ago
add a comment |
You're trying to call this custom function without any parameters:
lb_acf_profile_avatar()
But it's not meant to work like that. It's not a function that you should call. It's a filter callback and it has to get 5 parameters:
- $avatar,
- $id_or_email,
- $size,
- $default,
- $alt
So yes - you will get errors if you won't pass them.
But, as I've already said, this custom function is a filter callback. It means, that whenever get_avatar
function is called, the custom function will be run and it will modify the default behavior of get_avatar
function. (It will change/filter its result)
This means that you should call default get_avatar
function in your header.
You still have to pass some arguments to it. At least one of them:
- $id_or_email
You can use it like so:
echo get_avatar( get_current_user_id() );
And you can pass size
as second parameter.
You're trying to call this custom function without any parameters:
lb_acf_profile_avatar()
But it's not meant to work like that. It's not a function that you should call. It's a filter callback and it has to get 5 parameters:
- $avatar,
- $id_or_email,
- $size,
- $default,
- $alt
So yes - you will get errors if you won't pass them.
But, as I've already said, this custom function is a filter callback. It means, that whenever get_avatar
function is called, the custom function will be run and it will modify the default behavior of get_avatar
function. (It will change/filter its result)
This means that you should call default get_avatar
function in your header.
You still have to pass some arguments to it. At least one of them:
- $id_or_email
You can use it like so:
echo get_avatar( get_current_user_id() );
And you can pass size
as second parameter.
answered 14 hours ago
Krzysiek DróżdżKrzysiek Dróżdż
18k73246
18k73246
1
I think you may have hit the nail on the head here pal! Beautifully worded too.
– Jason Is My Name
14 hours ago
We are super close. You're correct in how it works and how to call it. However, I need to return just the url, I currently if echo'ing it out will get the full img tag. Are we able to cut just the url for the avatar out please?
– Jason Is My Name
14 hours ago
This part won't be so easy... The custom function doesn't change the URL and it doesn't return the URL itself. So you would have to write your custom function that will do that.
– Krzysiek Dróżdż
14 hours ago
I can get JS to do this. I would have just liked it to all be handled by php ideally
– Jason Is My Name
14 hours ago
You could modify the existing code to be a get_avatar_data or get_avatar_url filter instead, and call get_avatar_url in PHP. (Just to be clear: this is more work than just changing the add_filter call.) Or just wrap a call to get_avatar_url with a little of your own code: all you're doing is fetching the user object and checking if it has a non-empty user_url property, and if it does using that instead of the default URL.
– Rup
13 hours ago
add a comment |
1
I think you may have hit the nail on the head here pal! Beautifully worded too.
– Jason Is My Name
14 hours ago
We are super close. You're correct in how it works and how to call it. However, I need to return just the url, I currently if echo'ing it out will get the full img tag. Are we able to cut just the url for the avatar out please?
– Jason Is My Name
14 hours ago
This part won't be so easy... The custom function doesn't change the URL and it doesn't return the URL itself. So you would have to write your custom function that will do that.
– Krzysiek Dróżdż
14 hours ago
I can get JS to do this. I would have just liked it to all be handled by php ideally
– Jason Is My Name
14 hours ago
You could modify the existing code to be a get_avatar_data or get_avatar_url filter instead, and call get_avatar_url in PHP. (Just to be clear: this is more work than just changing the add_filter call.) Or just wrap a call to get_avatar_url with a little of your own code: all you're doing is fetching the user object and checking if it has a non-empty user_url property, and if it does using that instead of the default URL.
– Rup
13 hours ago
1
1
I think you may have hit the nail on the head here pal! Beautifully worded too.
– Jason Is My Name
14 hours ago
I think you may have hit the nail on the head here pal! Beautifully worded too.
– Jason Is My Name
14 hours ago
We are super close. You're correct in how it works and how to call it. However, I need to return just the url, I currently if echo'ing it out will get the full img tag. Are we able to cut just the url for the avatar out please?
– Jason Is My Name
14 hours ago
We are super close. You're correct in how it works and how to call it. However, I need to return just the url, I currently if echo'ing it out will get the full img tag. Are we able to cut just the url for the avatar out please?
– Jason Is My Name
14 hours ago
This part won't be so easy... The custom function doesn't change the URL and it doesn't return the URL itself. So you would have to write your custom function that will do that.
– Krzysiek Dróżdż
14 hours ago
This part won't be so easy... The custom function doesn't change the URL and it doesn't return the URL itself. So you would have to write your custom function that will do that.
– Krzysiek Dróżdż
14 hours ago
I can get JS to do this. I would have just liked it to all be handled by php ideally
– Jason Is My Name
14 hours ago
I can get JS to do this. I would have just liked it to all be handled by php ideally
– Jason Is My Name
14 hours ago
You could modify the existing code to be a get_avatar_data or get_avatar_url filter instead, and call get_avatar_url in PHP. (Just to be clear: this is more work than just changing the add_filter call.) Or just wrap a call to get_avatar_url with a little of your own code: all you're doing is fetching the user object and checking if it has a non-empty user_url property, and if it does using that instead of the default URL.
– Rup
13 hours ago
You could modify the existing code to be a get_avatar_data or get_avatar_url filter instead, and call get_avatar_url in PHP. (Just to be clear: this is more work than just changing the add_filter call.) Or just wrap a call to get_avatar_url with a little of your own code: all you're doing is fetching the user object and checking if it has a non-empty user_url property, and if it does using that instead of the default URL.
– Rup
13 hours ago
add a comment |
Thanks for contributing an answer to WordPress Development Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f332828%2fusing-a-function-written-in-my-functions-php-file-within-the-header-php-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
"I cannot just use the inbuilt get_avatar()" - I think you can? get_avatar just fills in missing arguments then calls the get_avatar filter, and your function is hooked into that filter. So if you call the inbuilt one it should call this code.
– Rup
14 hours ago
How can you tell it links to the basic wordpress function? - I will try to do it now and let you know if it fixes. I originally built it like that but then realised it didnt pull the image through that the user selected in the app. It would always be the basic gravatar avatar.
– Jason Is My Name
14 hours ago
1
The
add_filter('get_avatar'
line at the top. Here's the built-in get_avatar - you can see that both return statements go throughapply_filters( 'get_avatar' ...)
.– Rup
14 hours ago
To use this function, you need write something like that:
$my_avatar = apply_filter( 'get_avatar', $my_avatar, $user, /* other args */ );
. "apply_filters" on Codex– nmr
14 hours ago
@Rup - I think youre on to something here - thanks for identifying that. I have tried to call the get_avatar() and now I get the error I posted in my question...
– Jason Is My Name
14 hours ago