Forums

image upload in custom options page $_FILES empty (6 posts)

  1. iansane
    Member
    Posted 4 months ago #

    Hi, I'm trying to create a theme options page by tutsplus 'wordpress plugin development essentials' course.

    So far all I have is two fields (banner heading, and logo upload).

    Everything works fine till I add the upload field and then print_r($_FILES) is empty.

    Here's the code below. regular wp image upload works fine and other php upload scripts on the same server work fine. So can anyone see any mistakes in my code that might be causing the upload to fail?

    <?php
    /*
    Plugin Name: JW Options
    Plugin URI: http://nettuts.com
    Description: Options page for a theme.
    Version: 1.0
    Author: Ian Simmons
    Author URI: http://net.tutsplus.com
    */
    class JW_Options{
    
    	public $options;
    
    	public function __construct()
    	{
    		$this->options = get_option('jw_plugin_options');
    		$this->register_settings_and_fields();
    	}
    
    	public function add_menu_page()
    	{
    		add_options_page('Theme Options', 'Theme Options', 'administrator', __FILE__, array(
    			'JW_Options', 'display_options_page'
    			)
    		);
    	}
    
    	public function display_options_page()
    	{
    		?>
    
    		<div class="wrap">
    			<?php screen_icon();?>
    			<h2>My Theme Options</h2>
    			<form method="post" action="options.php" enctype="multipart/form-data">
    				<?php settings_fields('jw_plugin_options');?>
    				<?php do_settings_sections(__FILE__);?>
    
    				<p class="submit">
    					<input name="submit" type="submit" class="button-primary" value="Save Changes"/>
    				</p>
    			</form>
    		</div>
    
    		<?php
    	}
    
    	public function register_settings_and_fields()
    	{
    		//register settings, create section, create fields associated with that section
    		register_setting('jw_plugin_options', 'jw_plugin_options', array($this, 'jw_validate_settings'));//3rd param = optional cb
    		//params = id, section title, cb, which page?
    		add_settings_section(
    			'jw_main_section',
    			'Main Settings',
    			array($this, 'jw_main_section_cb'),
    			__FILE__);
    		//first field
    		add_settings_field(
    			'jw_banner_heading',
    			'Banner Heading: ',
    			array($this, 'jw_banner_heading_setting'),
    			__FILE__,
    			'jw_main_section'
    		);
    		//second field
    		add_settings_field(
    			'jw_logo',
    			'Your Logo: ',
    			array($this, 'jw_logo_setting'),
    			__FILE__,
    			'jw_main_section'
    		);
    	}
    
    	public function jw_main_section_cb()
    	{
    		//optional
    	}
    
    	public function jw_validate_settings($plugin_options)
    	{
    		print_r($_FILES);
    	}
    	/*
    	*
    	* Inputs
    	*
    	*/
    	//banner heading settings
    	public function jw_banner_heading_setting()
    	{
    	echo "<input name='jw_plugin_options['jw_banner_heading']'
    		 type='text'
    		 value='{$this->options['jw_banner_heading']}' />";
    
    	}
    
    	//logo settings
    	public function jw_logo_setting()
    	{
    		echo '<input type="file"/>';
    	}
    
    }
    
    //ensure the options page only gets added for the admin_menu page
    add_action('admin_menu', function(){
    	JW_Options::add_menu_page();
    });
    
    //initiate the class
    add_action('admin_init', function(){
    	new JW_Options();
    });

    Thank you

  2. iansane
    Member
    Posted 4 months ago #

    bump,

    anyone?

    Am I posting this in the correct place?

  3. thereformation
    Member
    Posted 4 months ago #

    Its better I think to integrate in with the standard wordpress media upload functionality, so that your users can use images that might already be in the library. You can do this fairly easily in a theme options page. There is a good example here: http://www.deluxeblogtips.com/2011/03/meta-box-script-update-v30.html to get you started.

    But back to you actual issue;
    Is any data getting passed into your validation method? What does a var_dump() of your $plugin_options show?

  4. iansane
    Member
    Posted 4 months ago #

    Hi thereformation,

    Thanks for the reply.

    I'll read the link you provided and see if I can do this differently.

    The var_dump() outputs this.
    String(0) ""

    If I remove the file upload field the form works fine and that would be returning the header text correctly. I wish I could get php errors output to the page so I could see something that might give me a clue.

  5. iansane
    Member
    Posted 4 months ago #

    Wow this was so simple. I feel like a real dummy now. I had no name attribute for the file input in the jw_logo_settings method.

    I found the mistake while comparing to the code here:
    http://www.wptavern.com/forum/themes-templates/1346-creating-upload-function-options-page.html#post13306

    Problem solved.

  6. thereformation
    Member
    Posted 4 months ago #

    Ahh, I missed that as well.

Reply

You must log in to post.

About this Topic