Forums

[Plugin User Photo] Small PHP issue (4 posts)

  1. ThreeVisual
    Member
    Posted 3 years ago #

    I'm trying to integrate the user image plugin with a wordpress site, i've added the code (see below) but it's not doing what I want it to do, any ideas how I can fix this?

    style="background:url(<?php userphoto_the_author_photo() ?>)">

    Rather than returning the image it's returning the following:

    )">

    Cheers.

  2. ThreeVisual
    Member
    Posted 3 years ago #

    Still needing some help here, anyone have any ideas? Thank you.

  3. Mark
    Member
    Posted 3 years ago #

    That won't work because the function returns a complete set of HTML and all you want is a URL path to the image source file itself.

    After having a quick look at the plugin code it appears that you'll have to get someone to modify the plugin for you so that it has a function to return just the image source.

    Something like this:

    function userphoto__get_userphoto_url($user_id, $photoSize, $before, $after, $attributes, $default_src){
    	global $userphoto_prevent_override_avatar;
    	//Note: when we move to a global default user photo, we can always enter into the following conditional
    	if($user_id && ($userdata = get_userdata($user_id))){
    		if(($userdata->userphoto_approvalstatus == USERPHOTO_APPROVED) &&
    		    $image_file = ($photoSize == USERPHOTO_FULL_SIZE ? $userdata->userphoto_image_file : $userdata->userphoto_thumb_file))
    		{
    			$width = $photoSize == USERPHOTO_FULL_SIZE ? $userdata->userphoto_image_width : $userdata->userphoto_thumb_width;
    			$height = $photoSize == USERPHOTO_FULL_SIZE ? $userdata->userphoto_image_height : $userdata->userphoto_thumb_height;
    
    			$upload_dir = wp_upload_dir();
    			if(!empty($upload_dir['error']))
    				return "Error: " . $upload_dir['error'];
    			$src = trailingslashit($upload_dir['baseurl']) . 'userphoto/' . $image_file;
    		}
    		else if($default_src){
    			$src = $default_src;
    			$width = $height = 0;
    		}
    		else if(get_option('userphoto_use_avatar_fallback') && !$userphoto_prevent_override_avatar){
    			$width = $height = get_option($photoSize == USERPHOTO_FULL_SIZE ? 'userphoto_maximum_dimension' : 'userphoto_thumb_dimension');
    			global $userphoto_using_avatar_fallback;
    			$userphoto_using_avatar_fallback = true;
    			$img = get_avatar($user_id, $width);
    			$userphoto_using_avatar_fallback = false;
    			if(!$img)
    				return;
    			if(!preg_match('{src=([\'"])(.+?)\1}', $img, $matches))
    				return;
    			$src = str_replace('&amp;', '&', $matches[2]);
    			if(preg_match('{class=([\'"])(.+?)\1}', $img, $matches))
    				$attributes['class'] .= ' ' . $matches[2];
    		}
    		else return '';
    	return $src;
    	}
    }
    
    function userphoto_the_author_photo_url($before = '', $after = '', $attributes = array(), $default_src = ''){
    	global $authordata, $curauthor;
    	if(!empty($authordata) && $authordata->ID)
    		echo userphoto__get_userphoto_url($authordata->ID, USERPHOTO_FULL_SIZE, $before, $after, $attributes, $default_src);
    }

    Then use userphoto_the_author_photo_url() instead of userphoto_the_author_photo()

    I'm not 100% sure that code will work but that's basically what needs to be added to the plugin. I didn't test it though.

  4. ThreeVisual
    Member
    Posted 3 years ago #

    Brilliant, thanks for your help... worked perfectly, now I have to try and figure out how to edit that so it does the same for comment avatars.

Topic Closed

This topic has been closed to new replies.

About this Topic