Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: Upload Image in Admin Options Page

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

I have made an admin options page and now want to add a field to allow a user to upload an image. How can I upload an image to the wp-content/uploads folder. So far, I have this within a table:

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

Is this the right approach? If so, how do I direct the file to the right folder? Doesn't wordpress have it's own way of handling file uploads?

like image 837
bbbbbbbbbb Avatar asked Apr 04 '14 23:04

bbbbbbbbbb


People also ask

How do I add an image to my WordPress admin?

To upload images in WordPress, you should go to Media » Add New from your admin backend. You can drop multiple images in one go. The images you upload in WordPress should match the supported file types. The uploaded photos can be found in the Media » Library to view in the WordPress admin backend.

Why is WordPress not allowing me to upload images?

The image upload issue in WordPress is typically caused by incorrect file permissions. Your WordPress files are stored on your web hosting server and need specific file and directory permissions to work. Wrong file permissions prevent WordPress from reading or uploading file on the hosting server.

How do I allow people to upload photos to my website?

If you want to allow a user to upload an external file to your website, you need to use a file upload box, also known as a file select box. This is also created using the <input> element but type attribute is set to file.

How do I upload a custom image in WordPress?

openDefaultEditor({ // The image to load src: field. files[0], // Set up custom file upload imageWriter: { store: { // Where to post to url: form. getAttribute('action'), // Which fields to post dataset: (state) => [ // Copy file field name and set value to output image [field.name, state.


2 Answers

Add this code to your global custom option function.

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 info

or

Media uploader in theme and plugin

enter image description here

like image 80
Ravi Patel Avatar answered Oct 23 '22 23:10

Ravi Patel


You can use the inbuilt function of wordpress

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

This will automatically move the file to the uploads folder

Or

You can write your own PHP function.

Additional Details can be found here -> Wordpress File Upload

like image 1
Magesh Kumaar Avatar answered Oct 23 '22 21:10

Magesh Kumaar