Code from wordpress plugin presentation


A few weeks ago I gave a presentation on plugin development for the Northampton Web Developers’ WordPress group. At the end of my slides, I went through a simple plugin to ‘like’ posts. Below is the code I used, with some notes.

<?php
/**
 * Plugin Name: Like Post
 * Plugin URI: http://redradar.net/
 * Description: Add a 'like' button to your posts to find the most popular posts & list the top on the sidebar/wherever.
 * Author: Kelly Dwan
 * Version: 0.1
 * Date: 11.03.10
 * Author URI: http://www.redradar.net/
 */

/**
 * Add text domain to enable localization & register options validation.
 */
function lp_add_domain(){
    load_plugin_textdomain('lp_domain');
    register_setting( 'like_post_optgroup', 'lp_options', 'lp_validate' );
} add_action('init','lp_add_domain');

/**
 * Sanitize and validate input.
 * @param array $input an array to sanitize
 * @return array a valid array.
 */
function lp_validate($input) {
    $input['text'] = wp_filter_nohtml_kses($input['text']);
    $input['show_count'] = ($input['show_count'] ? 1 : 0);
    return $input;
}

/**
 * Add javascript and css to header files.
 */
function lp_includes(){
    wp_enqueue_script( 'lp-script', plugins_url("lp-script.js",__FILE__), array('jquery') );
    wp_enqueue_style( 'lp-style',  plugins_url("lp-style.css",__FILE__) );
} add_action('get_header','lp_includes');

/**
 * Add options to databases with defaults
 */
function show_lp_settings() {
    add_options_page('Like Post', 'Like Post Settings', 'manage_options', 'lp', 'lp_settings');
    if (!get_option("lp_options")) {
        $lp_options['text'] = 'like this post';
        $lp_options['show_count'] = 1;
        add_option ("lp_options", $lp_options, '', 'no') ;
    }
} add_action('admin_menu','show_lp_settings');

/**
 * Set up settings panel
 */
function lp_settings(){ ?>
    <div class="wrap lp">
		<h2><?php _e('Like Post Settings','lp_domain'); ?></h2>

		<form method="post" action="options.php" id="options">
			<?php settings_fields('like_post_optgroup'); ?>
			<?php $lp_options = get_option('lp_options'); ?>

			<table class="form-table">
				<tr><td><?php _e('What do you want do display as the button text?','lp_domain');?></td><td><input type='text' name='lp_options[text]' value='<?php echo $lp_options['text'];?>' /></td></tr>
                <tr><td><?php _e('Show the current like count','lp_domain');?></td><td><select name='lp_options[show_count]'><option value='1' <?php echo ($lp_options['show_count'])?'selected':'';?>><?php _e('Yes','lp_domain');?></option><option value='0' <?php echo ($lp_options['show_count'])?'':'selected';?>><?php _e('No','lp_domain');?></option></select></td></tr>
            </table>

            <p class="submit">
				<input type="submit" name="Submit" value="<?php _e('Save Changes','lp_domain') ?>" />
			</p>
		</form>
    </div>
<? }

/**
 * Output button to 'like' the post.
 */
function like_post() {
	$lp_options = get_option('lp_options');
    $nonce= wp_create_nonce('like-post');
?>
<div class="like-post">
    <form action="<?php echo plugins_url('lp-submit.php', __FILE__); ?>" method="post">
        <input type="hidden" name="lp_nonce" value="<?php echo $nonce; ?>" />
        <input type="hidden" name="lp_id" value="<?php global $id; echo $id; ?>" />
        <input type="submit" name="like-post" value="<?php echo $lp_options['text']; ?>" />
    </form>
    <?php $c = get_post_meta($id,'_lp_count',true); ?>
    <span class="like-count">
    <?php if ( ($lp_options['show_count']) && ("" != $c) ): ?>
        <?php echo $c; ?> <?php echo _n('like', 'likes', $c, 'lp_domain'); ?>
    <?php endif; ?>
    </span>
    <div style="clear:both"></div>
</div>
<? } add_action('the_post','like_post');
     //Add button to end of posts.

/**
 * Returns an unordered list ('ul > li > a') of the most popular items
 * [for our purposes, 'most popular' just means 'has more than 20 likes']
 */
function lp_popular(){
    $posts = get_posts('orderby=meta_value_num&meta_key=_lp_count&post_type=any');
    $out = "<ul id='popular_posts' >";
    $out .= "<li><h3>Popular Posts</h3></li>";
    foreach ($posts as $post)
        $out .= "<li><a href='". get_permalink($post->ID) ."'>". $post->post_name ."</a> (". get_post_meta($post->ID,'_lp_count',true) .")</li>";
    $out .= "</ul>";
    echo $out;
}
?>

There are plenty of resources around the web, a proper google search will probably turn up something useful. If you want someplace to start from, the WP Codex has a number of guides for different aspects of plugin/theme/etc development. Below are a few that I used in compiling my presentation (all of the links in the presentation are here, plus a few more).

The codex also has a search function & most common functions have their own page – like get_posts() and get_user_meta().

I also mis-attributed the ‘unit testing plugins’ presentation I saw at WordCamp NYC in ’09 – the presentation doesn’t appear to be online anywhere, but the software, MockPress, can be found on github.

Comment with your links or questions about developing plugins!

EDIT: Shortly after posting this I learned two things. First, I noticed that the action I’m hooking into to print the “like” button also displays the button on the Posts listing in the admin side – so that was the wrong hook to use. That’s not hard to change, though, just find a new action and hook into that.

Second, I found out that you can do orderby=meta_value_num to numerically sort posts in query_posts/get_posts – so the problem I mentioned at the presentation (that my ‘popular posts’ function was not sorting correctly) can be fixed (and has, in the code above – previously it said $posts = get_posts('orderby=meta_value&meta_key=_lp_count&post_type=any');, which sorts alphabetically.)

Posted in:

Previous:
Next:

Comments

No comments.