WordPress Plugin Skeleton

Here’s a foundation for writing a WordPress plugin, based on version 3.2. This would go in the main my-plugin-name.php file.

/**
 * Plugin Name: Plugin Name
 * Plugin URI: http://authorname.com/plugin/
 * Description: Description of Plugin
 * Author: Author Name
 * Version: 1.0
 * Author URI: http://www.authorname.com
 */

/**
 * Copyright 2011 Author Name
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

define('MYPLUGIN_VERSION', '1.0');
define('MYPLUGIN_PLUGIN_URL', plugin_dir_url( __FILE__ ));

/* for plugin Settings in Admin */
function myplugin_admin() {
	include('myplugin-admin.php');
}

add_action('init', 'myplugin_init');
add_action('plugins_loaded', 'myplugin_do_something');
add_action('admin_menu', 'myplugin_admin_actions');
add_action('update_option_mypluginoption', 'myplugin_do_something_else');

function myplugin_admin_actions() {
     // add_options_page('My Plugin Options', 'My Plugin', 'manage_options', 'my-unique-identifier', 'my_plugin_options');
     add_options_page("My Plugin", "My Plugin", "manage_options", "My Plugin", "myplugin_admin");
}

function potd_init() {
	if (is_admin()) {
		wp_register_script('myplugin.js', MYPLUGIN_PLUGIN_URL . 'myplugin.js', array('jquery'));
		wp_enqueue_script('myplugin.js');
	}
}

function myplugin_install() {
	global $wpdb;

	// Create default options if needed
	add_option('myplugin_option', 'the option');

        // Create a table if necessary (only if can't use options or postmeta tables)
	$table_name = $wpdb->prefix . "myplugin";

	// Create myplugin table if not existing
	if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {

		$sql = "
			CREATE TABLE IF NOT EXISTS `" . $table_name . "` (
				`id` BIGINT(20) NOT NULL AUTO_INCREMENT  PRIMARY KEY,
				`field_one` VARCHAR(255) NOT NULL
			);";

		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
		dbDelta($sql);

		// Initial insert
		$rows_affected = $wpdb->insert(
			$table_name,
			array(
				'field_one' => 'a string'
			)
		);
	}
}

function myplugin_do_something() {
     // something to do when plugins are loaded
}

function myplugin_do_something_else() {
     // do something when options are updated
}

/**
 * Replaces shortcode "[myplugin]" with plugin stuff
 * Can be called anywhere in template with
 * <?php echo do_shortcode('[myplugin]'); ?>
 * @param array $atts
 * @param string $content
 * @param string $code
 */
function myplugin_shortcode($atts = array(), $content=null, $code="") {
     // have to return, not echo
     return 'something to replace shortcode';
}

/**
* Drops the table and deletes the options
* when user clicks "delete plugin"
*/
function potd_uninstall() {
	global $wpdb;
	$table_name = $wpdb->prefix . "myplugin";

	if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") === $table_name) {
		$qry = "
			DROP TABLE `{$table_name}`
		";
		$wpdb->query($qry);
	}

	delete_option('myplugin_option');
}

register_activation_hook( __FILE__, 'myplugin_install' );
add_shortcode( 'myplugin', 'myplugin_shortcode' );
register_uninstall_hook( __FILE__, 'myplugin_uninstall' );
This entry was posted in Code Snippets and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>