Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 7 • Making the Most of WordPress Plugins 197


First, create the actual plugin, which will sit in a folder called addaff. The main plugin file is
called addaff.php and has this header:


<?php
/
Plugin Name: AddAff
Plugin URI: http://tdh.me/wordpress/addaff/
Description: A global affiliate link setting with post specific links.
Version: 1.0
Author: Thord Daniel Hedengren
Author URI: http://tdh.me/
/


Continue on by creating the actual settings page for the plugin:


/ SETTINGS PAGE
Create the plugin settings page
/


// Initiate addaff_init() in admin
add_action( 'admin_init', 'addaff_init' );


// Add the settings page function to the menu
add_action( 'admin_menu', 'addaff_add_page' );


// Whitelist the settings
function addaff_init(){
register_setting( 'addaff_options', 'addaff_option', 'addaff_validate' );
}


// Add to the menu
function addaff_add_page() {
add_options_page( 'AddAff Settings', 'AddAff', 'manage_options', 'addaff',
'addaff_do_page' );
}


You’ll no doubt recognize this from the previous example, so let’s push through to the actual
contents of the settings page. You’ll use addaff_option to store the content supplied. This
will be stored in an array, which you’ll have to remember when you’re pulling it out for use
when building the links later on.


The actual settings page is pretty simple — just two input fields and some descriptive text. If you
feel you’re unsure what this is, refer to the “Plugin Settings” subsection earlier in this chapter.


// Create the actual settings page
function addaff_do_page() {
?>



AddAff Settings


AddAff helps you easily manage product links and add affiliate
parameters to them. Most affiliate programs want you to provide a string
before and sometimes after the actual product link.

Free download pdf