Archive

Archive for the ‘Wordpress’ Category

Fun with WordPress Plugins and Pages

April 17th, 2009 Chris Hane No comments

For one of my clients I have written a wordpress plugin that can sit in the widget sidebar.  It allows a viewer of their blog to register to an email list in the Eureka! application.  The email list to subscribe to is set on a global basis for the entire blog.

I had a feature request that the email list should depend on the post/page being viewed.  It turns out this is simple – but really hard to figure out. There is a lot of documentation on wordpress, just some of the probably basics are not readily apparent.

So I opened google and started digging.  Turns out, there must not be a lot of demand for knowing which post/page someone is viewing in a widget plugin.  After a couple of hours of digging I finally figured out:

  1. I can access a global wordpress object that will contain lots of good information
  2. Custom fields can be assigned to a post/page

The relevant snippet of code is:

1
2
3
global $wp_query;
$postid = $wp_query->post->ID;
$customFieldValue = get_post_meta($postid, 'customFieldKey', true);

And volia, I now have the custom field value based on the post/page someone is viewing.

 
Categories: Wordpress