WordPress Tutorial

How does WordPress add a wp_editor editor on the front end to upload pictures without popping up the media library

 Alibaba Cloud

We often encounter editor problems in wordpress development. Wordpress has an editor called wp_editor. The editor has the function of uploading pictures, but it needs to be added to the media and pop up the window of the media library. This may not be friendly to users who submit articles in the foreground. So how can we add a minimalist editor with pictures uploaded to the foreground? Here I will explain it to you.

First, call the editor. We need to simplify the buttons on the editor.

Want to be here? contact us bar
 Maker host
  1.  wp_editor (  '' ,  'task_content' , task_editor_settings ( array ( 'textarea_name' => 'content' ,  'height' => two hundred and fifty ,  'allow_img' =>  one ) )  ) ;

Then we define some functions and upload logic

  1.  function task_editor_settings ( $args  =  array ( ) ) {
  2.  $allow_img  =  isset ( $args [ 'allow_img' ] )  &&  $args [ 'allow_img' ] ? one  :  zero ;
  3.  return  array (
  4.  'textarea_name'  =>  $args [ 'textarea_name' ] ,
  5.  'media_buttons'  =>  false ,
  6.  'quicktags'  =>  false ,
  7.  'tinymce'  =>  array (
  8.  'statusbar'  =>  false ,
  9.  'height'  =>  isset ( $args [ 'height' ] ) ? $args [ 'height' ]  :  one hundred and twenty ,
  10.  'toolbar1'  =>  'bold, italic,underline,blockquote,bullist,numlist' . ( $allow_img ? ',TaskImg' : '' ) ,
  11.  'toolbar2'  =>  '' ,
  12.  'toolbar3'  =>  ''
  13.  )
  14.  ) ;
  15.  }
  16.   
  17.  add_filter (  'mce_external_plugins' ,  'erphp_task_mce_plugin' ) ;
  18.  function erphp_task_mce_plugin ( $plugin_array ) {
  19.  $plugin_array [ 'TaskImg' ]  = ERPHP_TASK_URL .  '/static/js/TaskImg.js' ;
  20.  return  $plugin_array ;
  21.  }
  22.   
  23.  add_action ( 'wp_ajax_task_img_upload' ,  'erphp_task_img_upload' ) ;
  24.  function erphp_task_img_upload ( ) {
  25.  $res  =  array ( ) ;
  26.   
  27.  $user  = wp_get_current_user ( ) ;
  28.  if ( $user -> ID ) {
  29.  $upfile  =  $_FILES [ 'upfile' ] ;
  30.  $upload_overrides  =  array ( 'test_form'  =>  false ) ;
  31.  $file_return  = wp_handle_upload ( $upfile ,  $upload_overrides ) ;
  32.   
  33.  if  ( $file_return  &&  ! isset ( $file_return [ 'error' ] ) )  {
  34.  //Save to Media Library
  35.  $attachment  =  array (
  36.  'post_title'  =>  preg_replace (  '/\. [^.]+$/' ,  '' ,  basename (  $file_return [ 'file' ]  )  ) ,
  37.  'post_mime_type'  =>  $file_return [ 'type' ] ,
  38.  ) ;
  39.  $attach_id  = wp_insert_attachment ( $attachment ,  $file_return [ 'file' ] ) ;
  40.  $attach_data  = generate_attachment_metadata ( $attach_id ,  $file_return [ 'file' ] ) ;
  41.  wp_update_attachment_metadata ( $attach_id ,  $attach_data ) ;
  42.  $res [ 'result' ]  =  zero ;
  43.  $file_return [ 'alt' ]  =  preg_replace (  '/\. [^.]+$/' ,  '' ,  basename (  $file_return [ 'file' ]  )  ) ;
  44.  $res [ 'image' ]  =  $file_return ;
  45.  }  else  {
  46.  $res [ 'result' ]  =  one ;
  47.  }
  48.  }  else  {
  49.  $res [ 'result' ]  =  two ;
  50.  }
  51.  echo  json_encode ( $res ) ;
  52.  exit ;
  53.  }
  54.   
  55.  function generate_attachment_metadata ( $attachment_id ,  $file )  {
  56.  $attachment  = get_post (  $attachment_id  ) ;
  57.  $metadata  =  array  ( ) ;
  58.  if  ( ! function_exists ( 'file_is_displayable_image' ) )  include ( ABSPATH .  'wp-admin/includes/image.php'  ) ;
  59.   
  60.  if  ( preg_match  (  '!^ image/!' , get_post_mime_type (  $attachment  )  )  && file_is_displayable_image (  $file  ) )  {
  61.  $imagesize  =  getimagesize  (  $file  ) ;
  62.  $metadata  [ 'width' ]  =  $imagesize  [ zero ] ;
  63.  $metadata  [ 'height' ]  =  $imagesize  [ one ] ;
  64.  list  (  $uwidth ,  $uheight  )  = wp_constrain_dimensions (  $metadata  [ 'width' ] ,  $metadata  [ 'height' ] ,  one hundred and twenty-eight ,  ninety-six  ) ;
  65.  $metadata  [ 'hwstring_small' ]  =  "height=' $uheight ' width=' $uwidth '" ;
  66.   
  67.  // Make the file path relative to the upload dir
  68.  $metadata  [ 'file' ]  = _wp_relative_upload_path (  $file  ) ;
  69.  // work with some watermark plugin
  70.  $metadata  = apply_filters (  'wp_generate_attachment_metadata' ,  $metadata ,  $attachment_id  ) ;
  71.  }
  72.  return  $metadata ;
  73.  }

Then you need js to upload

  1.  tinymce. PluginManager . add ( 'TaskImg' ,  function ( editor , url )  {
  2.   
  3.  var $el = jQuery ( editor. getElement ( ) ) . parent ( ) ;
  4.  var timer =  null ;
  5.   
  6.  var input = document. createElement ( 'input' ) ;
  7.  input. setAttribute ( 'type' ,  'file' ) ;
  8.  input. setAttribute ( 'accept' ,  'image/*' ) ;
  9.   
  10.  function notice ( type , msg , time ) {
  11.  clearTimeout ( timer ) ;
  12.  jQuery ( '#notice' ) . remove ( ) ;
  13.  $el. append ( '<div id="notice"><div class="notice-bg"></div><div class="notice-wrap"><div class="notice-inner notice-' + type + '">' + msg + '</div></div></div>' ) ;
  14.  if ( time )  {
  15.  timer = setTimeout ( function  ( )  {
  16.  jQuery ( '#notice' ) . remove ( ) ;
  17.  } , time ) ;
  18.  }
  19.  }
  20.   
  21.  function img_post ( )  {
  22.  var fd =  new FormData ( ) ;
  23.  fd. append (  "upfile" , input. files [ zero ] ) ;
  24.  fd. append (  "action" ,  'task_img_upload' ) ; 
  25.  jQuery. ajax ( {
  26.  type :  'POST' ,
  27.  url : _ERPHP. ajaxurl ,
  28.  data : fd , 
  29.  processData :  false ,
  30.  contentType :  false ,
  31.  dataType :  'json' ,
  32.  success :  function ( data , textStatus , XMLHttpRequest )  {
  33.  clearTimeout ( timer ) ;
  34.  jQuery ( '#notice' ) . remove ( ) ;
  35.  if ( data. result == '0' ) {
  36.  editor. insertContent (  '<img src="' + data. image . url + '" alt="' + data. image . alt + '">'  ) ;
  37.  } else {
  38.  notice ( zero ,  'There was an error uploading the image, please try again later!' ,  one thousand and two hundred ) ;
  39.  }
  40.  } ,
  41.  error :  function ( MLHttpRequest , textStatus , errorThrown )  {
  42.  clearTimeout ( timer ) ;
  43.  jQuery ( '#notice' ) . remove ( ) ;
  44.  alert ( errorThrown ) ;
  45.  }
  46.  } ) ;
  47.  }
  48.   
  49.  input. onchange  =  function ( )  {
  50.  var file =  this . files [ zero ] ;
  51.   
  52.  if ( file ) {
  53.  if ( !/ \. ( gif | jpg | jpeg | png | GIF | JPG | JPEG | PNG ) $ / . test ( file. name ) ) {
  54.  notice ( zero ,  'Only image files in jpg, png and gif formats can be uploaded' ,  two thousand ) ;
  55.  return  false ;
  56.  } else  if ( file. size  >  two  *  one thousand and twenty-four  *  one thousand and twenty-four ) {
  57.  notice ( zero ,  'The image size cannot exceed 2M' ,  one thousand and five hundred ) ;
  58.  return  false ;
  59.  } else {
  60.  img_post ( ) ;
  61.  notice ( one ,  'Uploading...' ,  zero ) ;
  62.  }
  63.  }
  64.  } ;
  65.   
  66.   
  67.  editor. addButton ( 'TaskImg' ,  {
  68.  text :  '' ,
  69.  icon :  'image' ,
  70.  tooltip :  "Upload picture" ,
  71.  classes :  'TaskImg' ,
  72.  onclick :  function ( )  {
  73.  if (  !  /Android|webOS|iPhone|iPod|BlackBerry/i . test ( navigator. userAgent )  )  {
  74.  input. click ( ) ;
  75.  }
  76.  } ,
  77.  onTouchEnd :  function ( ) {
  78.  if (  /Android|webOS|iPhone|iPod|BlackBerry/i . test ( navigator. userAgent )  )  {
  79.  input. click ( ) ;
  80.  }
  81.  }
  82.  } ) ;
  83.  } ) ;

Finally, it should be noted that users need to have the upload permission of WordPress.

The tutorial is simple and needs someone with certain development ability to understand it clearly~

How does WordPress add a wp_editor editor on the front end to upload pictures without popping up the media library

276 people have bought
View Demo Upgrade VIP Buy Now

Collection
fabulous ( one )

Post reply

Hot selling template

Ashade - Works exhibition photo album WordPress Chinese theme
 LensNews

This site accepts WordPress/PbootCMS/DedeCMS, etc
System construction, imitation, development, customization and other services!