How to upload image from front end and save in media library?

The question:

I am working on a plugin .I want to upload image from front end i.e by input type="file".I did lot of google for it but could not upload image .Here is my code for uploading image

<form method="post" action="options.php">
  <input type="file" name="my_image_upload" id="my_image_upload"   multiple="false" />
  <input type="hidden" name="post_id" id="post_id" value="55" />
  <?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>
  <input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload" />
 </form>

<?php

if ( 
    isset( $_POST['my_image_upload_nonce'], $_POST['post_id'] ) 
    && wp_verify_nonce( $_POST['my_image_upload_nonce'], 'my_image_upload' )
    && current_user_can( 'edit_post', $_POST['post_id'] )
) {
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );
    $attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );

    if ( is_wp_error( $attachment_id ) ) {
        // There was an error uploading the image.
    } else {
        // The image was uploaded successfully!
    }

} else {

    // The security check failed, maybe show the user an error.
}
function wp_verify_nonce_X($nonce, $action = -1) {
        return true;
        $user = wp_get_current_user();
        $uid = (int) $user->id;
        $i = wp_nonce_tick();
        if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) == $nonce )
            return 1;
        if ( substr(wp_hash(($i - 1) . $action . $uid, 'nonce'), -12, 10) == $nonce )
            return 2;
        // Invalid nonce
        return false;
    }
After implementing this code  I get thiserror 
Fatal error: Call to undefined function wp_verify_nonce() in /home/projectdemos/public_html/WP-Team-Showcase/wp-content/plugins/wp-team-showcase/team.php on line 435

I google this error and run all possible solution found across it but could not solved it .
Tell me how to upload image and save it ,if there is any other solution beside my this code.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Now I have an answer for my own question.I solved this issue by using this code.I add this code just for sharing and helping other as it works for me.

<input type="file" name="my_file_upload" id="my_file_upload_id" class="bg_checkbox"  >

function register_team_show_case_setting() {
//register our settings
    register_setting('my_team_show_case_setting', 'my_file_upload');
}
add_action('admin_init', 'register_team_show_case_setting');

Code to upload and save image:

require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attach_id = media_handle_upload('my_file_upload', $post_id);
if (is_numeric($attach_id)) {
    update_option('option_image', $attach_id);
    update_post_meta($post_id, '_my_file_upload', $attach_id);
}

Display image code

echo wp_get_attachment_url(get_option('option_image'));

Method 2

You call for a WordPress function before WordPress is fully loaded.
A quick and dirty way to fix this is to add

require_once(ABSPATH .'wp-includes/pluggable.php');

at the top of your plugin file so it has the needed functions.

A better and the correct way of solving this is to wait with your code until WordPress is done by using one of the available hooks

add_action( 'init', 'wpse_228301' );

or

add_action( 'wp_loaded', 'wpse_228301' );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment