Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Problem With Logo Upload WordPress

  • SOLVED

I have a custom theme options page. I've created some code to upload a logo. Here's my problem. The first time I upload and the logo, it doesn't take. If I upload again, the logo gets uploaded to /uploads and everything is fine.

I need to get this to work on the first shot. Please help.

Here's my code....


//Logo Upload
add_action('admin_init', 'lg_admin_init');
function lg_admin_init() {
register_setting( 'lg_options', 'lg_options', 'lg_options_validate');
add_settings_section('lg_main', 'Main Settings', 'lg_section_text', 'lg');
add_settings_field('lg_filename', 'File:', 'lg_setting_filename', 'lg', 'lg_main');
}

// Logo Page
function adminlogo_page() {
?>
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/wtf/styles/style.css" type="text/css" media="screen" />
<form method="post" action="options.php" enctype="multipart/form-data">
<div class="wrap">
<?php if($saved){ ?><div class="updated fade" id="message"><p><strong>Settings saved.</strong></p></div><?php } ?>
<div style="clear:both;height:20px;"></div>

<div class="section-title">
<img alt="icon" src="<?php bloginfo('template_url'); ?>/wtf/images/icon32.png" style="float: left"/><h3>Upload Your Logo</h3>
<div class="clear"></div>
</div>
<div class="section">
<div class="option">
<label for="tracking_code">Logo Upload</label>
<div id="cclabels">
<?php settings_fields('lg_options'); ?>
<?php do_settings_sections('lg'); ?>
<p class="submit"><input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Click Here To Upload Logo'); ?>" />
<input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Click Here For NO Logo'); ?>" /></p>
</form>
</div>
<div class="clear"></div>
</div>
</div>
</div>

<?php }

function lg_section_text() {
$options = get_option('lg_options');
echo '<p>Upload your file here: (Recommended Logo Height Is 80px)<br/> <br/><strong>IMPORTANT NOTE:</strong> Some hosts will not take the logo on the first try. The fix is simple, just upload it again and it will appear below.</p>';
if (($file = $options['file']) && (($file['url']) =="")) {
echo "";}
else {
if ($file = $options['file']) {
echo "<img src='{$file['url']}' />";}}
}


function lg_setting_filename() {
echo '<input type="file" name="lg_filename" size="40" />';
}
function lg_options_validate($input) {
$newinput = array();
if ($_FILES['lg_filename']) {
$overrides = array('test_form' => false);
$file = wp_handle_upload($_FILES['lg_filename'], $overrides);
$newinput['file'] = $file;
}
return $newinput;
}



As you can see, I added a line telling people to upload again. That's just a quick work around. I need to get this fixed asap. Thank you.

I'm using the code from this page...

http://www.wptavern.com/forum/themes-templates/1346-creating-upload-function-options-page.html

As you can see on the 3rd page of comments, someone has the same issue, but there was no fix posted.

Look at the second comment from the last on page 3.

Answers (6)

2010-12-16

rilwis answers:

This problem is really strange. I'm not sure that is caused by WordPress or PHP. But when I digged into the code of upload file in wp_handle_upload function (wp-admin/includes/file.php), I found the error "Specified file failed upload test." at line 305 which was shown when first upload (if you use var_dump, you'll see). This code looks like:

if ( $test_upload && ! @ is_uploaded_file( $file['tmp_name'] ) )
return call_user_func($upload_error_handler, $file, __( 'Specified file failed upload test.' ));


The problem here is is_uploaded_file() returns false ($test_upload is set true by default)! That means the uploaded file is treated not as an valid uploaded file. That's strange. I'm wondering whether or not WP handles form upload in a strange way (transferring uploaded variables through pages, for ex.).

Digging a bit further, I found that upload doesn't succeed only when the option is not set. I tried to set an empty option when initializing, and it works (this doesn't hurt your code at all, but you can make it better by setting predefined option).!

So I think that may help you at the present: adding an empty option when activating theme, like this:

add_action('after_setup_theme', 'rw_add_empty_option');

function rw_add_empty_option() {
$options = array();
add_option('lg_options', $options, '', 'no');
}


If you use the code as a plugin, you might want to add this:

register_activation_hook(__FILE__, 'rw_add_empty_option');

function rw_add_empty_option() {
$options = array();
add_option('lg_options', $options, '', 'no');
}


Armand Morin comments:

rilwis,

That worked PERFECTLY. Thank you.

Your assessment was right on the money.

2010-12-16

urbanoid answers:

Hello,

I think you should use PHP's move_uploaded_file() function instead of wp_handle_upload:

//Create directory first where you want to upload
@mkdir('../wp-content/uploads');
@mkdir('../wp-content/uploads/admin_uploads');
//check if file is added and place it
if ($_FILES['lg_filename']['name'] != '') {
if(!move_uploaded_file($_FILES['lg_filename']['tmp_name'], '../wp-content/uploads/admin_uploads/' . $_FILES['lg_filename']['name']))
{
print 'error'!;
}
else
{
print 'success';
}
}


Armand Morin comments:

urbanoid,

Thanks, but I'd like to try to make the above code work. It actually does. It just doesn't do it on the FIRST upload.

When you try everytime after the first try, it works.


urbanoid comments:

Replace these lines:

function lg_options_validate($input) {

$newinput = array();

if ($_FILES['lg_filename']) {

$overrides = array('test_form' => false);

$file = wp_handle_upload($_FILES['lg_filename'], $overrides);

$newinput['file'] = $file;

}

return $newinput;

}


With these:

function lg_options_validate() {
@mkdir('../wp-content/uploads');
@mkdir('../wp-content/uploads/admin_uploads');
$newinput = array();

if ($_FILES['lg_filename']['name'] != '') {
if(move_uploaded_file($_FILES['lg_filename']['tmp_name'], '../wp-content/uploads/admin_uploads/' . $_FILES['lg_filename']['name']))
{
$newinput['file'] = $_FILES['lg_filename']['name'];
return $newinput;
}
}
}


...and it will work and get the same result.


Armand Morin comments:

I don't understand why I would want to place it in /uploads then move it to /uploads/admin_uploads

Also, how would I call back the image?

My image is now broken in the preview.


urbanoid comments:

Let me explain:

mkdir creates folders so with these 2 lines: You create a directory into wp-content/uploads/ folder for the upload:
@mkdir('../wp-content/uploads');
@mkdir('../wp-content/uploads/admin_uploads');


After that it places the uploaded files into this folder: ../wp-content/uploads/admin_uploads

Finally you can reach your correct file name this way from admin panel:

<img src="../wp-content/uploads/admin_uploads/<?php print $newinput['file']; ?>" />


urbanoid comments:

I think I got this! Maybe the logo is uploaded for the first time but you can't see it beacause you don't redirect the page after upload.

Try to upload the logo and go to this uploading page again. Does it show up?

If yes it means after you upload the logo, you should use this code to redirect on success:

header("Location: YOUR_URL");

Replace YOUR_URL with the exact link of your admin page url where upload url is!


Armand Morin comments:

The logo is NOT uploaded on the first try, I checked it.

I did var_dump to show the array.

2010-12-16

Chris Lee answers:

Have you tried the custom logo plugin: [[LINK href="http://wordpress.org/extend/plugins/custom-logo/"]]http://wordpress.org/extend/plugins/custom-logo/[[/LINK]]

Have you looked at twentyten's Header upload function. It gives you some insight on how to accomplish this:

http://xref.yoast.com/trunk/nav.html?wp-content/themes/twentyten/functions.php.source.html

2010-12-16

Pippin Williamson answers:

Not entirely sure about this, but give it a try.

Replace:


if ($_FILES['lg_filename']) {

$overrides = array('test_form' => false);

$file = wp_handle_upload($_FILES['lg_filename'], $overrides);

$newinput['file'] = $file;
}


With this:


if ($_FILES['lg_filename']) {

$overrides = array('test_form' => false);

$file = wp_handle_upload($_FILES['lg_filename'], $overrides);

$newinput = $file['file'];
}


Armand Morin comments:

Tried that it blew an error


Pippin Williamson comments:

What was the error?


Armand Morin comments:

The image is labeled as "C" and that's it.


Pippin Williamson comments:

"C" gets inputted into the into field?


Armand Morin comments:

YES just "C"

2010-12-16

Sébastien | French WordpressDesigner answers:

The first time the image is not displayed but the image is in the uploads file. So, no problem with your function of upload

But there is a problem with your function which display image

replace

function lg_section_text() {

$options = get_option('lg_options');

echo '<p>Upload your file here: (Recommended Logo Height Is 80px)<br/> <br/>IMPORTANT NOTE: Some hosts will not take the logo on the first try. The fix is simple, just upload it again and it will appear below.</p>';

if (($file = $options['file']) && (($file['url']) =="")) {

echo "";}

else {

if ($file = $options['file']) {

echo "<img src='{$file['url']}' />";}}

}


by

function lg_section_text() {

$options = get_option('lg_options');

echo '<p>Upload your file here: (Recommended Logo Height Is 80px)<br/> <br/>IMPORTANT NOTE: Some hosts will not take the logo on the first try. The fix is simple, just upload it again and it will appear below.</p>';



if ($file = $options['file']) {
if(file_exists($file['file'])) { echo "<img src='{$file['url']}' />";}

}


}



----------------------

Il n'y a pas de probleme avec la fonction d'upload puisque dès le 1er upload (quand l'image n'est pas affichée) on constate que l'image est bel et bien dans le ftp (dossier uploads)
le probleme vient de la fonction qui affiche l'image.


Armand Morin comments:

I tried the solution. You're right, the image did upload. Although upon upload, it still did not display even with your code. I refreshed the page still nothing. it wasn't until I uploaded it again that I could see the image.

very strange.


Sébastien | French WordpressDesigner comments:

So, you have your solution, i'm happy for you :-)


Armand Morin comments:

NO, the image apparently ALWAYS LOADED.

I still have to UPLOAD IT TWICE which was my original problem.

So I still have the same problem I had before.

I have to upload the image TWICE before it is displayed.


Sébastien | French WordpressDesigner comments:

Doh!

A question : if you upload the logo, the first time you must upload the image twice.
After, it's ok.
And after, what do you do to be in the same situation of the first time ?

2010-12-16

idt answers:


if (($file = $options['file']) && (($file['url']) ==""))


isn't it suppose to be

if (($file == $options['file']) && (($file['url']) ==""))


idt comments:

and this too:

if ($file = $options['file'])


if ($file == $options['file'])


idt comments:

I think the reason is the if and else I have. Since you're not using the correct comparison operator so that might have caused the issue. Please try changing the "=" to "==". So please try changing this:

function lg_section_text() {
$options = get_option('lg_options');
echo '<p>Upload your file here: (Recommended Logo Height Is 80px)<br/> <br/>IMPORTANT NOTE: Some hosts will not take the logo on the first try. The fix is simple, just upload it again and it will appear below.</p>';
if (($file = $options['file']) && (($file['url']) =="")) {
echo "";}
else {
if ($file = $options['file']) {
echo "<img src='{$file['url']}' />";}}
}

to this

function lg_section_text() {
$options = get_option('lg_options');
echo '<p>Upload your file here: (Recommended Logo Height Is 80px)<br/> <br/>IMPORTANT NOTE: Some hosts will not take the logo on the first try. The fix is simple, just upload it again and it will appear below.</p>';
if (($file == $options['file']) && (($file['url']) =="")) {
echo "";}
else {
if ($file == $options['file']) {
echo "<img src='{$file['url']}' />";}}
}