Skip to content

Instantly share code, notes, and snippets.

@AVDW
Created October 13, 2013 13:44
Show Gist options
  • Save AVDW/6962534 to your computer and use it in GitHub Desktop.
Save AVDW/6962534 to your computer and use it in GitHub Desktop.
Handy Wordpress Snippets
global $wpdb;
if (!class_exists("[class name]")) {
class [class name] {
public function ignite(){
add_action('admin_init', array(&$this, 'adminInit'));
add_action('admin_menu', array(&$this, 'addMenu'));
add_action('admin_head', array(&$this, 'adminStyles'));
}
public function adminInit(){
// register your plugin's settings
}
public function adminStyles(){
echo '<link rel="stylesheet" type="text/css" href="'.plugins_url('[file]').'">';
}
public function addMenu(){
add_menu_page([Refer to wordpress.org]);
// Add a page to manage this plugin's settings
add_options_page([Refer to wordpress.org]);
}
public function pluginAdmin(){
if(!current_user_can('manage_options')){
wp_die(__('You do not have sufficient permissions to access this page.'));
}
// Render the settings template
include(sprintf("%s/[file]", dirname(__FILE__)));
} // END public function pluginAdmin()
public function pluginSettings(){
if(!current_user_can('manage_options')){
wp_die(__('You do not have sufficient permissions to access this page.'));
}
// Render the settings template
include(sprintf("%s/[file]", dirname(__FILE__)));
} // END public function pluginSettings()
/** Activate the plugin */
public static function activate() {
// Do nothing
} // END public static function activate
/** Deactivate the plugin */
public static function deactivate() {
// Do nothing
} // END public static function deactivate
public function init_settings() {
// register the settings for this plugin
register_setting('wp_plugin_template-group', 'setting_a');
register_setting('wp_plugin_template-group', 'setting_b');
} // END public function init_custom_settings())
}
}
if(class_exists([class name])){
register_activation_hook(__FILE__, array('[class name]', 'activate'));
register_deactivation_hook(__FILE__, array('[class name]', 'deactivate'));
$[class name] = new [class name];
$[class name]->ignite();
}
global $wpdb;
$table_name = "your-table-name-here";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
//table is not created. you may create the table here.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment