Hi,
this days i’m making a wordpress plugin for a client and i have to search some things in google.so i want to make this tips for you to make the things more easy.
1)how to make a widget for your plugin
this it’s something hard to find the right solution, i implemented this code in my plugin:
function widget_init() {
// Check for required functions
if (function_exists(‘register_sidebar_widget’)){
register_sidebar_widget(‘Simple Widget’,’simple_widget’);
}
}
function simple_widget($args) {
extract($args);
echo $before_widget;
echo $before_title
. ‘Simple Widget’
. $after_title.$after_widget;
}
add_action(“plugins_loaded”, “widget_init”);
add_action(‘widgets_init’, ‘widget_init’);
2)how to create your own plugin pages in the frontend
if you need to show the content of your plugin in a separated page, the best solution its to create a page for it.
$sql =”INSERT INTO “.$wpdb->posts.”
(post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_type)
VALUES
(‘1′, ‘$post_date’, ‘$post_date_gmt’, ‘[PLUGIN PAGE DONT DELETE THIS]‘, ”, ‘–plugin page’, ”, ‘publish’, ‘closed’, ‘closed’, ”, ‘[PLUGIN PAGE DONT DELETE THIS]‘, ”, ”, ‘$post_date’, ‘$post_date_gmt’, ‘$post_parent’, ‘0′, ‘page’)”;
$wpdb->query($sql);
add_option(‘plugin_page_id’,$wpdb->insert_id);
with this you are creating a page in the DB, then you add the hook and function to filter the content:
add_filter(‘the_posts’,'cmfrontend’);
function cmfrontend($posts){
$id=get_option(‘plugin_page_id’);
if($id==$_GET['page_id']){
$newpost[0]=$posts[0];
$newpost[0]->post_content=”your plugin page content”;
$newpost[0]->post_title=”Your plugin page title”;
}
}
and it’s done
3)remember to add the uninstall hook
to avoid future problems, in WP versions and DB updates, always add the deactivation hook, to erase (or clean) the DB and options. if the data it’s important, you can add a message warning this
.
register_deactivation_hook(__FILE__,’uninstallfunction’);
4)use a external css for the styles
when you make the style for the frontend of the plugin, do it assigning classes or id and save it using an external css file. this give you the feature for the future users/designer/programer change the style, colors and size whiteout touching your plugin code and avoiding he break something
you can do it setting a wp_head hook and in the function printing the import of the css file:
add_action(‘wp_head’,'echohead’);
function echohead(){
echo ‘<link rel=”stylesheet” href=”wp-content/plugins/yourpluginname/style.css” type=”text/css” />’;
}
5)keep a clean code
dont harcode the plugin, if you need to do something, search on the web, you going to find some hook or filter to make what you need. if everthing fails, then, make what you need and try to keep the compatiblity.
good places where find wodpress hooks and filters:
http://wphooks.flatearth.org/
i hope you find this useful
Regards,
Shadow.