1

So I'm making this Property Listing theme for a client of mine, and on the Properties Custom Post Type (CPT) which I have created I need to add a meta box that allows him to upload multiple pictures - something similar to this:

Upload Meta Box

How could I do this? Everything I have seen so far recommends plugins like Advanced Custom Fields but I would like to avoid using 3rd party Plugins if I can.

Thanks :)

2 Answers 2

2

I recommend you these videos --> https://www.youtube.com/playlist?list=PLIjMj0-5C8TJYSkKnUyHH1VXlIpvZXOMT

In my opinion I prefer use the plugin ACF. The plugin helps to save time. It's easy to use and the client will be happy. Also, the client won't care if you used a plugin or you wrote the code by yourself, the client will care if the functionality works.

I am the first in not installing plugins but ACF plugin is necessary (mandatory). I use it in all my projects.

2
  • I know some others who disagree with ACF ;) I know what ACF is and does but personally I'd prefer to learn more about the WordPress CodeX and how to do things without a Plugin - but then again that's just personal preference and everyone is different :) Thanks for that link I will take a look into it when I can
    – Stephen
    Commented Apr 6, 2016 at 22:37
  • 1
    ACF is a plugin and if you create that functionality, to create a custom image uploader, you will have to create a plugin so... you will have to have a plugin for that, your won plugin or ACF. I guess... you can not do it writing two or three lines in the functions.php
    – andresgl
    Commented Apr 6, 2016 at 22:44
0

Add a security object for file uploads like so:

class FileSecure
{
    public resource $Allowed;
    private object $Info;
    public function __construct($allow)
    {
        $this->Allowed = $allow;    
        $this->Info = new finfo();
    }
    public function Check($file) : bool
    {
        if(in_array($fileType = $this->Info->file($file, FILEINFO_MIME_TYPE, $this->Allowed))) { return true; } else { return false; }
    }
}

$fileCheck = array(
     'Image' => new FileSecure(['image/bmp', 'image/gif', 'image/jpeg', 'image/png']),
     'Text' => new FileSecure(['text/plain']),
     'Compressed' => new FileSecure(['application/zip', 'application/x-rar-compressed'])
);

Click here to view all the MIME name's to extensions

Then add a front-end form which allows file upload and just pass it through this object before use of the uploaded file.

// note it must exist on the server before checking
if(!$fileCheck['Image']->Check($filename)):
    // delete the file
endif;

You could use the Database to hold file locations or just have a default upload directory on your server and iterate through the files held within that directory.

1
  • Shall I make a Plugin to do this? Will it be used in more than one Project?
    – Jaquarh
    Commented Apr 12, 2016 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.