in plugins folder i’have custom-post-metabox folder, inside it custom-post-metabox.php, admin.js and admin.css.
//code of custom-post-metabox.php

<?php
/**
 * Plugin Name: Custom Post Meta Box
 * Plugin URI: http://localhost/wpuploadtest
 * Description: Demonstration of custom post meta box
 * Version:1.0.0
 * Author: me
 * Author URI: http://google.co.in
 * License:GPL2
 */
function cpmb_css(){
    wp_enqueue_style('cpmb-admin', plugins_url('custom-post-metabox/admin.css'));
}
add_action('admin_enqueue_scripts', 'cpmb_css');
function cpmb_js(){
  $screen = get_current_screen();
  if ('post' == $screen->id) {
    wp_enqueue_script('cpmb-admin', plugins_url('custom-post-metabox/admin.js'));
  }
}
add_action('admin_enqueue_scripts', 'cpmb_js');
function cpmb_add_meta_box(){
    add_meta_box(
       'cpmb_audio',            //The ID for the meta box
       'Add_mp3',               //The title for the meta box
       'cpmb_display_meta_box', //the callback function for the meta box
       'post',                  //we'll be only displaying this on posts
       'side',                  //where the meta box should appear
       'core'                   //the priority of where the meta box should be displayed
        );
}
add_action('add_meta_boxes', 'cpmb_add_meta_box');
function cpmb_display_meta_box($post){
  wp_nonce_field(plugin_basename(__FILE__), 'cpmb-nonce-field');
  if ('invalid-file-type' == get_post_meta($post->ID, 'mp3', true)) {
    ?>
    <div class="error">Invalid File Type</div>
  <?php
    }
    ?>
         <label id="title_label" for="mp3_title"> MP3 Title</label>
         <input type="text" id="mp3_title" name="mp3_title" value="<?php echo get_post_meta($post->ID, 'mp3_title', true); ?>" placeholder="Give me some sunshine">
         <label id="file_label" for="mp3_file"> MP3 File</label>
         <input type="file" id="mp3_file" name="mp3_file">
    <?php
}
function cpmb_save_metabox_data($post_id){
    if(cpmb_user_can_save($post_id, 'cpmb-nonce-field')){
               if (isset($_POST['mp3_title']) && !empty($_POST['mp3_title']) ) {
                 $mp3_title = stripslashes(strip_tags($_POST['mp3_title']));
                 update_post_meta($post_id, 'mp3_title', $mp3_title);
               }
              if (isset($_FILES['mp3_file']) && !empty($_FILES['mp3_file']) ) {
                       if (cpmb_valid_mp3($_FILES['mp3_file']['name'])) {
                              $response = wp_upload_bits($_FILES['mp3_file']['name'], null, file_get_contents($_FILES['tmp_name']));
                              if (0 ==strlen(trim($response['error']))) {
                                update_post_meta($post_id, 'mp3', $response['url']);
                              }
                       }else{
                           update_post_meta($post_id, 'mp3', 'invalid-file-type');
                       }
             }
    }
}
add_action('save_post', 'cpmb_save_metabox_data');
function cpmb_valid_mp3($filename){
 $path_parts = pathinfo($filename);
 return 'mp3' == strtolower($path_parts['extension']);
}
function cpmb_user_can_save($post_id, $nonce){
  $is_autosave = wp_is_post_autosave($post_id) ;
  $is_revision = wp_is_post_revision($post_id);
  $is_valid_nonce = (isset($_POST[$nonce]) && wp_verify_nonce($_POST[$nonce], plugin_basename(__FILE__) )  );
  return !($is_autosave || $is_revision) && $is_valid_nonce;
}
function cpmb_display_mp3($content){
      if(is_single()){
           if('invalid-file-type' != get_post_meta(get_the_ID(), 'mp3', true)){
            $title = get_post_meta(get_the_ID(), 'mp3_title', true);
            $link  = get_post_meta(get_the_ID(), 'mp3', true);
            $content .= '<a href="'.$link.'">'.$title.'</a>';
           }
      }
         return $content;
}
add_action('the_content', 'cpmb_display_mp3');

//code of admin.js

(function($){
  "use strict";
  $(function(){
    $('form').attr('enctype', 'multipart/form-data');
  });
})(jQuery);

The problem is if i just click on the update button without uploading anything the error message of invalid file type is still there , even if i leave the page and comeback to the edit page the error message is still there. How to fix it?

Read more here: How to get rid of error message of custom metabox in dashboard when moving out of edit page?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , , , , , ,

Wordpress related questions and answers: