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.1.4 now available

MasterPress 1.1.4 is now available. This release contains an important compatibility fix for WordPress 4.5 to allow correct detection of the taxonomy term editing screen. Without this fix, any custom fields you have attached to custom taxonomies will not be shown at all in the editing form. Note also that MasterPress will still detect the edit screen correctly in… 

Plugin Requirements

MasterPress requires a minimum of WordPress version 3.4, MySQL 5, and PHP version 5.2.4.

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 WordPress.com hosted service.

Traversal