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" />


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.


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'));
  • Hello. How can i upload multiple images with your example? – bilimokur Apr 12 '18 at 14:32

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' );


add_action( 'wp_loaded', 'wpse_228301' );
  • after using require_once(ABSPATH .'wp-includes/pluggable.php'); at the top error removed but no imageis uploading to media library – raxa May 31 '16 at 15:41
  • I urge you to develop the plugin as it should be, so use the init or wp_loaded action hooks to initialize the plugin. That way all wordpress functions are available to you. The way you are handling it now with requiring files works in most cases but is definitely not how it should be done. Also I aimed to get rid of your error message, I did not check your entire code so if the error you had is gone, some other part of your code is not working properly. – Jeffrey von Grumbkow Jun 1 '16 at 7:35
  • I have uploaded image using this code <input type="file" name="my_file_upload" id="my_file_upload"><?php 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_post_meta( $post_id, '_my_file_upload', $attach_id ); } ?> In my team.php file .Now I want to display it in post-type.php file Can you tell me how to display it ? – raxa Jun 1 '16 at 8:41
  • You should make a new post for this to avoid cluttering since this issue is resolved but you save it as post meta "_my_file_upload" so you can get it back on the front-end by retreiving that post meta: $attachment = get_post_meta( get_the_ID(), '_my_file_upload' ); – Jeffrey von Grumbkow Jun 1 '16 at 9:26
  • $attachment = get_post_meta( get_the_ID(), '_my_file_upload' ); it gives me a empty array – raxa Jun 1 '16 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.