I have pre-written Objects that do such things like file checking the MIME type, then a main upload object but neither of which use the Database. So I am just curious to whether I could intergrate these Obejcts inside a Wordpress Plugin.

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
add_action('admin_menu', 'file_upload_menu');
function file_upload_menu()
    add_options_page('E4K File Uploader', 'E4k File Uploader', 'manage_options', 'e4k-File-Uploader', 'file_upload_options');
function file_upload_options()
    class Example
        public function canI(){ echo 'is this allowed?'; }
    (new Example)->canI();

Is something like that Possible? I am not a Wordpress developer, I just come from a PHP background developing CMS's and API's. This is new to me but was requested so I am just curious to limitations of OOP within Wordpress and/or its plugins.

Actual Code (Class):

class FileSecure
    public $Allowed;
    private $Info;
    public function __construct($allow)
        $this->Allowed = $allow;
        $this->Info    = new finfo();
    public function upload($file, $dir)
        $target = $dir . basename($file["name"]);
        (self::Check($file))? move_uploaded_file($file['tmp_name'], $target) : "";
    public function Check($file)
        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'] )

Actual Code (back-end function part):

function file_upload_options()
    if (!current_user_can('manage_options'))
        wp_die(__('You do not have sufficient permissions to access this page.'));
    if (isset($_FILES['Picture']))
        require_once 'e4k-file-upload-class.php';
        if($fileCheck['Image']->upload($_FILES['Picture'], 'Uploads/')): echo '<script>alert("Successfully uploaded");</script>'; endif;
    echo '<form enctype="multipart/form-data" method="POST">';
    echo '<input type="hidden" name="MAX_FILE_SIZE" value="30000" />';
    echo 'Select a file to upload: <input name="Picture" type="file" />';
    echo '<input type="submit" value="Upload File" />';
    echo '</form>';
    echo '<br /> <br />';
    echo 'Current Libary: <br /> <br />';
    foreach(uploaded_files_iterate('Upload/') as $file)
        (is_string($file))? $file : '<a href="'.Get_template_directory_uri().'/Uploads/'.$file['name'].'">'.$file['name'].'</a> <br />';
  • Well, I am not going to set-up the class inside the function, I will use a require_once() and then instance the class to get the Object inside the function, I just wanted to know if You can use OOP inside of wordpress - I was just told that its limited to using Codex and not creating your own Objects.
    – Jaquarh
    Commented Apr 13, 2016 at 9:29
  • Where did you get that info from. There are thousands of plugins on wordpress.org coded the OOP way. Widgets in core a coded in a crappy OOP way. The person who told you you can't use OOP definitely knows nothing about coding or does not know what OOP or WordPress is Commented Apr 13, 2016 at 9:49
  • 1
    What is true though, it is not always necessary to create objects, sometimes a normal spaghetti setup is much better. The fact remains, you CAN use OOP Commented Apr 13, 2016 at 9:51
  • Well, its not really about creating them, I have so many pre-built objects over my time that I could just integrate into the plugin, I just wanted to consolidate that I could use these Objects inside a plugin. Appreciated this, now just to design the hierarchy of how its going to come together ;) Thats probably the best part about Objects, they're reusable in projects if done properly
    – Jaquarh
    Commented Apr 13, 2016 at 9:53

1 Answer 1


depends if you want to write OOP or just use PHP classes ;)

wordpress hook system is procedural in nature and there is a big amount of globals being used so it takes some mental effort to translate it into actual OOP. Most (99.9%) people attempting this fail and produce something which is basically procedural code wrapped in a class.

TL;Dr You can use whatever the language lets you. Callbacks are usually a point where people fail especially when you need to have an option to remove them from a hook

  • I will update my question with my actual code :P - done, would that work? (its still under work but you get the gist) ;)
    – Jaquarh
    Commented Apr 13, 2016 at 10:05
  • Well, the code itself has no obvious problems I can spot as far as the structure of it is concerned, as I said in the answer the biggest problems are the integration with the procedural hook system. That said, I can spot that you are using some code that assumes there is no CMS around, and with that you might break some of the idioms of using wordpress. For example file uploads should be handled by the specific WP API, if you are not in the mood of recreating some of the WP core functionality. The more complex your classes are the more likely they are doing something that WP API already does. Commented Apr 13, 2016 at 10:46
  • ..... and you should always prefer wordpress API because the usage of the API allow other plugins a way to manipulate things (in your case for example, auto upload files to a CDN). Commented Apr 13, 2016 at 10:48
  • Wait, I understand now - that Object already exists inside Wordpress for use - just maybe less complex? And you're saying its more easy to use pre-built Objects/functions than try integrate your own? But, if it doesn't exist, then by all means, use your own?
    – Jaquarh
    Commented Apr 13, 2016 at 11:21
  • exactly, for many things an api exists already. You might need to add to it, or prefer to wrap around it for some reason, but for most things that you would expect a CMS to handle there is probably some core API for that. Commented Apr 13, 2016 at 11:30

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.