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

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

Why am I allowed to create multiple unique pointers from a single object?

What exact does MIB represent in SNMP? How is it different from OID?

What can we do to stop prior company from asking us questions?

Non-deterministic sum of floats

Interfacing a button to MCU (and PC) with 50m long cable

How to avoid supervisors with prejudiced views?

What is ( CFMCC ) on ILS approach chart?

Anatomically Correct Strange Women In Ponds Distributing Swords

Preparing Indesign booklet with .psd graphics for print

Has this building technique been used in an official set?

Can we say or write : "No, it'sn't"?

Is there a difference between "Fahrstuhl" and "Aufzug"

How to solve a differential equation with a term to a power?

What happens if you roll doubles 3 times then land on "Go to jail?"

Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?

Parametric curve length - calculus

Is there a way to save my career from absolute disaster?

Inappropriate reference requests from Journal reviewers

Contours of a clandestine nature

How to count occurrences of text in a file?

Is micro rebar a better way to reinforce concrete than rebar?

Is it professional to write unrelated content in an almost-empty email?

I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin



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










1















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.










share|improve this question



















  • 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
    yesterday











  • 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
    yesterday






  • 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
    yesterday











  • 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
    yesterday












  • @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
    yesterday















1















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.










share|improve this question



















  • 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
    yesterday











  • 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
    yesterday






  • 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
    yesterday











  • 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
    yesterday












  • @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
    yesterday













1












1








1


1






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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday







Jason Is My Name

















asked yesterday









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
    yesterday











  • 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
    yesterday






  • 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
    yesterday











  • 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
    yesterday












  • @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
    yesterday












  • 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
    yesterday











  • 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
    yesterday






  • 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
    yesterday











  • 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
    yesterday












  • @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
    yesterday







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
yesterday





"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
yesterday













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
yesterday





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
yesterday




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
yesterday





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
yesterday













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
yesterday






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
yesterday














@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
yesterday





@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
yesterday










2 Answers
2






active

oldest

votes


















1














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.






share|improve this answer























  • This is brilliant. Thanks so much!

    – Jason Is My Name
    yesterday


















2














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.






share|improve this answer


















  • 1





    I think you may have hit the nail on the head here pal! Beautifully worded too.

    – Jason Is My Name
    yesterday











  • 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
    yesterday











  • 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ż
    yesterday











  • I can get JS to do this. I would have just liked it to all be handled by php ideally

    – Jason Is My Name
    yesterday












  • 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
    yesterday












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



);













draft saved

draft discarded


















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









1














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.






share|improve this answer























  • This is brilliant. Thanks so much!

    – Jason Is My Name
    yesterday















1














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.






share|improve this answer























  • This is brilliant. Thanks so much!

    – Jason Is My Name
    yesterday













1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









RupRup

681614




681614












  • This is brilliant. Thanks so much!

    – Jason Is My Name
    yesterday

















  • This is brilliant. Thanks so much!

    – Jason Is My Name
    yesterday
















This is brilliant. Thanks so much!

– Jason Is My Name
yesterday





This is brilliant. Thanks so much!

– Jason Is My Name
yesterday













2














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.






share|improve this answer


















  • 1





    I think you may have hit the nail on the head here pal! Beautifully worded too.

    – Jason Is My Name
    yesterday











  • 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
    yesterday











  • 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ż
    yesterday











  • I can get JS to do this. I would have just liked it to all be handled by php ideally

    – Jason Is My Name
    yesterday












  • 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
    yesterday
















2














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.






share|improve this answer


















  • 1





    I think you may have hit the nail on the head here pal! Beautifully worded too.

    – Jason Is My Name
    yesterday











  • 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
    yesterday











  • 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ż
    yesterday











  • I can get JS to do this. I would have just liked it to all be handled by php ideally

    – Jason Is My Name
    yesterday












  • 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
    yesterday














2












2








2







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









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
    yesterday











  • 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
    yesterday











  • 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ż
    yesterday











  • I can get JS to do this. I would have just liked it to all be handled by php ideally

    – Jason Is My Name
    yesterday












  • 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
    yesterday













  • 1





    I think you may have hit the nail on the head here pal! Beautifully worded too.

    – Jason Is My Name
    yesterday











  • 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
    yesterday











  • 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ż
    yesterday











  • I can get JS to do this. I would have just liked it to all be handled by php ideally

    – Jason Is My Name
    yesterday












  • 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
    yesterday








1




1





I think you may have hit the nail on the head here pal! Beautifully worded too.

– Jason Is My Name
yesterday





I think you may have hit the nail on the head here pal! Beautifully worded too.

– Jason Is My Name
yesterday













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
yesterday





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
yesterday













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ż
yesterday





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ż
yesterday













I can get JS to do this. I would have just liked it to all be handled by php ideally

– Jason Is My Name
yesterday






I can get JS to do this. I would have just liked it to all be handled by php ideally

– Jason Is My Name
yesterday














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
yesterday






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
yesterday


















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

រឿង រ៉ូមេអូ និង ហ្ស៊ុយលីយេ សង្ខេបរឿង តួអង្គ បញ្ជីណែនាំ

QGIS export composer to PDF scale the map [closed] Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Print Composer QGIS 2.6, how to export image?QGIS 2.8.1 print composer won't export all OpenCycleMap base layer tilesSave Print/Map QGIS composer view as PNG/PDF using Python (without changing anything in visible layout)?Export QGIS Print Composer PDF with searchable text labelsQGIS Print Composer does not change from landscape to portrait orientation?How can I avoid map size and scale changes in print composer?Fuzzy PDF export in QGIS running on macSierra OSExport the legend into its 100% size using Print ComposerScale-dependent rendering in QGIS PDF output

PDF-ში გადმოწერა სანავიგაციო მენიუproject page