Boston WordPress Meetup Example Code
As promised here’s the example code from the meetup I gave last Monday. It’s fairly small, so I only describe what it does briefly in the top comment. Feel free to use it how you wish (though if you make a lot of money off of it you owe me a t-shirt):
Example 1: Erroring out on install
register_activation_hook(__FILE__, 'bostonwp_install_error');
if('error_scrape' == $_GET['action'] && basename($_SERVER['PHP_SELF']) == 'plugins.php') {
echo '<pre>' . get_option('bostonwp_activate_error') . '</pre>';
delete_option('bostonwp_activate_error');
deactivate_plugins($_GET['plugin']);
die();
}
function bostonwp_install_error($error = false) {
if(!$error) {
$error = 'Generic Error Occurred';
}
add_option('bostonwp_activate_error', $error);
//trigger a fatal error
trigger_error('', E_USER_ERROR);
}
Example 2: Updating your DB structure
// http://codex.wordpress.org/Creating_Tables_with_Plugins
define('BOSTONWP_DB_VERSION', '1.0');
register_activation_hook(__FILE__, 'bostonwp_install');
$wpdb->bostonwp = $wpdb->prefix . 'bostonwp';
function bostonwp_install() {
global $wpdb;
$installed_ver = get_option( 'bostonwp_db_version' );
$sql = 'CREATE TABLE ' . $wpdb->bostonwp . ' (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time bigint(11) DEFAULT 0 NOT NULL,
name tinytext NOT NULL,
text text NOT NULL,
url VARCHAR(55) NOT NULL,
UNIQUE KEY id (id)
);';
if($wpdb->get_var('show tables like "' . $wpdb->bostonwp . '"') != $wpdb->bostonwp) {
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
$welcome_name = 'Mr. WordPress';
$welcome_text = 'Congratulations, you just completed the installation!';
$insert = 'INSERT INTO ' . $wpdb->bostonwp .
' (time, name, text) ' .
'VALUES (' . time() . ',"' . $wpdb->escape($welcome_name) . '","' . $wpdb->escape($welcome_text) . '")"';
$results = $wpdb->query( $insert );
add_option("bostonwp_db_version", BOSTONWP_DB_VERSION);
}
elseif( $installed_ver != BOSTONWP_DB_VERSION ) {
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
update_option( "bostonwp_db_version", BOSTONWP_DB_VERSION );
}
}
Example 3: Adding admin menus
// http://codex.wordpress.org/Adding_Administration_Menus
add_action('admin_menu', 'bostonwp_menu');
function bostonwp_menu() {
add_posts_page('My Page', 'My Page', 'manage_categories', 'bostonwp_page', 'bostonwp_dostuff');
}
function bostonwp_dostuff() {
?>
<div class="wrap">
<?php if(function_exists('screen_icon')) screen_icon(); ?>
<h2>My Boston WP Page</h2>
</div>
<?php
}
Example 4: Adding meta boxes to posts
// http://www.deluxeblogtips.com/2010/04/how-to-create-meta-box-wordpress-post.html
add_action('add_meta_boxes_post', 'bostonwp_box');
add_action('save_post', 'bostonwp_save');
add_action('delete_post', 'bostonwp_delete');
function bostonwp_box($post) {
// http://codex.wordpress.org/Function_Reference/add_meta_box
add_meta_box('bostonwp', __('Boston WP'), 'bostonwp_meta_box', 'post', 'side');
}
function bostonwp_meta_box() {
global $post;
$val = get_post_meta($post->ID, 'bostonwp', true);
// Use nonce for verification
echo '<input type="hidden" name="bostonwp_nonce" id="bostonwp_nonce" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// The actual fields for data entry
echo '<input type="checkbox" name="bostonwp" value="1" ' . ($val ? ' checked="checked" ' : '') . ' />';
echo '<label for="bostonwp">' . __('There Is Cake!') . '</label> ';
}
function bostonwp_save($id) {
// verify nonce
if ($_POST['action'] != 'inline-save' && !wp_verify_nonce($_POST['bostonwp_nonce'], plugin_basename(__FILE__))) {
return $id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $id;
}
update_post_meta($id, 'bostonwp', $_POST['bostonwp']);
}
function bostonwp_delete($id) {
delete_post_meta($id, 'bostonwp');
}
Example 5: Adding columns to post list
add_filter('manage_posts_columns', 'bostonwp_posts_column');
add_action('manage_posts_custom_column', 'bostonwp_manage_column', 10, 2);
function bostonwp_posts_column($cols) {
$cols['bostonwp'] = __('Boston WP');
return $cols;
}
function bostonwp_manage_column($name, $id) {
if($name != 'bostonwp') {
return;
}
$cake = get_post_meta($id, 'bostonwp', true);
if($cake) {
echo 'There is cake!';
$cake = 1;
}
else {
echo 'The cake is a lie!';
$cake = 0;
}
echo '<div id="inline_' . $id . '_bostonwp" class="hidden">' . $cake . '</div>';
}
Example 6: Expanding Quick Edit
add_action('quick_edit_custom_box', 'bostonwp_posts_custom_box', 10, 2);
add_action('admin_head-edit.php', 'bostonwp_add_script');
function bostonwp_posts_custom_box($col, $type) {
if($col != 'bostonwp' || $type != 'post') {
return;
}
?>
<fieldset class="inline-edit-col-right"><div class="inline-edit-col">
<div class="inline-edit-group">
<label class="alignleft">
<input type="checkbox" value="1" name="bostonwp" id="bostonwp_check">
<span class="checkbox-title">This Post Has Cake</span>
</label>
</div>
</fieldset>
<?php
}
function bostonwp_add_script() {
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('a.editinline').live('click', function() {
var id = inlineEditPost.getId(this);
var val = parseInt(jQuery('#inline_' + id + '_bostonwp').text());
jQuery('#bostonwp_check').attr('checked', !!val);
});
});
</script>
<?php
}
Recent Comments