Api settings image upload
-
Hi, I am creating api settings page. I have an logo upload option in page. My problem is, if I click upload button when the file upload field is empty, my option array looks like this a:3:{ s:5:”files”;s:5:”Array”;s:4:”file”;a:1:{s:5:”error”;s:21:”No file was uploaded.”;}}. I have written my full code below please help somebody.
<?php /* Plugin Name:Rough test */ //Logo Upload add_action('admin_init', 'ld_admin_init'); function ld_admin_init() { register_setting( 'ld_options', 'ld_options', 'ld_options_validate' ); add_settings_section('ld_main', 'Upload', 'ld_section_text', 'ld'); add_settings_field('ld_setting_checkbox', 'Use image', 'ld_setting_checkbox', 'ld', 'ld_main'); add_settings_field('ld_setting_string', 'Logo text', 'ld_setting_string', 'ld', 'ld_main'); add_settings_field('ld_filename', 'File:', 'ld_setting_filename', 'ld', 'ld_main'); } // add the admin options page add_action('admin_menu', 'ld_admin_add_page'); function ld_admin_add_page() { $mypage = add_theme_page('Logo', 'Logo', 'manage_options', 'ld', 'ld_options_page'); } // display the admin options page function ld_options_page() { ?> <div class="wrap"> <h2>Logo settings</h2> <p>You can upload a new logo.</p> <form method="post" enctype="multipart/form-data" action="options.php"> <?php settings_fields('ld_options'); ?> <?php do_settings_sections('ld'); ?> <p class="submit"> <input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" /> </p> </form> </div> <?php } function ld_section_text() { echo '<input type="hidden" name="ld_options[file]" value="' . serialize( $file ) . '" />'; $options = get_option('ld_options'); echo '<p>Upload your file here:</p>'; if ($file = $options['file']) { //var_dump($file); echo "<img src='{$file['url']}' />"; } } function ld_setting_filename() { echo '<input type="file" name="ld_filename" size="40" />'; } function ld_setting_checkbox() { $usetext = get_option('ld_options'); if($usetext['chkbox1']) { $checked = ' checked="checked" '; } echo "<input ".$checked." id='plugin_chk1' name='ld_options[chkbox1]' type='checkbox' />"; } function ld_setting_string() { $logotext = get_option('ld_options'); echo "<input id='ld_logo_text' name='ld_options[text_string]' size='40' type='text' value='{$logotext['text_string']}' />"; } function ld_options_validate( $input ) { // set $newinput to the $input that was POSTed $newinput = $input; // update the file field if ($_FILES['ld_filename']) { $overrides = array('test_form' => false); $file = wp_handle_upload($_FILES['ld_filename'], $overrides); $newinput['file'] = $file; } else { $newinput['file'] = maybe_unserialize( $input['file'] ); } return $newinput; } ?>
- The topic ‘Api settings image upload’ is closed to new replies.