Adding Your Own Scripts

When you need to add a JavaScript file to your theme, it's tempting to simply paste the < script> tag into header.php, dust off your hands, and move on to your next task. However, this is the wrong way to add scripts to a WordPress theme. You might recall from Chapter 3 that you can specify the location of the wp-content directory. If someone who has moved wp-content() tries to use this theme, the scripts will never load. Adding the < script> tags via the wp_head() action, as shown in...

Enabling Shortcodes in Text Widgets

As you saw in Chapter 4, shortcodes are bracketed placeholders you can use while editing your content that will be replaced with some other, more complicated content (like an embedded video or image gallery) when your post is displayed to visitors. Unfortunately, shortcodes are processed only in post page content. You can use them in text widgets, however, if you enable theme in the theme functions as shown in Listing 6-39. Listing 6-39. Enabling shortcode parsing in text widgets...

Custom Taxonomies for Custom Content Types

Let's say the department in question is the Honors office. This office tracks the Honors courses offered in each college. Before custom content types came along, they would use categories for each college, but those categories were mixed in with the general blog categories (news, scholarships) and the office's podcast. You can instead use a custom taxonomy for your colleges. Once you've created the taxonomies, you have to register them for the course content type. Listing 12-14 shows the...

Creating the Custom Fields

First, you have to modify your post type function to add a callback. This is the name of the function that will add (and or remove) custom meta boxes from the Edit screen. Listing 12-21 shows the change. Listing 12-21. Updating the post_type_courses() function to include callback argument function post_type_courses() register_post_type( 'course', array( 'singular_name' > _( 'Course' ), 'description' > _('Individual course data'), 'public' > true, 'show_ui' > true, 'register_meta_box_cb'...

Caching and Mobile Stylesheets

The final checkbox in the main set of Super Cache options turns on mobile support. This doesn't mean much unless you're also running a second plugin that provides a mobile version of your site. To demonstrate how this works, I'll walk you through setting up Super Cache to work with WPTouch, the plugin that provides mobile versions of all sites on wordpress.com. The WPTouch settings screen has a multitude of options. I won't discuss them all here the ones I'm interested in showing you are in the...

Using Categories and Tags as Keywords

If you need to add meta keywords to your single post archive template, you have all the data you need your categories and tags. All you have to do is get a combined list, separated by commas. In this example, you first use a conditional tag to make sure this code is used only on single post archives. (Pages don't have categories or tags, and the get_the_tags() and get_the_category() functions won't work correctly on archive pages containing multiple posts.) Then, for each tag and category, you...

Including Custom Content Types in Your Theme

To create a single archive page for a custom content type, all you have to do is create another file in your theme directory, single-type.php. However, there is no built-in way to create a dedicated archive for a custom content type. To work around this, you must use a page template. Listing 12-18 shows an example archive template for Twenty Ten. Save this to your theme directory, then create a new page called Course Archives and choose this file as its page template (Figure 12-20). Listing...

Installing Plugins

While WordPress includes most of the features you would want in a basic site, sooner or later you'll probably find that you want something more. Visit www.wordpress.org extend plugins to see all the things-over 9,000 -you can add to your site. You can download the plugin files and upload them to your wp-content plugins folder, just as you did with themes. However, there is also an automatic plugin installer. From your Dashboard, visit Plugins Add New. On this screen (Figure 2-15), you can...

Page Lists and Page Menus

The wp_list_pages function supports several parameters that allow you to change the way pages are listed. By default, all pages will be listed in alphabetical order. Listing 6-27 shows several alternatives. See for the full list of available parameters and their default settings. Listing 6-27. Using the wp_list_pages function lt -- all pages in alphabetical order -- gt lt ul gt lt php wp_list_pages gt lt ul gt lt -- all pages in menu order -- gt lt ul gt lt php gt lt ul gt lt -- to exclude a...

Adding the Gettext Domain

At this point, if you haven't already signed up for your Subversion repository, go ahead and do so. You won't have access to the automated tools until you've checked in the first copy of your plugin. If you don't want the plugin to be available to the public until the translation features are done, give the plugin file header a lower version number than the stable version listed in the readme file. Once you've checked in the first copy of your plugin, log in to Extend and visit your plugin's...

Advanced Link Attributes

The image address Figure 4-40 is the URL of any image you want to represent your link. You could use something small, like a favicon that could be displayed next to the link text in a typical blogroll list. At the other extreme, you could choose a large image, and customize your theme so that the images are shown and the titles appear only as alt or title attributes. Using this technique, you could turn your link manager into an image gallery and, by adding a little Javascript into the mix, you...

Tracking Down Performance Problems

If your site is slow, but it's not because of an inefficient permalink structure, tracking down the cause can be bewildering. What are some things that would cause performance problems Plugins that write a lot of records to the database, like Audit Trail, or anything that says it will slow things down, like the Super Cache option to clear the cache every time a post or page is saved. There are several tools you can use to identify the problem. First, you should make sure it's not just a...

Login Lockdown

The Login Lockdown plugin helps protect you from brute force password attacks by disabling the login function for a range of IP addresses alter several failed attempts in a short period of time, as shown in Figure 11-10. Using the default settings, it will lock you out for an hour after you've failed to enter the right password three times in five minutes. If you're the sort of person who continually forgets your password, this might not be the plugin for you However, since guessing at...

Loop for Each Category

Now that you've reset your query, it's time to create that magazine layout. There are various ways to accomplish this, depending on how you've set up your content. Perhaps you've created a hierarchy of pages, and you want to show those instead. For the moment, however, let's assume that you're using categories to segregate your posts into different departments, since this is the most common scenario. Once you see how this works, you should be able to adapt this code to your needs using some of...

Setting up Super Cache

Unlike most plugins, Super Cache doesn't start working as soon as you activate it. You have to configure it first. You'll see a red-outlined warning message on your plugin list until you set up caching or deactivate the plugin, as shown in Figure 11-2. Figure 11-2. WP Super Cache activation Figure 11-2. WP Super Cache activation Go to Settings Super Cache to configure the plugin Figure 11-3 . The first thing you have to decide is whether to run the plugin in Half On or full On mode. In Half On...

Making Room for Options

For a more complex example, let's look at the Next Page plugin, which provides three template tags. They print links to the previous, next, and parent pages of the currently viewed page. The corresponding functions are very similar to one another, but I'll show you all three so you can see the minor differences. There's a fourth function as well, a utility that's called by the previous_link and next_link functions. It gets a list of all pages, minus those explicitly excluded, and returns them...

Adding an Options Page

Almost every WordPress plugin involves some sort of option, and that means you'll need to create the form that lets users manage those options. If you went through the process of creating theme functions back in Chapter 7, the process for plugins will look very much the same. To add an options page to the main navigation menu, you need two functions. One will display the content of the options page, and the other is a container for the add_options_page function. Finally, you'll need to add your...

The Shortcode API

Creating shortcodes in WordPress is a matter of creating the function that prints out what you want, and then defining it as a shortcode. In the case of Next Page, the shortcodes are just alternatives to the template tags they do the same thing. Since that's the case, and there are no arguments to be dealt with, creating the shortcodes takes all of three lines, as shown in Listing 9-17. Listing 9-17. Creating the three Next Page shortcodes add_shortcode 'previous', 'previous_link' add_shortcode...

Why Word Press

WordPress is one of many PHP MySQL content management systems that allow content editors to use a web interface to maintain their sites instead of editing and uploading HTML files to a server. Some systems, like Movable Type and Textpattern, have reputations as good blogging platforms. Others such as Joomla, Drupal, and Expression Engine are more commonly associated with commercial or community sites. WordPress began as a blogging tool, but early on the developers added pages as a separate...