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 in a flat array.

Figure 9-2. Page IDs

To illustrate, Figure 9-2 shows a set of pages and their IDs (using the Simply Show IDs plugin). The flattened array would be (119,174,173,172,143,144,145), and the next/previous tags would display the pages in that order.

Listing 9-5 shows the Next Page template tag functions.

Listing 9-5. The three Next Page template tag functions

global $post; // exclude page IDs 1, 2, and 3 $exclude = array(1,2,3);

$pagelist = next_page_flatten_page_list($exclude); $current = array_search($post->ID, $pagelist); $nextID = $pagelist[$current+1];

$linkurl = get_permalink($nextID); $title = get_the_title($nextID); $after_link = '→';

$link = '<a href="' . $linkurl . '">' . $title . '</a>' . $after_link; echo $link;

// previous_link tag function previous_link() {

global $post; // exclude page IDs 1, 2, and 3 $exclude = array(1,2,3);

$pagelist = next_page_flatten_page_list($exclude); $current = array_search($post->ID, $pagelist); $prevID = $pagelist[$current-1];

$before_link = '&larr;'; $linkurl = get_permalink($prevID); $title = get_the_title($prevID);

$link = $before_link . '<a href="' . $linkurl . '">' . $title . '</a>'; echo $link;

// parent_link tag function parent_link() { global $post;

$parentID = $post->post_parent;

// exclude page IDs 1, 2, and 3 $exclude = array(1,2,3);

if (in_array($parentID, $exclude)) return false;

$linkurl = get_permalink($parentID); $title = get_the_title($parentID);

$link = '<a href="' . $linkurl . '">' . $title . '</a>'; echo $link;

// utility function function next_page_flatten_page_list($exclude = '') {

$args = 'sort_column=menu_order&sort_order=asc'; if (!empty($exclude)) $args .= '&exclude='.$exclude; $pagelist = get_pages($args); $mypages = array(); foreach ($pagelist as $thispage) { $mypages[] = $thispage->ID;

return $mypages;

Once you have the flattened array, it's easy to find the pages adjacent to the current one: you just increase or decrease the array key by one, grab the value, and use that page ID to build a permalink.

You could insert Listing 9-5 into your plugin file (next-page/next-page.php), activate it, and use next_link() and previous_link() in your templates. However, what if you wanted to use a character other than an arrow as part of the link text? What if you want to use "Next" and "Previous" in place of the page titles? You'd have to edit the plugin.

To allow users to change the linked text as well as the characters shown before or after the page titles, you need to add some options. Listing 9-6 shows the revised plugin code, using a single $options array in place of the hard-coded values used in Listing 9-5.

Listing 9-6. Using option variables

// next_link tag function next_link() { global $post;

$options = get_option('next_page'); $exclude = $options['exclude'];

$pagelist = next_page_flatten_page_list($exclude); $current = array_search($post->ID, $pagelist); $nextID = $pagelist[$current+l];

$before_link = stripslashes($options['before_next_link']);

$linkurl = get_permalink($nextID); $title = get_the_title($nextID); $linktext = $options['next link text']; if (strpos($linktext, '%title%') !== false)

$linktext = str_replace('%title%', $title, $linktext); $after_link = stripslashes($options['after_next_link']);

$link = $before_link . '<a href="' . $linkurl . title="' . $title . '">' . $linktext . '</a>' . $after_link;

echo $link;

// previous_link tag function previous_link() { global $post;

$options = get_option('next_page'); $exclude = $options['exclude'];

$pagelist = next_page_flatten_page_list($exclude); $current = array_search($post->ID, $pagelist); $prevID = $pagelist[$current-1];

$before_link = stripslashes($options['before_prev_link']);

$linkurl = get_permalink($prevID); $title = get_the_title($prevID); $linktext = $options['prev link text']; if (strpos($linktext, '%title%') !== false)

$linktext = str_replace('%title%', $title, $linktext); $after_link = stripslashes($options['after_prev_link']);

$link = $before_link . '<a href="' . $linkurl . '" title="' . $title . '">' . $linktext . '</a>' . $after_link;

echo $link;

// parent_link tag function parent_link() { global $post;

$parentID = $post->post_parent;

if (in_array($parentID, $exclude)) return false; else {

$before_link = stripslashes($options['before_parent_link']);

$linkurl = get_permalink($parentID); $title = get_the_title($parentID); $linktext = $options['parent link text']; if (strpos($linktext, '%title%') !== false)

$linktext = str_replace('%title%', $title, $linktext); $after_link = stripslashes($options['after_parent_link']);

$link = $before_link . '<a href="' . $linkurl . '" title="' . $title . '">' . $linktext . '</a>' . $after_link;

echo $link;

// utility function function next_page_flatten_page_list($exclude = '') {

$args = 'sort_column=menu_order&sort_order=asc'; if (!empty($exclude)) $args .= '&exclude='.$exclude; $pagelist = get_pages($args); $mypages = array(); foreach ($pagelist as $thispage) { $mypages[] = $thispage->ID;

return $mypages;

Now that you know how the user will be able to modify the template tags' output using options, you need to build the form that will let them save those options to the database.

0 0

Post a comment