Tag archive: media

Setting the image crop position in WordPress

Here’s a nice addition to WordPress, which I missed in last year’s 3.9 release. Now, when you define an image size with add_image_size, instead of just saying ‘soft crop’ (keep proportions) or ‘hard crop’ (fit to area), you can now also pass an array to define where you want a hard crop to attack the image from. For example:

add_image_size( 'custom-size', 220, 220, array( 'left', 'top' ) );

The first element in the ‘crop array’ can be ‘left’, ‘center’, or ‘right’; the other can be ‘top’, ‘center’, or ‘bottom’. Nice!

As an image size bonus, here’s what I came up with recently when I wanted all WordPress image sizes to be controlled via custom theme code (not just the custom sizes).

Planning WordPress image sizes

On larger projects, designs that need to be turned into a custom WordPress theme can have a wide array of image sizes. I’m currently working on such a project, and I thought I’d share a few things that might help others working in a similar situation. My aim is to contribute to a better understanding of how WordPress currently handles image sizing, how understanding this can potentially feed into the design process, and how WordPress developers can respond to design requirements while not making life too confusing for the client’s content editors.

Read more »

WordPress Media Library URLs

Many, many times in my custom WordPress development, there are situations where I have to ask clients to grab the URL for an image from the Media Library. Maybe to paste into a widget or a custom field? In any case, to get the URL of the file, by default in WP you have to edit the media first—you get a read-only input called File URL.

Wouldn’t it be easier if the URL was output in the main list? I posted some code here a while ago to do this. But what if you want to make the URLs for all the different sizes of images in the Media Library available? I thought I’d revamp my code to tackle this and post it afresh here.

Read more »

WordPress hacks and tips: Media

First off, hi to anyone who’s been reading my occasional posts here. I’ve just redesigned my site and I’m hoping to blog here a little more regularly. I’ve opened up the comments, so please say hello; I’m especially interested in anyone else working on similar things in WordPress who has ideas to add to my own.

I’m going to post a series of collections of code snippets and tips from my recent work with WP. I’ll try and group them together logically, though I’ll certainly end up with a “bits and bobs” post. I’ll also try and keep each post updated as I learn new stuff.

Many of the tips come from my current habit, when developing custom WP themes for clients, of minimizing plugins. If I can add a bit of code to functions.php rather than install a plugin, I’ll often do the former. Plugins are of course fantastic for more complex modifications, or for offering specific hacks to people who aren’t comfortable editing their theme’s PHP. But if neither of these apply, and the modification is very simple, it seems that it’s worth just slipping the code into your custom theme. Many of these snippets are just included in my default theme “skeleton”, which I edit as necessary for each project.

I should also stress that not all of these are results of my own ingenuity. I’ll credit people where possible, but often I’ve forgotten the source of the code.

OK, without further ado, here’s some media-related stuff.

Read more »

Complete archive

Main index