PHP Tutorials Category Archive

PHP: Stepping Through Arrays by Curtiss - June 19, 2011

I’ve posted a few articles about working with arrays in PHP over the past few years. I have posted information about adding and replacing elements in arrays; searching for items in arrays; and even a general post about handy array-related functions in PHP. I’m back again with a few more handy functions.

In PHP, you can obviously loop through arrays pretty easily by using a foreach() loop, but did you know you can actually step through arrays manually? PHP offers a handful of functions to do just that. Let’s take a look at those functions, and how you might be able to use them. (more…)

WordPress: Adding Some Class to Meta Boxes by Curtiss - June 14, 2011

For WordPress plugin developers, the metabox API generally proves to be invaluable. Meta boxes allow you to organize various bits of information, groups of options and more into nice, attractive, collapsible boxes rather easily. What’s more, these boxes can be dragged around the screen and reorganized without much hassle. However, one thing that you can’t do with meta boxes is to assign additional CSS classes to them.

However, in WordPress 3.2, you will be able to do just that. WordPress 3.2 will introduce a brand new filter that allows you to modify the list of classes that are applied to a meta box. In order to use it, you’ll need to know the slug of the page on which the meta box is being displayed and the ID of the meta box. (more…)

Adding Settings Fields to Your WordPress Plugin by Curtiss - May 9, 2011

When developing a plugin for WordPress, a lot of times you’ll want to create your own page of options within the administration area. To do so, you’ll usually use the add_options_page() or add_submenu_page() function to actually create the page; but then you’ll have to populate that page with the actual settings fields.

The first step in that process is to understand the add_settings_field() function. This function accepts 6 different parameters. They are as follows: (more…)

Using jQuery in your WordPress plugins by Curtiss - May 3, 2011

This evening on Twitter, @viper007Bond posted a quick tip about using jQuery in your WordPress plugins (also applicable to themes). His initial tweet was:

Using jQuery in your WordPress plugin? Make sure you’re using quotes in your selector strings! http://api.jquery.com/category/selectors/

Then, @dimensionmedia, @viper007Bond and I had the following brief conversation: (more…)

WordPress: Hooking Into The Upload Action by Curtiss - April 8, 2011

While WordPress implements a really nice asynchronous upload function, it doesn’t really offer any simple way to manipulate the files before they’re actually stored in your uploads folder. There are multiple filters you can hook into after the file’s been uploaded and processed; but there aren’t any filters available to do anything with the file beforehand. (more…)

WordPress: Optional Widget Areas by Curtiss - April 3, 2011

When developing a new WordPress theme, sometimes you might need to create optional widget areas within the template; that is, areas that can include widgets if the user wants, but don’t appear at all if the user has not added any widgets to that particular area.

For instance, some users of your theme might want to include a tag cloud above the footer; or maybe they want to include Google AdSense ads above the content. Other users of your theme might not want anything to appear in those areas, though.

So how do you create an optional widget area in a WordPress theme? The simple answer is, you use the is_active_sidebar() function. (more…)

WordPress: Checkbox, Radio and Select Helper Functions by Curtiss - March 29, 2011

As you may or may not know, WordPress has two simple helper functions built into it that make it easy to determine whether a checkbox/radio button should be checked or a select option should be selected. (more…)

WordPress: Adding a Proper Visual Editor to Your Plugin by Curtiss - March 24, 2011

When developing a new plugin for WordPress, sometimes you want to add a visual/WYSIWYG editor to one of your plugin’s settings fields. Unfortunately, most of the tutorials you’ll find online only explain part of what needs to be done in order to get that working. The main problem I have encountered when looking at these tutorials and example plugins is the fact that they only invoke the visual editor; they don’t offer any way for the user to use the editor in HTML mode. (more…)

WordPress: Storing Temporary Information by Curtiss - March 9, 2011

When developing plugins for WordPress, most of the time I deal with semi-permanent settings with my plugins. A user goes to the “Settings” page for the plugin, they set things up the way they want them, and they expect those settings to remain that way until they decide to change them again.

However, there are times when you need to store temporary information that needs to either expire or be updated on a regular basis. There are functions within WordPress to help you with that, too. They are part of the “transient API” in WordPress. Basically, transient options are options that have an expiration date. (more…)

Using Google’s CDN for WordPress JavaScript by Curtiss - February 27, 2011

As you probably know by now, Google hosts most of the major JavaScript libraries on its own content distribution/delivery network (CDN) for everyone to use. However, WordPress actually comes bundled with many of the same JavaScript libraries. So, what are you to do when you want to use Google’s copy? Sure, you could simply include the call to the Google JavaScript library of your choice in your theme files, but that would cause the library to load twice in many cases (potentially causing conflicts all over the place).

The way to handle this, quite simply, is to tell WordPress not to use its local copy of the library; but to use Google’s copy instead. To do so, you simply “deregister” the WordPress copy (for these examples, I will be showing how to use Google’s jQuery library), then register (and potentially enqueue) the Google copy. (more…)