{"id":2429,"date":"2019-11-27T15:57:23","date_gmt":"2019-11-27T15:57:23","guid":{"rendered":"https:\/\/ibex.tech\/wordpress-sites\/?p=2429"},"modified":"2022-07-14T12:36:58","modified_gmt":"2022-07-14T11:36:58","slug":"member-avatar","status":"publish","type":"post","link":"https:\/\/ibex.tech\/wordpress-sites\/plugins\/buddypress\/images-and-avatars\/member-avatar","title":{"rendered":"Member Avatar"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Replace default member avatar<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>function myavatar_add_default_avatar( $url )\r\n{\r\n  return get_stylesheet_directory_uri() . '\/images\/my_image.png';\r\n}\r\nadd_filter( 'bp_core_mysteryman_src', 'myavatar_add_default_avatar' );<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Replace buddyypress \/ wordpress avatar<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/****************************************************\n\/\/****************************************************\n\/\/********** REPLACE DEFAULT WP USER AVATAR **********\n\/\/****************************************************\n\/\/****************************************************\nadd_filter( 'bp_core_fetch_avatar', 'my_bp_core_fetch_avatar', 10, 2 );\nfunction my_bp_core_fetch_avatar ($avatar_img_tag, $params)\n{\n\n  if (\n    (!empty($params)) &amp;&amp;\n    ( ($params&#91;'object'] == 'user') || $params&#91;'object'] == '') )\n  {\n    \/\/----- CALL IS FOR A USERS AVATAR -----\n    $ImgWidth = intval($params&#91;'width']);\n    $ImgHeight = intval($params&#91;'height']);\n   \n        \n    $user_id = $params&#91;'item_id'];\n    if (is_int($user_id))   \/\/Ensure user ID is set\n    {\n   \n      $ImageUrl = \"ADD IMAGE URL HERE\";\n\n      \n      if (strlen($ImageUrl ) &gt; 0)\n      {\n          \/\/WE HAVE AN IMAGE FOR THIS USER TO OVERRIDE THE DEFAULT IMAGE WITH\n          \n          \/\/If there is a width and height specified then override styling to use object-fit: cover (fill area and crop image if needed)\n          if ( ($ImgWidth &gt; 0) &amp;&amp; ($ImgHeight &gt; 0) )\n          {\n            \/\/Override the string with our styled img tag so image stays in correct proportion\n            $ImgStyle = 'style=\"';\n            $ImgStyle .= 'object-fit: cover;';\n            $ImgStyle .= 'width: ' . $ImgWidth . 'px;';\n            $ImgStyle .= 'height: ' . $ImgHeight . 'px;';\n            $ImgStyle .= '\" ';\n\n            $avatar_img_tag = preg_replace( '\/(&lt;img src=\")(.+)(\" class.+)\/', \"$1$ImageUrl$3\", $avatar_img_tag );      \/\/Replace the url\n            $avatar_img_tag = str_replace(\"\/&gt;\", \"&gt;\", $avatar_img_tag);                \/\/Ensure it doesn't end with \/&gt;\n            $avatar_img_tag = str_replace(\"&gt;\", \" $ImgStyle &gt;\", $avatar_img_tag);      \/\/Add our special style to the end\n          }\n          else\n          {\n            \/\/Use the original string and just replace the url\n            $avatar_img_tag = preg_replace( '\/(&lt;img src=\")(.+)(\" class.+)\/', \"$1$ImageUrl$3\", $avatar_img_tag );      \/\/Replace the url\n          }\n         \n      }\n    }\n\n  }\n\n  return $avatar_img_tag;\n}\n\n\/* THERE IS ALSO THIS FUNCTION IN CASE EVER NEEDED:\nadd_filter( 'bp_core_fetch_avatar_url', 'my_bp_core_fetch_avatar_url', 10, 2 );\nfunction my_bp_core_fetch_avatar_url ( $avatar_url, $params )\n{\n\n\treturn $avatar_url;\n}\n*\/<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Replace default member avatar Replace buddyypress \/ wordpress avatar<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[367],"tags":[],"class_list":["post-2429","post","type-post","status-publish","format-standard","hentry","category-images-and-avatars"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/posts\/2429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/comments?post=2429"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/posts\/2429\/revisions"}],"predecessor-version":[{"id":4423,"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/posts\/2429\/revisions\/4423"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/media?parent=2429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/categories?post=2429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/tags?post=2429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}