Wordpress: Upload an image on the Admin Settings page

I am developing my first wordpress plugin. It should only allow the user to change the logo in a custom template and change the color scheme in a custom template.

I created the administration settings page and now I want to add a field that allows the user to upload an image. How to upload image to wp-content / uploads folder. So far I have this in the table:

<td><input name="logo_image" type="file" id="logo_image" value="" /></td> 

Is this the right approach? If so, how can I redirect the file to the correct folder? Does Wordpress have its own way to handle file uploads?

+6
source share
2 answers

Add this code to your global custom option.

 if(function_exists( 'wp_enqueue_media' )){ wp_enqueue_media(); }else{ wp_enqueue_style('thickbox'); wp_enqueue_script('media-upload'); wp_enqueue_script('thickbox'); } <p><strong>Header Logo Image URL:</strong><br /> <img class="header_logo" src="<?php echo get_option('header_logo'); ?>" height="100" width="100"/> <input class="header_logo_url" type="text" name="header_logo" size="60" value="<?php echo get_option('header_logo'); ?>"> <a href="#" class="header_logo_upload">Upload</a> </p> <script> jQuery(document).ready(function($) { $('.header_logo_upload').click(function(e) { e.preventDefault(); var custom_uploader = wp.media({ title: 'Custom Image', button: { text: 'Upload Image' }, multiple: false // Set this to true to allow multiple files to be selected }) .on('select', function() { var attachment = custom_uploader.state().get('selection').first().toJSON(); $('.header_logo').attr('src', attachment.url); $('.header_logo_url').val(attachment.url); }) .open(); }); }); </script> 

More details

or

Media downloader in theme and plugin

enter image description here

+14
source

You can use the built-in wordpress function

 <?php wp_handle_upload( $file, $overrides, $time ); ?> 

This will automatically move the file to the upload folder.

Or

You can write your own PHP function.

Further information can be found here → Download Wordpress File

0
source

All Articles