WordPress: Create Custom Post Option in Dashboard
In this tutorial I’m going to show you a quick trick to make a custom post option like a QuickPress in your WordPress dashboard. This will allow you to create your own options with custom field for a quick and easy blogging. So, let’s get started…
First of all we have to create out custom option panel in dashboard to get started, here is the code…
// Dashborad Posting Page - Gajaparan.com
function dashboard_post_function() {
echo ‘Hellow World!!’;
}
// Create the function use in the action hook
function dashboard_post_widget() {
wp_add_dashboard_widget('dashboard_post_widget', 'MyPress', 'dashboard_post_function');
}
// Hoook into the 'wp_dashboard_setup' action to register our other functions
add_action('wp_dashboard_setup', 'dashboard_post_widget' );
Result

Now, let’s add the html form inside the option panel.
echo '<div id="wrap">
<form action="" method="post">
<table border="1" width="700">
<tr>
<td><label for="post_title">Title</label></td>
<td><input name="post_title" type="text" size="60" /></td>
</tr>
<tr>
<td><label for="content">Content</label></td>
<td><textarea cols="70" rows="3" class="mceEditor" name="content"></textarea></td>
</tr>
<tr>
<td><label for="post_image">Post Image</label></td>
<td><input name="post_image" type="text" size="60" value="image url" /></td>
</tr>
<tr>
<td><label for="tag">TAG</label></td>
<td><input name="tag" type="text" size="60"/></td>
</tr>
</table>
<p>Woow… I did it…!!</p>
<br/>
<input name="submit" type="submit" value="Publish" class="button-primary" />
</form>
</div>';
Result

Now add the following code to make it work…
$postTitle = $_POST['post_title'];
$cont = $_POST['content'];
$tags = $_POST['tag'];
$submit = $_POST['submit'];
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
global $wpdb;
$image = $_POST['post_image'];
add_post_meta($post_ID, 'pimage', $image, true);
}
if( isset($submit) && !empty( $_POST['post_title'] )) {
global $user_ID;
// Get current User Information
require_once (ABSPATH . WPINC . '/pluggable.php');
global $current_user;
get_currentuserinfo();
$new_post = array(
'post_title' => $postTitle,
'post_content' => $cont,
'tags_input' => $tags,
'get_post_meta' => $image,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $current_user->ID,
'post_type' => 'post',
);
wp_insert_post($new_post);
}
So finally our code looks like this:
// Dashborad Posting Page - Gajaparan.com
function dashboard_post_function() {
echo '<div id="wrap">
<form action="" method="post">
<table border="1" width="700">
<tr>
<td><label for="post_title">Title</label></td>
<td><input name="post_title" type="text" size="60" /></td>
</tr>
<tr>
<td><label for="content">Content</label></td>
<td><textarea cols="70" rows="3" class="mceEditor" name="content"></textarea></td>
</tr>
<tr>
<td><label for="post_image">Post Image</label></td>
<td><input name="post_image" type="text" size="60" value="image url" /></td>
</tr>
<tr>
<td><label for="tag">TAG</label></td>
<td><input name="tag" type="text" size="60"/></td>
</tr>
</table>
<p>Woow… I did it…!!</p>
<br/>
<input name="submit" type="submit" value="Publish" class="button-primary" />
</form>
</div>';
}
// Create the function use in the action hook
function dashboard_post_widget() {
wp_add_dashboard_widget('dashboard_post_widget', 'MyPress', 'dashboard_post_function');
}
// Hoook into the 'wp_dashboard_setup' action to register our other functions
add_action('wp_dashboard_setup', 'dashboard_post_widget' );
// $_POST['****']; - * value should be your name="***" of you input form or textarea.
$postTitle = $_POST['post_title']; // title
$cont = $_POST['content']; // content
$tags = $_POST['tag']; // tags
$submit = $_POST['submit']; // submit button...
//add custom field automatically to your post
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
global $wpdb;
//store the image url in $image variable.
$image = $_POST['post_image'];
// add it to custom field "pimage"....
add_post_meta($post_ID, 'pimage', $image, true);
}
// if title not empty - submit button works...
if( isset($submit) && !empty( $_POST['post_title'] )) {
global $user_ID;
// Get current User Information
require_once (ABSPATH . WPINC . '/pluggable.php');
global $current_user;
get_currentuserinfo();
$new_post = array(
// add the title for the post
'post_title' => $postTitle,
// the content
'post_content' => $cont,
// add the tags
'tags_input' => $tags,
// add the custom field...
'get_post_meta' => $image,
// finally your done....
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
// Show the author
'post_author' => $current_user->ID,
'post_type' => 'post',
);
wp_insert_post($new_post);
}
So, that’s it… hope you like this tutorial…. If you have any questions just leave me a comment… I’m happy to answer your questions.
Reference:
http://codex.wordpress.org/Dashboard_Widgets_API
http://codex.wordpress.org/Function_Reference/add_post_meta
http://wpcanyon.com/tipsandtricks/adding-a-custom-field-automatically-on-postpage-publish/
Share it with Others
If you have enjoyed this entry. Please feel free to Bookmark / Share it using one of the following social sites.
Short Url : http://goo.gl/WmGtu



Use date_i18n instead of date, in order to retrieve the date in localized format.
And there is something wrong with “post author”, its empty if you publish a post from dashboard.
figure that out:
require_once (ABSPATH . WPINC . ‘/pluggable.php’);
global $current_user;
get_currentuserinfo();
and then use
‘post_author’ => $current_user->ID
to set the correct author info.
Hey Leo, Thank you for pointing out, Fixed