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

(avery) #1

CHAPTER 7 • Making the Most of WordPress Plugins 199


Figure 7-6 shows the finished settings page for the plugin. Sure, it could do with some lovin’,
but that’s just markup, so let’s not worry about that this time around.


Figure 7-6: The AddAff Settings page.


That’s it for the settings page. Now you can create the meta box!


CREATING THE ADDAFF META BOX


Every post will get a new meta box, which contains two input fields and some descriptive text.
The first field is for giving the affiliate link a title because although you could have had a global
variable for this, or pulled in the_title() or something, this is more versatile. The second
field contains the URL to the actual product, which is what you’ll be manipulating by adding
the before and after strings from the settings page when outputting the links.


First, begin creating the meta box:


/ META BOXES
Add the product link meta box to posts
/


// Create meta boxes
add_action( 'add_meta_boxes', 'addaff_meta' );
function addaff_meta() {
add_meta_box( 'addaff_meta_name', __( 'Affiliate Product Link', 'addaff' ),
'addaff_meta_box', 'post', 'normal', 'core' );
}


The meta box will end up in the left main area of the Edit Post interface to make sure that there is
ample room for the user to see what’s going on. The box is called Affiliate Product Link and shows
up only on posts, below the core fields. If you need a recap on meta boxes, refer to Chapter 6.

Free download pdf