Hi, Today Discussed WordPress Theme customized and admin and frontend How to Create new modules Wordpress without plugin created this task discussed today. New modules without plugin Follows Briefly discussed.
<?php
//
// 'staff' Custom Post Type
//
// Register the Post Type - staff
// http://codex.wordpress.org/Function_Reference/register_post_type
add_action('init', 'register_staff');
function register_staff() {
$labels = array(
'name' => _x('Staff', 'post type general name'),
'singular_name' => _x('Staff', 'post type singular name'),
'add_new' => _x('Add New', 'Staff'),
'add_new_item' => __('Add New Staff Member'),
'edit_item' => __('Edit Staff'),
'new_item' => __('New Staff'),
'view_item' => __('View Staff'),
'search_items' => __('Search Staffs'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'staff',
'with_front' => false
),
'has_archive' => 'staff',
'capability_type' => 'page',
'hierarchical' => false,
'menu_position' => 22,
'supports' => array('custom-fields') // 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' , 'page-attributes'
);
register_post_type( 'staff' , $args );
}
//
// Create Profile Images sizes
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'staff-profile-small', 45, 45, true );
add_image_size( 'staff-profile-thumb', 124, 124, true );
add_image_size( 'staff-profile', 204, 204, true );
}
//
// Add Meta Boxes
// http://codex.wordpress.org/Function_Reference/add_meta_box
//
function tpx_staff_init(){
add_meta_box("metabox_staff_staff", "Staff Details", "metabox_staff_staff", "staff", "normal","high");
add_meta_box("metabox_staff_description", "Description", "metabox_staff_description", "staff", "normal","high");
}
add_action("admin_init", "tpx_staff_init");
function metabox_staff_staff() {
global $post;
?>
<div class="custom_meta">
<div class="photo_profile">
<label>Photo</label>
<div class="fileWrapper"><?php
$photo_profileID = get_post_meta($post->ID, 'staff_file_photo_profile', true);
$photoArray = wp_get_attachment_image_src( $photo_profileID, 'staff-profile-thumb', false);
$photoSrc = $photoArray[0];
if($photoSrc){
echo '<a class="photo"><img src="' . $photoSrc . '" alt="staffimage" /></a>';
} else {
echo '<a class="photo"><span>Upload Photo</span></a>';
}
?>
<input type="file" name="staff_file_photo_profile" /></div>
</div>
<ul class="formfields">
<li>
<label>First Name</label>
<?php tpx_meta_input_text('staff_name_first'); ?>
</li>
<li>
<label>Surname</label>
<?php tpx_meta_input_text('staff_name_last'); ?>
</li>
<li>
<label>Position</label>
<?php tpx_meta_input_text('staff_position'); ?>
</li>
</ul>
</div>
<?php
}
function metabox_staff_description() {
global $post;
?>
<div class="custom_meta">
<?php
$staff_description = get_post_meta($post->ID, 'staff_description', true);
the_editor($staff_description, 'staff_description', 'title', false, 1); ?>
</div>
<?php
}
add_action('save_post', 'save_staff_meta');
function save_staff_meta(){
global $post, $initLoop;
$prefix = "staff_";
$initLoop = true;
//Upload Headshot
foreach($_FILES as $field => $file){
if($field == 'staff_file_photo_profile'){
$filename = $_POST['staff_name_first'] . ' ' . $_POST['staff_name_last'] . ' Profile Photo';
tpx_save_attachement($file, $post->ID, $filename, 'staff_file_photo_profile');
//Remove it from the array to avoid duplicates.
unset($_FILES[$field]);
}
}
//Normal Inputs
foreach($_POST as $field => $value){
$has_prefix = strstr($field, $prefix);
if($has_prefix){
//Capitalize Name
if($field == 'staff_name_first') $value = ucwords($value);
if($field == 'staff_name_last') $value = ucwords($value);
if($value){
update_post_meta($post->ID, $field, $value); // Save Value
} else {
delete_post_meta($post->ID, $field); // Remove it if its not there
}
}
}
}
add_filter('wp_insert_post_data', 'update_staff_title', 10, 2);
function update_staff_title($data, $postarr){
if($data['post_type'] == 'staff' && $_POST['staff_name_first']){
$new_title = $_POST['staff_name_first'] . ' ' . $_POST['staff_name_last'];
$new_slug = strtolower($_POST['staff_name_first']) . '-' . strtolower($_POST['staff_name_last']);
$new_slug = str_replace(' ', '', $new_slug);
$data['post_title'] = ucwords($new_title);
$data['post_name'] = $new_slug;
}
return $data;
}
//
// Sort Staff Menu
//
// add the admin options page
add_action('admin_menu', 'tpx_add_page_sort_staff');
function tpx_add_page_sort_staff() {
add_submenu_page('edit.php?post_type=staff', 'Sort Staff', 'Sort Staff', 'edit_pages', 'tpx-staff-sort', 'tpx_options_staff_sort');
add_submenu_page('edit.php?post_type=staff', 'Staff Overview', 'Overview', 'edit_pages', 'tpx-staff-overview', 'tpx_options_staff_overview');
}
//
// Sort Staff Page
//
function tpx_options_staff_sort() {
$tpx_options_staff_sort = get_option( 'tpx_options_staff_sort');
?>
<div class="wrap">
<h2>Sort Staff</h2>
<p>Click and Drag to sort the order of staff members</p>
<div id="poststuff" class="tpx_options">
<form action="options.php" method="post">
<?php settings_fields('tpx_options_staff_sort'); ?>
<ul class="tpx_sort_list">
<?php
$args = array(
'post_type' => 'staff',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1
);
$staff_list = get_posts($args);
foreach($staff_list as $staff){
echo '<li class="draggable" id="' . $staff->ID . '">'. $staff->post_title . '<li>';
}
?>
</ul>
<input name="tpx_options_staff_sort[staff_order]" type="hidden" value="" class="sort_value" />
<input name="Submit" type="submit" value="<?php esc_attr_e('Publish'); ?>" class="button-primary" />
</form></div></div>
<?php
}
//
// Staff Overview Page
//
function tpx_options_staff_overview() {
$tpx_options_staff_overview = get_option( 'tpx_options_staff_overview');
?>
<div class="wrap">
<h2>Staff Overview</h2>
<p>This text will appear on the front page of the website.</p>
<div id="poststuff" class="tpx_options tpx_options_staff_overview">
<form action="options.php" method="post" enctype="multipart/form-data" encoding="multipart/form-data" accept-charset="utf-8">
<?php settings_fields('tpx_options_staff_overview'); ?>
<div class="feature_image ">
<h3>Feature Image</h3>
<?php
$attachment_ID = $tpx_options_staff_overview['feature_id'];
tpx_attachment_uploader($attachment_ID, 'feature_attachment', 'photo-medium')
?>
</div>
<div class="tiny_mce_wrapper"><?php the_editor($tpx_options_staff_overview['html'], 'tpx_options_staff_overview[html]', 'title', false, 1); ?></div>
<input name="Submit" type="submit" value="<?php esc_attr_e('Publish'); ?>" class="button-primary" />
</form></div></div>
<?php
}
// add the admin settings and such
add_action('admin_init', 'tpx_staff_admin_init');
function tpx_staff_admin_init(){
register_setting( 'tpx_options_staff_sort', 'tpx_options_staff_sort', 'tpx_options_staff_sort_validate' );
register_setting( 'tpx_options_staff_overview', 'tpx_options_staff_overview', 'tpx_options_staff_overview_validate' );
add_settings_section('tpx_staff_main', 'Main Settings', 'tpx_staff_section_text', 'tpx_staff_section');
add_filter( 'option_page_capability_tpx_options_staff_sort', 'tpx_options_staff_sort_capability' );
}
function tpx_options_staff_sort_capability( $capability ) {
return 'edit_pages';
}
// validate our options
function tpx_options_staff_sort_validate($input) {
$list_string = $input['staff_order'];
if($list_string){
$IDs = explode(',', $list_string);
$count = 0;
//print_r($staff_IDs);
foreach($IDs as $ID){
if($ID != ''){
$staff_post = array();
$staff_post['ID'] = $ID;
$staff_post['menu_order'] = $count;
wp_update_post($staff_post);
$count++;
} else {
unset($ID);
}
}
$newinput['staff_order'] = implode(',', $IDs);
}
return $newinput;
}
// validate our options
function tpx_options_staff_overview_validate($input) {
$attachment_id = tpx_save_attachement($_FILES['feature_attachment'], 0, 0, 0, true);
if($_POST['remove_feature_attachment']) $newinput['feature_id'] = '';
//echo '$attachment_id - ' . $attachment_id;
if($attachment_id) $newinput['feature_id'] = $attachment_id;
$newinput['html'] = $input['html'];
return $newinput;
}
?>