Created
January 11, 2013 07:29
-
-
Save ankitnetwork18/4508704 to your computer and use it in GitHub Desktop.
wordpress: add custom fields to post editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Plugin Name: Patti | |
Description: Modify Wordpress Add New Post for Adding Patti Data | |
Version: 1.0 | |
Author: Ankit Agrawal | |
*/ | |
/****************************************************************************/ | |
/************************* ADD PATTI CUSTOM FIELDS *************************/ | |
add_action( 'admin_init', 'patti_admin' ); | |
function patti_admin() { | |
//add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args ); | |
add_meta_box( 'patti_meta_box','Location Details','display_patti_meta_box','post', 'normal', 'high' ); | |
add_meta_box( 'patti_image_video', 'Image & Video', 'display_patti_image_video_meta_box', 'post', 'normal', 'high' ); | |
} | |
//add meta box for patti topics and location | |
function display_patti_meta_box( $patti ) { | |
$patti_tags = get_post_meta( $patti->ID, 'patti_tags', true ); | |
$patti_state = get_post_meta( $patti->ID, 'patti_state', true ); | |
$patti_city = get_post_meta( $patti->ID, 'patti_city', true ); | |
?> | |
<table> | |
<tr> | |
<td style="width: 150px">State</td> | |
<td> | |
<select style="width: 100px" name="patti_state"> | |
<?php | |
// Generate all items of drop-down list of states | |
$states = array( | |
'Select State', | |
'Uttar Pradesh', | |
'Delhi', | |
'Himachal Pradesh', | |
'Maharashtra' | |
); | |
foreach ( $states as $state ) { ?> | |
<option value="<?php echo $state; ?>" | |
<?php echo selected( $state, $patti_state ); ?>> | |
<?php echo $state; ?> | |
<?php } ?> | |
</select> | |
</td> | |
</tr> | |
<tr> | |
<td style="width: 150px">City</td> | |
<td> | |
<select style="width: 100px" name="patti_city"> | |
<?php | |
// Generate all items of drop-down list of states | |
$cities = array( | |
'Select City', | |
'Delhi', | |
'Mumbai', | |
'Lucknow', | |
'Agra' | |
); | |
foreach ( $cities as $city ) { ?> | |
<option value="<?php echo $city; ?>" | |
<?php echo selected( $city, $patti_city); ?>> | |
<?php echo $city; ?> | |
<?php } ?> | |
</select> | |
</td> | |
</tr> | |
</table> | |
<?php } | |
//add metabox for patti image and video | |
function display_patti_image_video_meta_box( $patti ) { | |
$patti_image = esc_url( get_post_meta( $patti->ID, 'patti_image', true ) ); | |
$patti_video = esc_url( get_post_meta( $patti->ID, 'patti_video', true ) ); | |
?> | |
<table> | |
<tr> | |
<td style="width: 150px">Image Path:</td> | |
<td> | |
<input id="patti_image" type="text" size="100" name="patti_image" value="<?php echo $patti_image; ?>" /> | |
<input id="upload_logo_button" type="button" class="button" value="<?php _e( 'Upload Image', 'patti' ); ?>" /> | |
</td> | |
</tr> | |
<tr> | |
<td style="width: 150px">Video Path:</td> | |
<td><input type="text" size="100" name="patti_video" value="<?php echo $patti_video; ?>" /></td> | |
</tr> | |
</table> | |
<?php } | |
//save patti fields | |
add_action( 'save_post','add_patti_fields', 10, 2 ); | |
function add_patti_fields( $patti_id,$patti ) { | |
// Check post type for post | |
if ( $patti->post_type == 'post' ) { | |
//implictly set content type to patti | |
update_post_meta( $patti_id, 'content_type', 'patti'); | |
// Store data in post meta table if present in post data | |
if ( isset( $_POST['patti_tags'] ) && $_POST['patti_tags'] != '' ) { | |
update_post_meta( $patti_id, 'patti_tags',$_POST['patti_tags'] ); | |
} | |
if ( isset( $_POST['patti_state'] ) && $_POST['patti_state'] != '' ) { | |
update_post_meta( $patti_id, 'patti_state', $_POST['patti_state'] ); | |
} | |
if ( isset( $_POST['patti_city'] ) && $_POST['patti_city'] != '' ) { | |
update_post_meta( $patti_id, 'patti_city', $_POST['patti_city'] ); | |
} | |
if ( isset( $_POST['patti_image'] ) && $_POST['patti_image'] != '' ) { | |
update_post_meta( $patti_id, 'patti_image', $_POST['patti_image'] ); | |
} | |
if ( isset( $_POST['patti_video'] ) && $_POST['patti_video'] != '' ) { | |
update_post_meta( $patti_id, 'patti_video', $_POST['patti_video'] ); | |
} | |
} | |
} | |
/****************************************************************************/ | |
/****************************************************************************/ | |
/*****************************************************************************/ | |
/********************* ADD MEDIA UPLOADER **************************/ | |
// load wordpress media uploader | |
function patti_enqueue_scripts() { | |
wp_register_script( 'patti-upload', plugins_url() .'/Patti/patti-upload.js', array('jquery','media-upload','thickbox') ); | |
wp_enqueue_script('jquery'); | |
wp_enqueue_script('thickbox'); | |
wp_enqueue_style('thickbox'); | |
wp_enqueue_script('media-upload'); | |
wp_enqueue_script('patti-upload'); | |
} | |
add_action('admin_enqueue_scripts', 'patti_enqueue_scripts'); | |
// modify media uploader settings | |
function patti_options_setup() { | |
global $pagenow; | |
if ( 'media-upload.php' == $pagenow || 'async-upload.php' == $pagenow ) { | |
// Now we'll replace the 'Insert into Post Button' inside Thickbox | |
add_filter( 'gettext', 'replace_thickbox_text' , 1, 3 ); | |
} | |
} | |
add_action( 'admin_init', 'patti_options_setup' ); | |
function replace_thickbox_text($translated_text, $text, $domain) { | |
if ('Insert into Post' == $text) { | |
$referer = strpos( wp_get_referer(), 'patti-settings-image' ); | |
if ( $referer != '' ) { | |
return __('Insert this image into Patti', 'patti' ); | |
} | |
} | |
return $translated_text; | |
} | |
/*******************************************************************/ | |
/*****************************************************************************/ | |
/****************************************************************************/ | |
/********************* DISABLE DEFAULTS **********************/ | |
//disable media buttons | |
add_action('admin_head', 'patti_remove_mediabuttons'); | |
function patti_remove_mediabuttons() | |
{ | |
global $post; | |
if($post->post_type == 'post' && current_user_can('publish_posts') ) | |
{ | |
remove_action( 'media_buttons', 'media_buttons' ); | |
} | |
} | |
//disable rich text editor | |
add_filter( 'user_can_richedit', 'disable_for_cpt' ); | |
function disable_for_cpt( $default ) { | |
global $post; | |
if ( 'post' == get_post_type( $post ) ) | |
return false; | |
return $default; | |
} | |
/****************************************************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment