Ian Hoar – Passion for Technology – Geeking Out - Technology, Web, Toys, Games, Design, Entertainment, Gadgets, & Geeking Out

Using WordPress custom fields for toggling plugins

WordPressMore and more I find myself using WordPress as a CMS. This requires a little more customization than you would need for the average out of the box blog experience but WordPress can make a wonderful little CMS.  Custom fields are just one way to gain finer control over your pages and posts both for blog or CMS functionality.

One of the sites I work on that uses WordPress as a CMS needed a share this article with a friend link. I needed this link to appear on some of the pages, but not all of them. I used Lester Chan’s WP-Email; this is a very powerful plugin that allows you to set up an HTML email which can be personalized with the users information and then sent to their friends.

The plugin allows you to embed the email link either in the template or the actual post. Unfortunately I needed something more flexible than that. I needed the share this email with a friend link to appear at the top and bottom of select pages and in a specific place. This is where custom fields come in.

Custom fields can be set up in any post or page through the WordPress admin. You will find the option below the editor. I set up a custom field called share with a value of true. Then in the template I used the following PHP code within the WordPress loop to call the email_link() function.

if (get_post_meta($post->ID, 'share', true)) {
	if(function_exists('wp_email')) { email_link(); }
}

This code checks if share is set to true for this specific post or page ID. If true it then checks if wp_email function exists and if so runs the plugin function email_link. This is a great way to toggle plugins or any chunk of code for select pages in WordPress. Now all I have to do when I want a share this page with a friend link to show up is set the share value to true in the custom field for share.

More on Custom Fields in the WordPress Codex.

Comments are closed.