Working with Files

All images and files throughout the MasterPress API are represented by the WOOF_Image and WOOF_File classes respectively. The classes offers a lot of utility methods which will often save you from needing to call on lower level PHP file functions. The examples presented in this section assume you already know how to access files and images via the API, please read through Accessing Files and Images if you’re not familiar.

Note that WOOF_File is the base class for WOOF_Image, so all examples related to files also apply to images.

File links and enclosures

The method WOOF_File::link can be used to easily build an HTML anchor (a) tag linking to the file. This method accepts an arguments string or array to override both the attributes used, and the link text via the “text” argument:

Example 1: Linking to a file

Building an enclosure tag for an RSS feed is made easy via the WOOF_File::enclosure method:

Example 2: Building an enclosure tag

Property Methods

The WOOF_File class contains a host of utility methods for accessing properties on the file, many of which have more concise method names than their counterpart functions in PHP.

Path and file name methods

Firstly, let’s look at accessing path and filename attributes, via the methods WOOF_File::pathWOOF_File::extensionWOOF_File::basename, WOOF_File::name, and WOOF_File::filename:

Example 3: File path and file name methods

All of these should be explained well enough in the comments in Example 1, and the class reference for WOOF_File explains them in detail.

File URLs

Next, lets look at a number of ways to get absolute and root-relative URLs with the methods WOOF_File::urlWOOF_File::absolute_urlWOOF_File::permalink, and WOOF_File::abs:

Example 4: URL methods

Again, these should be straightforward.

File Attributes

WOOF_File has a number of methods for dealing with the size of the file, which can be really useful to output nicely formatted file sizes for linking to file downloads. Let’s look at how to use WOOF_File::size and WOOF_File::bytes:

Example 5: File size methods

Now let’s look at how to access information about the kind of file we’re dealing with. In addition to a the WOOF_File::mime method to retrieve the mime-type, WOOF_File also contains a number of static arrays which store both short and long descriptions of known file extensions, accessable through the WOOF_File::filetype and methods:

Example 6: Getting file type info

Finally you can access the modification date of a file with the WOOF_File::modified function, which can take also take a formatter string:

Example 7: Modification date

Deleting a file

We can delete the file this object points to, assuming the web user has permission to do so, with one of two methods: WOOF_File::delete and WOOF_File::unlink:

Example 8: Deleting a file

Latest From the Blog

MasterPress 1.4.1 is now available

27th May 2024

This update addresses backwards compatibility issues that were introduced in the previous update. There were some edge cases where PHP 7 had issues and it didn’t work at all on PHP 5.6. We have now changed it so that the PHP 8 compatibility changes will now only affect PHP 8 and will be more thorough… 

Plugin Requirements

MasterPress requires a minimum of WordPress version 4.9, MySQL 5.6, and PHP version 5.6.20.

We also recommend that PHP is configured to use a memory limit of 64MB per request (128MB may be required for sites with higher complexity).

This plug-in is not compatible with the hosted service.

Three AM