Wrapping Up

Once you've completed all your functions, activate your plugin and try it out! Make sure there is no extra white space after your closing ?> tag, or WordPress will give an error on activation.

Listing 9-16 shows the completed Next Page plugin and Figure 9-6 is a screenshot of the plugin.

Listing 9-16. The complete Next Page plugin

Plugin Name: Next Page

Plugin URI: http://sillybean.net/code/wordpress/next-page/

Description: Provides shortcodes and template tags for next/previous navigation in pages.

Version: 1.4

License: GPLv2

Author: Stephanie Leary

Author URI: http://sillybean.net/

add_action('admin_menu', 'next_page_add_pages');

register_activation_hook(_FILE_, 'next_page_activation');

function next_page_activation() { // set defaults $options = array();

$options['before_prev_link'] = '<div class="alignleft">';

$options['prev_link_text'] = _('Previous:', 'next-page').' %title%';

$options['before_parent_link'] = '<div class="aligncenter">';

$options['parent_link_text'] = _('Up one level:', 'next-page').' %title%';

$options['before_next_link'] = '<div class="alignright">'; $options['next_link_text'] = __('Next:', 'next-page').' %title%'; $options['after_next_link'] = '</div>';

// set new option add_option('next_page', array_merge($oldoptions, $options), '', 'yes');

// when uninstalled, remove option register_uninstall_hook( _FILE_, 'next_page_delete_options' );

function next_page_delete_options() { delete_option('next_page');

define('WP_PLUGIN_DIR', dirname(dirname(__FILE__))); $lang_dir = basename(dirname(_FILE_)). '/languages';

load_plugin_textdomain( 'next_page', 'WP_PLUGIN_DIR'.$lang_dir, $lang_dir );

add_action('admin_init', 'register_next_page_options' ); function register_next_page_options(){

function next_page_add_pages() {

// Add a new submenu under Options:

$css = add_options_page('Next Page', 'Next Page', 'manage_options', 'next-page', 'next_page_options');

add_action("admin_head-$css", 'next_page_css');

function next_page_css() { ?> <style type="text/css">

#next-page, #parent-page, #previous-page { float: left; width: 30%; margin-right: 5%; }

// displays the options page content function next_page_options() { ?> <div class="wrap">

<form method="post" id="next_page_form" action="options.php"> <?php settings_fields('next_page'); $options = get_option('next_page'); ?>

<h2><?php _e( 'Next Page Options', 'next-page'); ?></h2>

<p><label><?php _e("Exclude pages: ", 'next-page'); ?><br /> <input type="text" name="next_page[exclude]" id="exclude"

value="<?php echo $options['exclude']; ?>" /><br /> <small><?php _e("Enter page IDs separated by commas.", 'next-page'); ?></small></label></p>

<div id="previous-page">

<h3><?php _e("Previous Page Display:", 'next-page'); ?></h3> <p><label><?php _e("Before previous page link: ", 'next-page'); ?><br /> <input type="text" name="next_page[before_prev_link]" id="before_prev_link"

value="<?php echo stripslashes(htmlentities($options['before_prev_link'])); ?>" /> </label></p>

<p><label><?php _e("Previous page link text: <small>Use %title% for the page title</small>", 'next-page'); ?><br />

<input type="text" name="next_page[prev_link_text]" id="prev_link_text"

value="<?php echo stripslashes(htmlentities($options['prev_link_text'])); ?>"

<p><label><?php _e("After previous page link: ", 'next-page'); ?><br /> <input type="text" name="next_page[after_prev_link]" id="after_prev_link"

value="<?php echo stripslashes(htmlentities($options['after_prev_link'])); ?>" /> </label></p>

<p><?php _e('Shortcode:'); ?> <strong>[previous]</strong><br />

<?php _e('Template tag:'); ?> <strong>&lt;?php previous_link(); ?&gt;</strong></p>

<div id="parent-page">

<h3><?php _e("Parent Page Display:", 'next-page'); ?></h3>

<p><label><?php _e("Before parent page link: ", 'next-page'); ?><br />

<input type="text" name="next_page[before_parent_link]" id="before_parent_link"

value="<?php echo stripslashes(htmlentities($options['before_parent_link'])); ?>" /> </label></p>

<p><label><?php _e("Parent page link text: <small>Use %title% for the page title</small>", 'next-page'); ?><br />

<input type="text" name="next_page[parent_link_text]" id="parent_link_text"

value="<?php echo stripslashes(htmlentities($options['parent_link_text'])); ?>" /> </label></p>

<p><label><?php _e("After parent page link: ", 'next-page'); ?><br /> <input type="text" name="next_page[after_parent_link]" id="after_parent_link"

value="<?php echo stripslashes(htmlentities($options['after_parent_link'])); ?>" /> </label></p>

<p><?php _e('Shortcode:'); ?> <strong>[parent]</strong><br />

<?php _e('Template tag:'); ?> <strong>&lt;?php parent_link(); ?&gt;</strong></p>

<h3><?php _e("Next Page Display:", 'next-page'); ?></h3> <p><label><?php _e("Before next page link: ", 'next-page'); ?><br /> <input type="text" name="next_page[before_next_link]" id="before_next_link"

value="<?php echo stripslashes(htmlentities($options['before_next_link'])); ?>" /> </label></p>

<p><label><?php _e("Next page link text: <small>Use %title% for the page title</small>", 'next-page'); ?><br />

<input type="text" name="next_page[next_link_text]" id="next_link_text"

value="<?php echo stripslashes(htmlentities($options['next_link_text'])); ?>"

<p><label><?php _e("After next page link: ", 'next-page'); ?><br /> <input type="text" name="next_page[after_next_link]" id="after_next_link"

value="<?php echo stripslashes(htmlentities($options['after_next_link'])); ?>"

<p><?php _e('Shortcode:'); ?> <strong>[next]</strong><br />

<?php _e('Template tag:'); ?> <strong>&lt;?php next_link(); ?&gt;</strong></p>

<p class="submit">

<input type="submit" name="submit" class="button-primary" value="<?php _e('Update Options', 'next-page'); ?>" /> </p> </form> </div>

} // end function next_page_options() // make the magic happen 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;

function next_link() { global $post;

$pagelist = next_page_flatten_page_list($exclude);

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

$linkurl = get_permalink($nextID);

$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;

function previous_link() { global $post;

$pagelist = next_page_flatten_page_list($exclude);

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

$linkurl = get_permalink($prevID);

$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;

function parent_link() { global $post;

$options = get_option('next_page'); $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;

Figure 9-6. The previous and next page links

Was this article helpful?

0 0
100 Backlinking Strategies

100 Backlinking Strategies

This ebook will give you 100 backlinking strategies. Backlinks can increase your traffic, sales and search engine ranking. You'll discover many different types of backlinks you could get and tactics you could use to acquire those those backlinks from other high PR web sites.

Get My Free Ebook

Post a comment