Good day. There is a problem: I am creating a plugin for WordPress. In the functionality of the plug-in there is a moment of uploading the file to the specified directory when editing the post and, accordingly, attaching the file to the post. Sample code (all inside the class):

add_action('save_post', array(&$this, 'savePostIcon')); function savePostIcon($id){ $opts = $this->getOptions(); //var_dump($_FILES); - пусто if(isset($_FILES['myplugin'])){ if(move_uploaded_file($_FILES['myplugin']['tmp_name'], $opts['path'].$_FILES['myplugin']['name'])){ add_post_meta($id, 'posticon_icon', $_FILES['myplugin']['name']); } } } 

The file is not sent through $ _FILES. And how then to fill it in the directory defined in the plugin settings?
Thank you in advance.

PS: enctype form is not worth it. What, in this case, can be done, except for inserting it into your javascript template to download the file?

  • one
    Feel free to ask - is the form enctype worth it? =) - Zowie
  • Damn, no. = / And then how to be? - ling

2 answers 2

In general, we need a mold like this:

 <form id="mypluginForm" method="POST" enctype="multipart/form-data"> <input type="file" name="myplugin"> </form> 

If, for example, the form is already there, but there is no possibility to correct it in the template - you can add enctype using javascript.

 onload = function(){ document.getElementById("mypluginForm").enctype = "multipart/form-data"; } 

Well, if there is an opportunity to correct the pattern - we fix it and that's it =)

Then, if the file is uploaded, it will be available in $_FILES['myplugin']

  • Great, thank you). - ling
  • always welcome =) - Zowie

Or in the plugin file

 // Добовление к форме multipart/form-data add_action('post_edit_form_tag' , 'add_form_tag'); function add_form_tag() { echo ' enctype="multipart/form-data"'; }