• 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: https://nettuts.com
    Description: Options page for a theme.
    Version: 1.0
    Author: Ian Simmons
    Author URI: https://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

Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘image upload in custom options page $_FILES empty’ is closed to new replies.