Plugin Foundation Flashcards
Write plugin activation hook with function to deactivate plugin if WP version < 3.1
register_activation_hook( __FILE__, ‘boj_install’ );
function boj_install() {
If ( version_compare( get_bloginfo( ‘version’ ), ‘3.1’, ‘<’ ) ) {
deactivate_plugins( basename( __FILE__ ) );
}
}
?>
Write plugin activation hook with function to create default settings of view = grid, food = bacon, mode = zombie.
<?php </p>
register_activation_hook( __FILE__, ‘boj_install’ );
function boj_install() {
$boj_myplugin_options = array(
‘view’ => ‘grid’,
‘food’ => ‘bacon’,
‘mode’ => ‘zombie’
);
update_option( ‘boj_myplugin_options’, $boj_myplugin_options);
}
?>
Why shoudn’t you include uninstall functionality when a plugin is deactivated?
Imagine if you accidentally deactivate a plugin and upon reactivation you realize all your settings for that plugin have been deleted. Also, remember the WordPress automatic update feature deactivates all plugins prior to installing the new version of WordPress.
Write uninstall.php script that A.) ensures WP is actually calling the uninstall file and B.) deletes the plugin’s options(boj_myplugin_options) from the DB.
<?php </p>
if( !defined( ‘WP_UNINSTALL_PLUGIN’ ) )
exit ();
delete_option( ‘boj_myplugin_options’ );
?>
How should PHP functions be written?
lowercase with underscores
function function_name ( $myplugin_variable ) {
//do something
}
?>
How should plugin files be named?
With a unique name, lowercase, hyphen-separated.
boj-plugin-name.php