guideline custom post type with multple fields in wordpress
<?php
add_action( 'init', 'guideline' );
function guideline(){
register_post_type( 'guideline',
array(
'labels' => array(
'name' => __( 'Guidelines' ),
'singular_name' => __( 'Guideline' ),
'menu_name' => __( 'Guidelines' ),
'all_items' => __( 'All Guidelines' ),
'add_new' => __( 'Add New Guidelines' ),
'add_new_item' => __( 'Add New Guidelines' ),
'edit_item' => __( 'Edit Guideline' ),
'new_item' => __( 'New Guideline' ),
'view_item' => __( 'View Guideline' ),
'search_items' => __( 'Search Guidelines' ),
'not_found' => __( 'Guideline Not Found' ),
'not_found_in_trash' => __( 'Guidelines Not Found In Trash' ),
'parent_item_colon' => __( 'Parent Guideline' ),
),
'public' => true,
'has_archive' => true,
'supports' => array('thumbnail'),
)
);
}
function guideline_meta() {
add_meta_box( 'guideline_meta', 'Guideline Details', 'guideline_meta_functions', 'guideline', 'normal', 'high' );
}
?>
<?php
function guideline_meta_functions() {
global $post;
wp_nonce_field( plugin_basename( __FILE__ ), 'guideline_meta_functions_nonce' ); // Create nonce to protect post
// If meta exists, get it
$first_name = get_post_meta($post->ID, '_first_name', true);
$last_name = get_post_meta($post->ID, '_last_name', true);
$position = get_post_meta($post->ID, '_position', true);
$notes = get_post_meta($post->ID, '_notes', true);
?>
<p>
<label for="first_name">First Name</label>
<input class="widefat" type="text" name="_first_name" id="first_name" value="<?php
if(isset($first_name)) {
echo $first_name;
}
?>" />
</p>
<p>
<label for="last_name">Last Name</label>
<input class="widefat" type="text" name="_last_name" id="last_name" value="<?php
if(isset($last_name)) {
echo $last_name;
}
?>" />
</p>
<p>
<label for="position">Position</label>
<input class="widefat" type="text" name="_position" id="position" value="<?php
if(isset($position)) {
echo $position;
}
?>" />
</p>
<p>
<label for="notes">Notes</label>
<textarea class="widefat" rows="10" name="_notes" id="notes"><?php
if(isset($notes)) {
echo $notes;
}
?></textarea>
</p>
<?php
}
function guideline_meta_save($post_id, $post) {
global $post;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return false;
if ( !current_user_can( 'edit_post', $post->ID ) ) {
return $post->ID;
}
if (!wp_verify_nonce( $_POST['guideline_meta_functions_nonce'], plugin_basename( __FILE__ ) ) ) {
} else {
if($_POST['_first_name']) {
update_post_meta($post->ID, '_first_name', $_POST['_first_name']);
} else {
update_post_meta($post->ID, '_first_name', '');
}
if($_POST['_last_name']) {
update_post_meta($post->ID, '_last_name', $_POST['_last_name']);
} else {
update_post_meta($post->ID, '_last_name', '');
}
if($_POST['_position']) {
update_post_meta($post->ID, '_position', $_POST['_position']);
} else {
update_post_meta($post->ID, '_position', '');
}
if($_POST['_notes']) {
update_post_meta($post->ID, '_notes', $_POST['_notes']);
} else {
update_post_meta($post->ID, '_notes', '');
}
}
return false;
}
add_action( 'admin_menu', 'guideline_meta' );
add_action( 'save_post', 'guideline_meta_save', 1, 2);
// Custom column Display
add_filter( 'manage_edit-guideline_columns', 'edit_guideline_columns' ) ;
function edit_guideline_columns( $columns ) {
$columns = array(
/* 'thumbnail' => ('Thumbnail'),*/
'first_name' => __( 'First Name' ),
'last_name' => __(' Last Name' ),
'position' => __('Position'),
'edit' => __('Edit')
);
return $columns;
}
// What to display in each list column
add_action( 'manage_guideline_posts_custom_column', 'manage_guideline_columns', 10, 2 );
function manage_guideline_columns( $column, $post_id ) {
// Get meta if exists
/* $thumbnail = get_the_post_thumbnail( $post_id, 'thumbnail' );*/
$first_name = get_post_meta( $post_id, '_first_name', true );
$last_name = get_post_meta( $post_id, '_last_name', true );
$position = get_post_meta( $post_id, '_position', true );
switch( $column ) {
/* case 'thumbnail' :
if ( empty( $thumbnail))
echo __( 'Unknown' );
else
echo $thumbnail;
break;*/
case 'first_name' :
if ( empty( $first_name ) ) {
echo __( 'Unknown' );
} else {
echo $first_name;
}
break;
case 'last_name' :
if ( empty( $last_name ) ) {
echo __( 'Unknown' );
} else {
echo $last_name;
}
break;
case 'position' :
if ( empty( $position ) ) {
echo __( 'Unknown' );
} else {
echo $position;
}
break;
case 'edit' :
echo '<a href="'.site_url('/wp-admin/post.php?post='.$post_id.'&action=edit').'">EDIT</a>';
break;
}
}
// Which columns are filterable
add_filter( 'manage_edit-guideline_sortable_columns', 'guideline_sortable_columns' );
function guideline_sortable_columns( $columns ) {
$columns['first_name'] = 'first_name';
$columns['last_name'] = 'last_name';
$columns['position'] = 'position';
return $columns;
}
function staff_assets(){
wp_register_script( 'jquery_datatables_js', get_template_directory_uri() . '/staff/asset/js/jquery.dataTables.min.js', array(),null,true );
wp_enqueue_script( 'jquery_datatables_js' );
wp_register_style( 'jquery_datatables_css', get_template_directory_uri() . '/staff/asset/css/jquery.dataTables.css');
wp_enqueue_style( 'jquery_datatables_css' );
}
add_action('wp_enqueue_scripts', 'staff_assets');
function list_staff() {
wp_reset_query();
// Only staff members
$args = array(
'post_type' => 'guideline',
'posts_per_page' => 9999 // Set to high number to override default posts limit
);
$query = new WP_Query( $args );
if ($query->have_posts()) :
// Specify this table is a dataTable and assign an ID
echo '<table class="tablepress dataTable" id="staff_table">';
echo '<thead>';
echo '<tr>';
/* echo '<th>Image</th>';*/
echo '<th>First Name</th>';
echo '<th>Last Name</th>';
echo '<th>Position</th>';
echo '<th>Notes</th>';
echo '</tr>';
echo '</thead>';
while( $query->have_posts() ) : $query->the_post();
// Get meta if exists
/* $thumbnail = get_the_post_thumbnail( get_the_ID(), 'thumbnail' );*/
$first_name = get_post_meta( get_the_ID(), '_first_name', true);
$last_name = get_post_meta( get_the_ID(), '_last_name', true);
$position = get_post_meta( get_the_ID(), '_position', true);
$notes = get_post_meta( get_the_ID(), '_notes', true);
?>
<tr>
<?php /*?> <td>
<?php
if($thumbnail) {
echo $thumbnail;
} else {
echo 'No Image';
}
?>
</td><?php */?>
<td><?php echo $first_name; ?></td>
<td><?php echo $last_name; ?></td>
<td><?php echo $position; ?></td>
<td><?php echo $notes; ?>
<?php if ( current_user_can('edit_post', get_the_ID()) ) {
// Edit button if current user can edit staff members
echo '<span class="edit-link"><a class="post-edit-link" href="/wp-admin/post.php?post='.get_the_ID().'&action=edit">EDIT</a></span>';
} ?>
</td>
</tr>
<?php
endwhile;
echo '</table>';
?>
<script type="text/javascript">
jQuery(document).ready(function(){
// Init dataTable
jQuery('#staff_table').dataTable({
"aaSorting":[],
"bSortClasses":false,
"asStripeClasses":['even','odd'],
"bSort":true,
aoColumnDefs: [
{
bSortable: false,
aTargets: [ 0,4 ]
}
]
});
});
</script>
<?php
endif;
}
function list_staff_obj($atts, $content=null) {
ob_start();
list_staff($atts, $content=null);
$output=ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode( 'list-staff', 'list_staff_obj' );
add_action( 'init', 'guideline' );
function guideline(){
register_post_type( 'guideline',
array(
'labels' => array(
'name' => __( 'Guidelines' ),
'singular_name' => __( 'Guideline' ),
'menu_name' => __( 'Guidelines' ),
'all_items' => __( 'All Guidelines' ),
'add_new' => __( 'Add New Guidelines' ),
'add_new_item' => __( 'Add New Guidelines' ),
'edit_item' => __( 'Edit Guideline' ),
'new_item' => __( 'New Guideline' ),
'view_item' => __( 'View Guideline' ),
'search_items' => __( 'Search Guidelines' ),
'not_found' => __( 'Guideline Not Found' ),
'not_found_in_trash' => __( 'Guidelines Not Found In Trash' ),
'parent_item_colon' => __( 'Parent Guideline' ),
),
'public' => true,
'has_archive' => true,
'supports' => array('thumbnail'),
)
);
}
function guideline_meta() {
add_meta_box( 'guideline_meta', 'Guideline Details', 'guideline_meta_functions', 'guideline', 'normal', 'high' );
}
?>
<?php
function guideline_meta_functions() {
global $post;
wp_nonce_field( plugin_basename( __FILE__ ), 'guideline_meta_functions_nonce' ); // Create nonce to protect post
// If meta exists, get it
$first_name = get_post_meta($post->ID, '_first_name', true);
$last_name = get_post_meta($post->ID, '_last_name', true);
$position = get_post_meta($post->ID, '_position', true);
$notes = get_post_meta($post->ID, '_notes', true);
?>
<p>
<label for="first_name">First Name</label>
<input class="widefat" type="text" name="_first_name" id="first_name" value="<?php
if(isset($first_name)) {
echo $first_name;
}
?>" />
</p>
<p>
<label for="last_name">Last Name</label>
<input class="widefat" type="text" name="_last_name" id="last_name" value="<?php
if(isset($last_name)) {
echo $last_name;
}
?>" />
</p>
<p>
<label for="position">Position</label>
<input class="widefat" type="text" name="_position" id="position" value="<?php
if(isset($position)) {
echo $position;
}
?>" />
</p>
<p>
<label for="notes">Notes</label>
<textarea class="widefat" rows="10" name="_notes" id="notes"><?php
if(isset($notes)) {
echo $notes;
}
?></textarea>
</p>
<?php
}
function guideline_meta_save($post_id, $post) {
global $post;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return false;
if ( !current_user_can( 'edit_post', $post->ID ) ) {
return $post->ID;
}
if (!wp_verify_nonce( $_POST['guideline_meta_functions_nonce'], plugin_basename( __FILE__ ) ) ) {
} else {
if($_POST['_first_name']) {
update_post_meta($post->ID, '_first_name', $_POST['_first_name']);
} else {
update_post_meta($post->ID, '_first_name', '');
}
if($_POST['_last_name']) {
update_post_meta($post->ID, '_last_name', $_POST['_last_name']);
} else {
update_post_meta($post->ID, '_last_name', '');
}
if($_POST['_position']) {
update_post_meta($post->ID, '_position', $_POST['_position']);
} else {
update_post_meta($post->ID, '_position', '');
}
if($_POST['_notes']) {
update_post_meta($post->ID, '_notes', $_POST['_notes']);
} else {
update_post_meta($post->ID, '_notes', '');
}
}
return false;
}
add_action( 'admin_menu', 'guideline_meta' );
add_action( 'save_post', 'guideline_meta_save', 1, 2);
// Custom column Display
add_filter( 'manage_edit-guideline_columns', 'edit_guideline_columns' ) ;
function edit_guideline_columns( $columns ) {
$columns = array(
/* 'thumbnail' => ('Thumbnail'),*/
'first_name' => __( 'First Name' ),
'last_name' => __(' Last Name' ),
'position' => __('Position'),
'edit' => __('Edit')
);
return $columns;
}
// What to display in each list column
add_action( 'manage_guideline_posts_custom_column', 'manage_guideline_columns', 10, 2 );
function manage_guideline_columns( $column, $post_id ) {
// Get meta if exists
/* $thumbnail = get_the_post_thumbnail( $post_id, 'thumbnail' );*/
$first_name = get_post_meta( $post_id, '_first_name', true );
$last_name = get_post_meta( $post_id, '_last_name', true );
$position = get_post_meta( $post_id, '_position', true );
switch( $column ) {
/* case 'thumbnail' :
if ( empty( $thumbnail))
echo __( 'Unknown' );
else
echo $thumbnail;
break;*/
case 'first_name' :
if ( empty( $first_name ) ) {
echo __( 'Unknown' );
} else {
echo $first_name;
}
break;
case 'last_name' :
if ( empty( $last_name ) ) {
echo __( 'Unknown' );
} else {
echo $last_name;
}
break;
case 'position' :
if ( empty( $position ) ) {
echo __( 'Unknown' );
} else {
echo $position;
}
break;
case 'edit' :
echo '<a href="'.site_url('/wp-admin/post.php?post='.$post_id.'&action=edit').'">EDIT</a>';
break;
}
}
// Which columns are filterable
add_filter( 'manage_edit-guideline_sortable_columns', 'guideline_sortable_columns' );
function guideline_sortable_columns( $columns ) {
$columns['first_name'] = 'first_name';
$columns['last_name'] = 'last_name';
$columns['position'] = 'position';
return $columns;
}
function staff_assets(){
wp_register_script( 'jquery_datatables_js', get_template_directory_uri() . '/staff/asset/js/jquery.dataTables.min.js', array(),null,true );
wp_enqueue_script( 'jquery_datatables_js' );
wp_register_style( 'jquery_datatables_css', get_template_directory_uri() . '/staff/asset/css/jquery.dataTables.css');
wp_enqueue_style( 'jquery_datatables_css' );
}
add_action('wp_enqueue_scripts', 'staff_assets');
function list_staff() {
wp_reset_query();
// Only staff members
$args = array(
'post_type' => 'guideline',
'posts_per_page' => 9999 // Set to high number to override default posts limit
);
$query = new WP_Query( $args );
if ($query->have_posts()) :
// Specify this table is a dataTable and assign an ID
echo '<table class="tablepress dataTable" id="staff_table">';
echo '<thead>';
echo '<tr>';
/* echo '<th>Image</th>';*/
echo '<th>First Name</th>';
echo '<th>Last Name</th>';
echo '<th>Position</th>';
echo '<th>Notes</th>';
echo '</tr>';
echo '</thead>';
while( $query->have_posts() ) : $query->the_post();
// Get meta if exists
/* $thumbnail = get_the_post_thumbnail( get_the_ID(), 'thumbnail' );*/
$first_name = get_post_meta( get_the_ID(), '_first_name', true);
$last_name = get_post_meta( get_the_ID(), '_last_name', true);
$position = get_post_meta( get_the_ID(), '_position', true);
$notes = get_post_meta( get_the_ID(), '_notes', true);
?>
<tr>
<?php /*?> <td>
<?php
if($thumbnail) {
echo $thumbnail;
} else {
echo 'No Image';
}
?>
</td><?php */?>
<td><?php echo $first_name; ?></td>
<td><?php echo $last_name; ?></td>
<td><?php echo $position; ?></td>
<td><?php echo $notes; ?>
<?php if ( current_user_can('edit_post', get_the_ID()) ) {
// Edit button if current user can edit staff members
echo '<span class="edit-link"><a class="post-edit-link" href="/wp-admin/post.php?post='.get_the_ID().'&action=edit">EDIT</a></span>';
} ?>
</td>
</tr>
<?php
endwhile;
echo '</table>';
?>
<script type="text/javascript">
jQuery(document).ready(function(){
// Init dataTable
jQuery('#staff_table').dataTable({
"aaSorting":[],
"bSortClasses":false,
"asStripeClasses":['even','odd'],
"bSort":true,
aoColumnDefs: [
{
bSortable: false,
aTargets: [ 0,4 ]
}
]
});
});
</script>
<?php
endif;
}
function list_staff_obj($atts, $content=null) {
ob_start();
list_staff($atts, $content=null);
$output=ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode( 'list-staff', 'list_staff_obj' );
Comments
Post a Comment