0

I am using a form field to let a user edit a post and upload a profile image.

Once they upload the profile image a URL is passed to a hidden custom text field called 'logo_header'. I read this URL in the PHP template and use it to display the image as follows.

<?php $logo = get_post_meta($post->ID, 'logo_header', true); ?>

<div class="profileLogo"><img src="<?php echo $logo; ?>" /></div>

My Problem is when a user decides to update their photo (basically change the photo) the new URL is just added to the end of the current one, I guess this creates an array. so it would be like

http://example.com/myprofile.jpg, http://example.com/myprofile2.jpg

Obviously the image src cannot use an array, it needs 1 URL.

I use gravity forms upload file button to edit the post and upload the image. There is no option to delete the current image that already exists (Thats another problem :( ) so the user is only given an upload button.

My question is

How can I always choose the last URL added to the 'logo_header' field?

3
  • what you get in $logo ? array ? Commented Oct 15, 2012 at 16:28
  • good question, yes I get 'array' returned Commented Oct 15, 2012 at 16:31
  • 1
    if(is_scalar($post->ID)) { $logo=$post->ID; } if(is_array($post->ID)) { $logo=array_pop($post->ID); } Commented Oct 15, 2012 at 16:33

3 Answers 3

2

Try $logo = array_pop(get_post_meta($post->ID, 'logo_header', false));

With the false parameter, you'll get an array of all the meta fields. We can only assume that they'll be inserted and returned in the correct order, so array_pop() will pull the last one in the list.

Edit - for cases where you don't always have multiple logos, you should use this:

$logo = get_post_meta($post->ID, 'logo_header', false);
$logo = is_array($logo) ? array_pop($logo) : $logo;
Sign up to request clarification or add additional context in comments.

1 Comment

Actually I spoke too soon on this. This solution did not allow for the fact that there might only be 1 url at the start and not be an array so the field will equal..www.example.com then when I add an image your code works, but when I only have 1 image it does not. Could u update it for that scenario?
1
$logo = get_post_meta($post->ID, 'logo_header', true);

$logoImgSrc = is_array($logo) ? end($logo) : $logo;

<div class="profileLogo">
    <img src="<?php echo $logoImgSrc ; ?>" />
</div>

Comments

0

I think you shouldn't be using a hidden text field, rather a hidden input field, when the user selects the new image (the files change() function, in jQuery) set the hidden field value to the new url, replacing the old, then post the hidden field name.

Edit to possibly help:

$("#browse_box").change(function() {
    $("#hidden_field").val($(this).val());
});

Typing from memory, but that would be the basic idea.

1 Comment

Sorry I meant hidden input 'text field' - I hide it as the user would have no clue what to do with it as it is just a URL that is put in there so I can grab it. If you think I can replace that current value with what you suggest that would be awesome, but I don't know how to do what you suggest, any other advice on that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.