Categories
Development PHP Plugins Themes WordPress

New in WordPress 3.6: the attachment_url_to_postid() Function

If you’re writing a WordPress plugin, you may have a scenario where you have an attachment (file) URL, and you’d like to obtain the associated post (attachment) ID.

Luckily, this is very easy to do in WordPress 3.6 because of the new attachment_url_to_postid() function.

The function’s syntax is simple – you pass it a string (the file URL), and it returns an integer (the associated post id), or zero on failure.

Here’s a simple example:

As mentioned above, this function is available in WordPress 3.6 – it will cause a fatal error if you try to use it in any earlier versions such as WordPress 3.5.x.

Thanks to Zack Tollman for the heads up about this handy new function.

Categories
Development PHP Plugins Themes WordPress

Better Mobile Detection with WordPress’ wp_is_mobile() Function

Although WordPress themes typically use Responsive Web Design to tailor website for mobile devices, there is sometimes a need to use PHP (backend code) to detect whether the visitor’s web browser is running on a mobile device.

For example, there may be scenarios where you may want to output certain HTML markup (such as a mobile navigation menu) on mobile devices.

Alternatively, you may only want to output a slider containing large images on desktop and not mobile devices. Doing this via PHP (instead of CSS) means that the visitor’s mobile browser won’t have to download all of the slider images even though the slider is never displayed.

WordPress’ wp_is_mobile() function to the rescue!

wp_is_mobile() is a litte-known function built into WordPress that detects whether the visitor is using a mobile devices such as iPhone, iPad, Android, Silk, Kindle, BlackBerry, Opera Mini, and Opera Mobi.

The function was introduced in WordPress 3.4, and it can be used in a WordPress plugin or theme.

It’s a simple function that accepts no parameters, and returns a simple boolean (true/false) value.

Here’s a simple example:

Interestingly, WordPress core currently uses this function in a few different places:

  • To completely disable the Visual Editor for Opera Mini.
  • To enable jQuery UI Touch Punch in the WordPress dashboard for mobile devices.
  • To detect whether the current device can upload files.
  • To disable the “shake” effect on the WordPress login page when an incorrect username or password is entered using a mobile device.

Are you currently using the wp_is_mobile() function in your plugin or theme? If so, I’d love to hear what your use-case is.

Categories
Themes WordPress

Twenty Twelve theme added to WordPress 3.4-alpha


WordPress 3.4 will have a brand new theme in it – Twenty Twelve.

The initial version of which, has just been added to the development version of WordPress. The commit message explains the philosophy of Twenty Twelve:

Initial import of the Twenty Twelve theme, by Drew Strojny. This time around we’re trying something different than the previous twenty-something themes, a much more minimalist approach that affords easy use as a CMS in addition to being blog-forward. More information will be on wpdevel soon. Props drewstrojny and lancewillett.

Source: Trac Ticket #19978

I have set up a Twenty Twelve Demo Site here: http://twentytwelve.jamesc.id.au/

I’m very interested to see how it pans out!

Update 7/2/2012: The Twenty Twelve Development Team has posted an update:

This is not yet a fully working theme — we’ll be adding in more features and lots of missing styles over the next 3–4 weeks. Including post formats, comments, archives and page templates, more in-post styling, and a nav menu rework so the main content comes ahead of the navigation.

Update 2/3/2012: The Twenty Twelve theme has been delayed, and will no longer be part of WordPress 3.4. It will be in WordPress 3.5 (scheduled for later in 2012).

Categories
Themes WordPress

Time For Change (of theme)

Today Automattic released the brand new Duster WordPress Theme.

Being a technical guy, I typically don’t care much for WordPress themes.

However I haven’t been happy with how my website has been looking so I decided to give it a try.

The showcase template is really cool (I’m using it for the home page), and I also really like the simplistic layout of the individual post and archive pages.

Anyway, enough talking about pretty things. Time to dig back into some code!