wordpress basic fundamental of plugin development and creating shortcode
DESCRIPTION
WordPress basic fundamental of plugin development,creating shortcode and how to create wordpress theme optionsTRANSCRIPT
© Copyright 2010 Nibiru Solutions Private Limited 1
Presented by: Rakesh
2© Copyright 2010 Nibiru Solutions Private Limited 2
Agenda
IntroductionHow to create wordpress plugin How to create shortcodeHow to create theme optionQuiz Questions
3© Copyright 2010 Nibiru Solutions Private Limited 3
Introduction
This session is very important and its very interesting.
In this session I cover some important worpress prebuild function
And there usesDeep understanding of creating worpress plugin.
Creating wordpress shortcode and uses.
How to create theme options and uses
4© Copyright 2010 Nibiru Solutions Private Limited 4
How To Create Wordpress Plugin
WordPress is not just a blogging platform and it is such a powerful CMS with unlimited capabilities, besides having a huge user base.
Almost anything can be scripted with wordpress. You can extend wordpress either by means of plugin or by a theme.
i will show you how to write a “Hello World” wordpress plugin, which unlike many believe is surprisingly easy, once you understand the very fundamentals.
5© Copyright 2010 Nibiru Solutions Private Limited 5
How To Create Wordpress Plugin -2
Plugin Files & Names 1. Always you chose a unique name to your plugin so that
it doesnt collide with names used in other plugins.
2. Assigning unique names, documenting and organizing the plugin files is very important part of plugin creation.
3- place the plugin php file directly into the wp-content/plugins folder
http://wordpress.org/plugins/about/readme.txt
6© Copyright 2010 Nibiru Solutions Private Limited 6
How To Create Wordpress Plugin -3
The Plugin Basics
The heart of a wordpress plugins is the below 2 functions (commonly called `hooks`)
add_action ($tag, $func) add_filter ($tag,$func)
add_action –> does an action at various points of wordpress execution
add_filter –> does filtering the data (eg. escaping quotes before mysql insert, or during output to browser.
7© Copyright 2010 Nibiru Solutions Private Limited 7
How To Create Wordpress Plugin -4
Plugin Information
Open your hello-world.php and in the first line, add this commented plugin information to your file.<?php /* Plugin Name: Hello-World Plugin URI: http://yourdomain.com/ Description: A simple hello world wordpress plugin Version: 1.0 Author: Your NameAuthor URI: http://yourdomain.com License: GPL */ ?>
Save file.
8© Copyright 2010 Nibiru Solutions Private Limited 8
How To Create Wordpress Plugin -5
Go to your wordpress admin > plugins and you will see the new plugin listed, waiting to get activated.
But this plugin had to do something right? -> For that we write the code using add_action below the
commented plugin information in the hello-world.php
9© Copyright 2010 Nibiru Solutions Private Limited 9
How To Create Wordpress Plugin -6
<?php /* This calls hello_world() function when wordpress initializes.*/ add_action('init','hello_world'); function hello_world() { echo "Hello World"; }?>
And Your Plugin done!When our plugin is activated, add_action command calls our hello_world()
function when wordpress starts loading.
To test Open Any index.php , page.php or single.php And place the following code.
<?php if(function_exists('hello_world')) { hello_world(); } ?>
10© Copyright 2010 Nibiru Solutions Private Limited 10
Build a plugin options page in admin area :
The plugin outputs hello world (its pretty much static).So make this to dynamic we need to create a good admin interface.
Here is what we do….We create a options menu for Hello World in WordPress Admin > Settings.We save the user entered data in the wordpress database. Using set_options() function.We retrieve the data stored in wordpress database and output it using get_options() function.
11© Copyright 2010 Nibiru Solutions Private Limited 11
Activating/Deactivating Plugin
It is very easy to write a function on what plugin does, when it gets activated. WordPress offers 4 very important functions
register_activation_hook -> Runs on plugin activationregister_deactivation_hook -> Runs on plugin deactivationadd_option -> Creates new database fieldget_option -> Retrieves the value in database field.
12© Copyright 2010 Nibiru Solutions Private Limited 12
Activating/Deactivating Plugin -2
Example : write fallowing code in hello-word.php<?php /* Runs when plugin is activated */
register_activation_hook(__FILE__,'hello_world_install'); /* Runs on plugin deactivation*/ register_deactivation_hook( __FILE__, 'hello_world_remove' ); function hello_world_install() { /* Creates new database field */ add_option("hello_world_data", 'Default', '', 'yes'); } function hello_world_remove() { /* Deletes the database field */ delete_option('hello_world_data'); } ?>
13© Copyright 2010 Nibiru Solutions Private Limited 13
Plugin Settings Page
All we need to create is plugin settings page in the wordpress admin area.
<?php if ( is_admin() ){
/* Call the html code */ add_action('admin_menu', 'hello_world_admin_menu'); function hello_world_admin_menu() { add_options_page('Hello World', 'Hello World', 'administrator',
'hello-world', 'hello_world_html_page'); }
} ?>
Here is a very important thing to remember:
The add_action for admin_menu should call a function hello_world_admin_menu() containing add_options_page, which in turn
should call a function hello_world_html_code() containing html code. This is how the code should flow! Refer to wordpress administration menus
14© Copyright 2010 Nibiru Solutions Private Limited 14
Plugin Settings Page – Coding Part -1
The below function has the html code for the settings page, containing the form
<?php
function hello_world_html_page() {?><div><h2>Hello World Options</h2><form method="post" action="options.php"><?php wp_nonce_field('update-options'); ?>
<table width="510"><tr valign="top"><th width="92" scope="row">Enter Text</th><td width="406">
15© Copyright 2010 Nibiru Solutions Private Limited 15
Plugin Settings Page – Coding Part -2
<input name="hello_world_data" type="text" id="hello_world_data"
value="<?php echo get_option('hello_world_data'); ?>" />(ex. Hello World)</td></tr></table> <input type="hidden" name="action" value="update" /><input type="hidden" name="page_options"
value="hello_world_data" /><p><input type="submit" value="<?php _e('Save Changes') ?>" /></p> </form></div><?php } ?>
16© Copyright 2010 Nibiru Solutions Private Limited 16
Plugin Settings Page – Coding Part -3
And finally your plugin ready and looks like following.
You must remember 2 things in the above code. 1. Specify the database field we created before in the input text box as
`hello_world_data`. <input name="hello_world_data" type="text" id="hello_world_data“
value="<?php echo get_option('hello_world_data'); ?>" /> 2. If your form has number of fields (like textbox, selectbox etc), all those
should be listed in the value field of page_options, separated by commas. For more information, refer to wordpress documentation<input type="hidden" name="page_options" value="hello_world_data" />
17© Copyright 2010 Nibiru Solutions Private Limited 17
Wwordpres Shortcode
The Shortcode API Introduced in WordPress 2.5 A simple set of functions for creating macro codes for use in post
content. And a simple shortcode used like [shotcode].
Lets Create a shortcode of Hello Word plugin <?php
function show_hello_text() { return get_option('hello_world_data'); } add_shortcode('showtext', 'show_hello_text'); ?>
Use the shortcode [showtext] into page or post content to show your hello world text.
18© Copyright 2010 Nibiru Solutions Private Limited 18
How to create custom them option
If you create your own themes you will, sooner or later, want to allow your theme users have some control over certain appearance and/or functional elements of your theme.
19© Copyright 2010 Nibiru Solutions Private Limited 19
How to create custom them option -2
This commonly use following functions. add_action( $tag, $function_to_add, $priority, $accepted_args );register_setting( $option_group, $option_name, $sanitize_callback )add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function );settings_fields( $option_group )
## coding section<? php add_action( 'admin_init', 'theme_options_init' );add_action( 'admin_menu', 'theme_options_add_page' ); function theme_options_init(){ register_setting( 'sample_options', 'sample_theme_options');} function theme_options_add_page() { add_theme_page( __( 'Theme Options', 'sampletheme' ), __( 'Theme
Options', 'sampletheme' ), 'edit_theme_options', 'theme_options', 'theme_options_do_page' );
}
1- this used in setting_fields();
2- this function name and its used html
20© Copyright 2010 Nibiru Solutions Private Limited 20
How to create custom them option -3
function theme_options_do_page() { global $select_options;
<form method="post" action="options.php"><?php settings_fields( 'sample_options' ); ?> <?php $options = get_option( 'sample_theme_options' ); ?>
<input id="sample_theme_options[showintro]" name="sample_theme_options[showintro]" type="checkbox" value="1“ <?php checked( '1', $options['showintro'] ); ?> />
<input id="sample_theme_options[fburl]" type="text" name="sample_theme_options[fburl]" value="<?php esc_attr_e( $options['fburl'] ); ?>" />
<textarea id="sample_theme_options[introtext]"class="large-text" cols="50" rows="10"
name="sample_theme_options[introtext]"><?php echo esc_textarea( $options['introtext'] ); ?></textarea>
<input type="submit" value="<?php _e( 'Save Options', 'customtheme' ); ?>" /> </form> } ?>
1
2
21© Copyright 2010 Nibiru Solutions Private Limited 21
The End Of Then Session
Thank You !