admin管理员组

文章数量:1326134

i am using this query in my plugin to uploading media file , when i keep it file in wp_enqueue_script it working properly except after i select the image , this code does not return on wordpress page.

$content .='<div class="col-sm-6">';
$content .='<label class="fo">Upload Image</label>';
$content .='<input type="button" type="button" name="txt_image"id="txt_image" class="form-control"style=" font: italic bold 12px/30px Georgia, serif;font-size:20px;" value="Upload Image">';
$content .='<img src="" id="getImage" style="height:100px;width:100px;"/>';
$content .='<input type="hidden" name="student_image" id="student_image"/>';
$content .='</div>';

<script>
        jQuery(document).on("click","#txt_image",function(){
              
              var image = wp.media({
                  title:"Upload Image",
                  multiple:false
              }).open().on("select", function(e){
                  
                  var uploaded_image = image.state().get("selection").first();
                var image_data = uploaded_image.toJSON();
                
                 jQuery("#getImage").attr("src", image_data.url);
                 jQuery("#student_image").val(image_data.url);
                  
              });
              
              
              
          });
    </script>

i am using this query in my plugin to uploading media file , when i keep it file in wp_enqueue_script it working properly except after i select the image , this code does not return on wordpress page.

$content .='<div class="col-sm-6">';
$content .='<label class="fo">Upload Image</label>';
$content .='<input type="button" type="button" name="txt_image"id="txt_image" class="form-control"style=" font: italic bold 12px/30px Georgia, serif;font-size:20px;" value="Upload Image">';
$content .='<img src="" id="getImage" style="height:100px;width:100px;"/>';
$content .='<input type="hidden" name="student_image" id="student_image"/>';
$content .='</div>';

<script>
        jQuery(document).on("click","#txt_image",function(){
              
              var image = wp.media({
                  title:"Upload Image",
                  multiple:false
              }).open().on("select", function(e){
                  
                  var uploaded_image = image.state().get("selection").first();
                var image_data = uploaded_image.toJSON();
                
                 jQuery("#getImage").attr("src", image_data.url);
                 jQuery("#student_image").val(image_data.url);
                  
              });
              
              
              
          });
    </script>
Share Improve this question edited Aug 11, 2020 at 19:34 t2pe 4823 silver badges11 bronze badges asked Aug 11, 2020 at 10:41 Arshpreet VenveruArshpreet Venveru 94 bronze badges 1
  • Do it the wordpress codex way not with jQuery as you need the theme and php code to be aware of your file: premium.wpmudev/blog/upload-file-functions. – Nathaniel Flick Commented Aug 11, 2020 at 20:49
Add a comment  | 

1 Answer 1

Reset to default 0
jQuery(document).ready(function($){
 $('#txt_image').click (function(e){
       e.preventDefault();
      var image = wp.media({
          title:"Upload Image",
          multiple:false
      }).open().on("select", function(e){
          
          var uploaded_image = image.state().get("selection").first();
        var image_data = uploaded_image.toJSON();
         jQuery("#getImage").attr("src", image_data.url);
         jQuery("#student_image").val(image_data.url);
          
      });
      

});
    
      
  });

本文标签: uploadsmedia file uploading