Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I followed this tutorial in order too add a custom upload button in the Wordpress admin.

It displays the picture you just uploaded right after saving:

enter image description here

The problem is that I can't manage to display the picture in the front end, in a template file.

This is the section that displays the picture:

   function ud_section_text() {
        $options = get_option('ud_options');
        echo '<p>Upload your file here:</p>';
        if ($file = $options['file']) {
            // var_dump($file);
            echo "<img src='{$file['url']}' />";
        }
    }

So I tried this:

<?php var_dump($file['url']); ?> 

but it returns NULL.

Any suggestions?

code:

<?php
/*
Plugin Name: Upload Demo
Description: Demonstrate a plugin that lets you upload an image
Author: Otto
Author URI: http://ottodestruct.com
License: GPL2

    Copyright 2010  Samuel Wood  (email : otto@ottodestruct.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2,
    as published by the Free Software Foundation.

    You may NOT assume that you can use any other version of the GPL.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    The license for this software can likely be found here:
    http://www.gnu.org/licenses/gpl-2.0.html

*/

// add the admin page and such
add_action('admin_init', 'ud_admin_init');
function ud_admin_init() {
    register_setting( 'ud_options', 'ud_options', 'ud_options_validate' );
    add_settings_section('ud_main', 'Main Section', 'ud_section_text', 'ud');
    add_settings_field('ud_filename', 'File:', 'ud_setting_filename', 'ud', 'ud_main');
}

// add the admin options page
add_action('admin_menu', 'ud_admin_add_page');
function ud_admin_add_page() {
    $mypage = add_options_page('Upload Demo', 'Upload Demo', 'manage_options', 'ud', 'ud_options_page');
}

// display the admin options page
function ud_options_page() {
?>
    <div class="wrap">
    <h2>Upload Demo</h2>
    <p>You can upload a file. It'll go in the uploads directory.</p>
    <form method="post" enctype="multipart/form-data" action="options.php">
    <?php settings_fields('ud_options'); ?>
    <?php do_settings_sections('ud'); ?>
    <p class="submit">
    <input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
    </p>
    </form>

    </div>

<?php
}

function ud_section_text() {
    $options = get_option('ud_options');
    echo '<p>Upload your file here:</p>';
    if ($file = $options['file']) {
        // var_dump($file);
        echo "<img src='{$file['url']}' />";
    }
}

function ud_setting_filename() {
    echo '<input type="file" name="ud_filename" size="40" />';
}

function ud_options_validate($input) {
    $newinput = array();
    if ($_FILES['ud_filename']) {
        $overrides = array('test_form' => false);
        $file = wp_handle_upload($_FILES['ud_filename'], $overrides);
        $newinput['file'] = $file;
    }
    return $newinput;
}
share|improve this question
    
How about var_dump($options); - and pls start accepting answers on your hundreds of Questions. –  kaiser May 9 '11 at 17:07
    
@kaiser OK I accepted some answers. But is that the reason I got down voted? –  janoChen May 9 '11 at 18:28
1  
no, the reason i downvoted was that you didn't a) try to debug vars other than $file b) dropped in a plugin from @Otto with no explanations how, where, et.c. you are dealing with it in your admin UI script (copying the plugin from the source makes no sense when you're already linking there). c) You're breaking your tasks into lot's of similar Qs where some of them could be seen as dublicate. –  kaiser May 9 '11 at 18:47
1  
Furthermore: Read this: wordpress.stackexchange.com/questions/15319/… and this: wordpress.stackexchange.com/questions/13063/… which will avoid a lot of Qs. And you should really take the time and upvote and accept ALL of your answers, not only some, because i'm pointing that out. It's a form of respect that honors other peoples efforts to help you. You still got dozens of Qs that have a satisfying answer that should be a) the solution and b) get an upvote. –  kaiser May 9 '11 at 18:49

1 Answer 1

up vote 1 down vote accepted

simple answer to this, if you wan to display the image use:

 $options = get_option('ud_options');
 if (isset($options['file'])) {
    echo "<img src='{$options['file']['url']}' />";
 }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.