Code snippets

This is the archive for Code Snippets - little bits of PHP to do interesting things in WordPress. By their very nature, Code Snippets tend to be short, sharp and to the point, and deal with one specific issue. More detailed tutorials and articles covering web site development using WordPress are filed under Web Development.

WordPress – List all image sizes in Media Uploader

Ducati image

A handy code snippet to show how to use the new WordPress 3.3 image_size_names_choose filter to display all image sizes (ie those created by add_image_size() as well as the default image sizes) in the Media Uploader.

Remove WordPress default media image sizes

Ducati image

A follow up to my add_image_size() demo article, showing you how to stop WordPress from automatically creating all or any of the default image sizes (thumbnail, medium, large) during the media upload process.

WordPress $current_screen variable for admin screens

More fun with comments

The $current_screen global variable contains information useful for plugin and theme authors to find out which WordPress file is generating the current admin screen. This article gives an overview of the $current_screen variable and how to access it.

Customising the_excerpt

iStock_000001952705XSmall

How to add “read more” links and change the length of the text generated by the_excerpt() Wordpress function. Examples for both Auto and Manual excerpts.

WordPress 3.0 action hook contextual_help

iStock_000001952705XSmall

Here’s an update to the previous article on adding contextual help to Wordpress plugin options/settings pages, which describes how to maintain compatibility with the new $screen object in Wordpress 3.0.

Dynamic category menu highlighting for single posts

More fun with comments

A tutorial to show how to implement dynamic menu highlighting of Wordpress category menus when viewing single post pages.

Limiting WordPress Post Revisions

Perhaps more glamourous than Post Revisions?

How to limit the number of Post Revisions automatically saved by Wordpress, or suppress them altogether, together with how to delete existing Post Revisions from the Wordpress database.

WordPress action hook contextual_help

iStock_000001952705XSmall

How to use the Wordpress Action hook “contextual_help” to add useful Help or other notes to a plugin’s Settings page. Although of greatest use for plugin developers, it could also be used by theme developers to add additional Help info to customised Post/Page Edit screens.

WordPress Editor – disabling wpautop()

How to disable wpautop() and take control of your paragraph and line-break tags when using the Wordpress Post/Page Editor.

Open comment author link in new window

More fun with comments

How to modify the Wordpress comment author link to open the link in a new window. A short tutorial.