202 PART III • Using Plugins with WordPress
// Store _addaff_meta_box_url in $addaff_link_url
$addaff_link_url = get_post_meta( get_the_ID(), '_addaff_meta_box_url', true );
// Check if BOTH $addaff_link_title and $addaff_link_url have content
if ( $addaff_link_title && $addaff_link_url != '' ) {
// Get the addaff_option settings
$adaff_url = get_option( 'addaff_option' );
// Build the markup and add to $content
$content .= '<ul id="addaff"><li class="addaff-item"><a href="';
$content .= $adaff_url[urlbefore];
$content .= $addaff_link_url;
$content .= $adaff_url[urlafter];
$content .= '">';
$content .= $addaff_link_title;
$content .= '</a></li></ul>';
// Return the content
return $content;
}
// Nope, both $addaff_link_title and $addaff_link_url didn't have content
else {
// Return the content
return $content;
}
}
The first thing you’re doing is storing the post meta from the post, should there be any, by
using get_post_meta() and pointing it to _addaff_meta_box_title and _addaff_
meta_box_url. You’ll remember these from when you created the meta boxes; these
contain the affiliate link title and the product URL. They are now in $addaff_link_title
and $addaff_link_url.
Should both the affiliate link title and URL be filled in, you need to do a conditional check to
see if both of those are NOT returning empty. && is a PHP parameter that means AND, so
both need to be not empty.
Because you need to do things with the settings saved on your settings page, you’ll put the
contents of addaff_option into $adaff_url, using get_option().
Let’s stack everything together. This part is actually building the whole markup, including the
strings saved on the settings page, as well as the post meta data. By using .=, you’ll link each
of the following lines together, building the affiliate link and markup you need:
// Build the markup and add to $content
$content .= '<ul id="addaff"><li class="addaff-item"><a href="';