0

I am making a very simple WordPress website for a non-technical person. For the admin, I wrote a PHP code for a form which creates events based on the information provided. However, for the image upload, I am using a jQuery plugin called FileUploader. The plugin stores images into a local folder. I tried to change the media library folder from wp-content/uploads to that specific local folder for testing purposes. I can see that the images are being uploaded to the folder but not showing up in the media library. Is there a way I can accomplish that? Any help is appreciated.

Here is a test code for form for image upload:

<?php
include('../../../src/class.fileuploader.php');
include('../../../../../../../wp-load.php');
include('../../../../../../../wp-admin/includes/image.php');

// initialize FileUploader
$FileUploader = new FileUploader('files', array(
    'uploadDir' => '../uploads/',
    'title' => 'name'
));

// call to upload the files
$data = $FileUploader->upload();

$file_name = 'title';
$file_path = '../uploads/'.$file_name;
$file_url = 'http://'.$file_path;
$wp_filetype = wp_check_filetype($file, null);

$attachment = array(
    'guid'           => $file_url,
    'post_mime_type' => $wp_filetype['type'],
    'post_title'     => $file_name,
    'post_status'    => 'inherit',
    'post_date'      => date('Y-m-d H:i:s')
);

$attachment_id = wp_insert_attachment($attachment, $file_path);
$attachment_data = wp_generate_attachment_metadata($attachment_id, $file_path);
wp_update_attachment_metadata($attachment_id, $attachment_data);

// if uploaded and success
if($data['isSuccess'] && count($data['files']) > 0) {
    // get uploaded files
    $uploadedFiles = $data['files'];
}
// if warnings
if($data['hasWarnings']) {
    // get warnings
    $warnings = $data['warnings'];

    echo '<pre>';
    print_r($warnings);
    echo '</pre>';
    exit;
}

// unlink the files
// !important only for appended files
// you will need to give the array with appendend files in 'files' option of the FileUploader
foreach($FileUploader->getRemovedFiles('file') as $key=>$value) {
    unlink('../uploads/' . $value['name']);
}

// get the fileList
$fileList = $FileUploader->getFileList();

// show
echo '<pre>';
print_r($fileList);
echo '</pre>';

So basically I am uploading the images to upload folder.

And I put this code in my wp-config to change the media library folder which references to the same upload folder:

define('UPLOADS', 'wp-content/themes/ilrtheme/fileuploader/examples/add-more/uploads/');
  • Please add more information to your question. A piece of code can be a good start. – Jack Johansson Apr 13 '17 at 3:04
  • The way WordPress was historically is a blog, so it organizes by month/year. There are many plugins that offer folder based organization (wordpress.org/plugins/media-library-plus), you can look in a plugin's code and see if you can get it to work. Otherwise, the drag and drop in WordPress plus a plugin if you want to use folders is very easy to use. – Christina Apr 13 '17 at 3:19
  • @JackJohansson I added the code. Hopefully it made my question a bit clearer – Nirmit Patel Apr 13 '17 at 4:17
  • @Christina The plugin that you showed helps me to organize my media folder. However, is it possible that I would just drag and drop the image to my Upload folder and since media library is linked with my Upload folder, the picture will also show up in the media library? – Nirmit Patel Apr 13 '17 at 4:20
  • 1
    If you code is only storing images to any folder then you won't see it in media library because when you upload an image in media library then it's entry use to store in DB. If there is not entry in DB for your custom folder of images then those images won't be visible in media library. In short, just uploading images in folder won't have any effect in media library until uploaded image don't get it's path submitted in DB. – Rishabh Apr 13 '17 at 4:38

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.