Basics of WordPress plugin Tutorial. – part 1


Download wordpress from http://wordpress.org/download/

Today we write a small plugin for adding custom field to post

for example we have below fields in admin page in wordpresss

Now we are going to see,

how to add extra field for post.  (eg: author )

First we should add metabox

add_action( 'admin_menu', 'my_post_meta_box' );

eg for add_action

add_action( $tag, $function_to_add, $priority, $accepted_args )

see add_action on http://codex.wordpress.org/Function_Reference/add_action

$tag is name of action you want to hook eg. admin_menu, edit_post, publish_post

See what all we can hook onto

http://codex.wordpress.org/Plugin_API/Action_Reference

to add metabox

function my_post_meta_box() { add_meta_box( 'my-custom-box', 'Author', 'my_post_meta_box', 'post', 'normal', 'high' );

}

see about add_metabox: http://codex.wordpress.org/Function_Reference/add_meta_box

Add form of author field like this

function my_post_meta_box( $object, $box ) { ?>

 Author
 <input name="author" id="author" style="width: 97%;" value = ID, 'Author', true ), 1 ); ?> > <input type="hidden" name="my_meta_box_nonce" value="" />

next we should add action for save_post

add_action( 'save_post', 'my_save_post_meta_box');

function my_save_post_meta_box to save

function my_save_post_meta_box( $post_id, $post ) { if ( !wp_verify_nonce( $_POST['my_meta_box_nonce'], plugin_basename( __FILE__ ) ) ) return $post_id; if ( !current_user_can( 'edit_post', $post_id ) ) return $post_id; $meta_value = get_post_meta( $post_id, 'Author', true ); $new_meta_value = stripslashes( $_POST['author'] ); if ( $new_meta_value && '' == $meta_value ) add_post_meta( $post_id, 'Author', $new_meta_value, true ); elseif ( $new_meta_value != $meta_value ) update_post_meta( $post_id, 'Author', $new_meta_value ); elseif ( '' == $new_meta_value && $meta_value ) delete_post_meta( $post_id, 'Author', $meta_value ); }

Total code is

plugin name is example

wp-content/plugins/example/example.php

<?php
/*
Plugin Name: Example
Description: Plugin for adding author field for post
Author: S. Sathianarayanan
Version: 1.0
Author URI: http://www.sathia27.wordpress.com
*/

//*************** Admin function ***************

add_action( 'admin_menu', 'my_create_post_meta_box' );
add_action( 'save_post', 'my_save_post_meta_box');
function my_create_post_meta_box() {
	add_meta_box( 'my-custom-box', 'Date', 'my_post_meta_box', 'post', 'normal', 'high' );
}
function my_post_meta_box( $object, $box ) { ?>
	<p>
		<label for="author">Author</label>
		<br />
		<input name="author" id="author" style="width: 97%;" value = <?php echo wp_specialchars( get_post_meta( $object->ID, 'Author', true ), 1 ); ?> >
		<input type="hidden" name="my_meta_box_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
	</p>
<?php }
function my_save_post_meta_box( $post_id, $post ) {

	if ( !wp_verify_nonce( $_POST['my_meta_box_nonce'], plugin_basename( __FILE__ ) ) )
		return $post_id;

	if ( !current_user_can( 'edit_post', $post_id ) )
		return $post_id;

	$meta_value = get_post_meta( $post_id, 'Author', true );
	$new_meta_value = stripslashes( $_POST['author'] );

	if ( $new_meta_value && '' == $meta_value )
		add_post_meta( $post_id, 'Author', $new_meta_value, true );

	elseif ( $new_meta_value != $meta_value )
		update_post_meta( $post_id, 'Author', $new_meta_value );

	elseif ( '' == $new_meta_value && $meta_value )
		delete_post_meta( $post_id, 'Author', $meta_value );

}

?>

Advertisements

About sathia

Web developer at cloudmint
This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

One Response to Basics of WordPress plugin Tutorial. – part 1

  1. Ramachandran says:

    Great man.:)
    Nice post…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s