How add multi-type custom functionality in wordpress site


Add these all in your function.php file under in your theme folder.

/*******User permission Section Start here *********/

/* Fire our meta box setup function on the post editor screen. */
add_action( ‘load-post.php’, ‘smashing_post_meta_boxes_setup’ );
add_action( ‘load-post-new.php’, ‘smashing_post_meta_boxes_setup’ );

/* Meta box setup function. */
function smashing_post_meta_boxes_setup() {

/* Add meta boxes on the ‘add_meta_boxes’ hook. */
add_action( ‘add_meta_boxes’, ‘smashing_add_post_meta_boxes’ );

/* Save post meta on the ‘save_post’ hook. */
add_action( ‘save_post’, ‘smashing_flautist_access_save_meta’, 10, 2 );
}

/* Create one or more meta boxes to be displayed on the post editor screen. */
function smashing_add_post_meta_boxes() {

add_meta_box(
‘smashing-flautist-access’, // Unique ID
esc_html__( ‘Select Attendees’, ‘smashing_flautist’ ), // Title
‘smashing_flautist_access_meta_box’, // Callback function
‘advisory-boards’, // Admin page (or post type)
‘normal’, // Context
‘default’ // Priority
);
}

/* Display the post meta box. */
function smashing_flautist_access_meta_box( $object, $box ) { ?>

ID );

$user_role = new WP_User($user->ID);
$userRoles =$user_role->roles[0];if( $userRoles==’editor’)
{
$displayRole=”Fully Privileged”;
}else
{
$displayRole=”Restricted”;
}if(in_array($user->user_login, get_post_meta( $object->ID, ‘smashing_flautist_access’, true ))) $ifchecked = ‘checked=”checked” ‘;
echo ”
“;echo “$user->user_login”;
echo ”
user_login\” ” . $ifchecked .”/>”;
echo ” $displayRole”;
echo “”;
unset($ifchecked);} ?>

Username Visiblity User Role

post_type );

/* Check if the current user has permission to edit the post. */
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
return $post_id;

/* Get the posted data and sanitize it for use as an HTML class. */
$new_meta_value = ( isset( $_POST[‘smashing-flautist-access’] ) ? ( $_POST[‘smashing-flautist-access’] ) : ” );

/* Get the meta key. */
$meta_key = ‘smashing_flautist_access’;

/* Get the meta value of the custom field key. */
$meta_value = get_post_meta( $post_id, $meta_key, true );

/* If a new meta value was added and there was no previous value, add it. */
if ( $new_meta_value && ” == $meta_value )
{
add_post_meta( $post_id, $meta_key, $new_meta_value, true );
$wpdb->query($wpdb->prepare(“UPDATE $wpdb->posts SET post_status = ‘private’ WHERE ID = “.$post_id.” AND post_type =’advisory-boards'”));
}
/* If the new meta value does not match the old value, update it. */
elseif ( $new_meta_value && $new_meta_value != $meta_value )
{
update_post_meta( $post_id, $meta_key, $new_meta_value );
$wpdb->query($wpdb->prepare(“UPDATE $wpdb->posts SET post_status = ‘private’ WHERE ID = “.$post_id.” AND post_type =’advisory-boards'”));
}
/* If there is no new meta value but an old value exists, delete it. */
elseif ( ” == $new_meta_value && $meta_value )
{
delete_post_meta( $post_id, $meta_key, $meta_value );
$wpdb->query($wpdb->prepare(“UPDATE $wpdb->posts SET post_status = ‘public’ WHERE ID = “.$post_id.” AND post_type =’advisory-boards'”));
}
}

function smashing_title_trim($title) {
$title = attribute_escape($title);
$needles = array(__(‘Protected: ‘),__(‘Private: ‘));
$title = str_replace($needles,”,$title);
return $title;
}
add_filter(‘protected_title_format’,’smashing_title_trim’);
add_filter(‘private_title_format’,’smashing_title_trim’);

$subRole = get_role( ‘subscriber’ );
$subRole->add_cap( ‘read_private_posts’ );

/******User permission Section End here *****/

Add this code for User permission condition

/* Get the post’s acceptable viewer. */
$flautist_access = get_post_meta($post->ID, ‘smashing_flautist_access’, true );
/* Get the post’s current viewer, if he or she is logged in. */
if(is_user_logged_in()) {$current_flautist = $current_user->user_login;}
/* See if the acceptable viewer and the current viewer are the same */
//print_r($flautist_access);

//echo $flautist_access.” == “.$current_flautist;
//echo ‘<br>’.$current_flautist;

if((in_array($current_flautist, $flautist_access)) || current_user_can(‘administrator’))
{
//put private content section here

}

  1. /***************Admin private for admin only*************/

/*
* Hide the admin bar in the front end
*/
add_filter(‘show_admin_bar’, ‘__return_false’);

/*
* Redirects Editor Authors and Subscribers to the site front page using: get_home_url()
*/
add_action(‘admin_init’,’wpse_53675_block_users’);
function wpse_53675_block_users()
{
if( current_user_can(‘author’) || current_user_can(‘subscriber’) || current_user_can(‘editor’))
{
wp_redirect( get_home_url(), 301 );
exit;
}
}

/***************Remove Auto save Section Start here**********/

function disableAutoSave(){
wp_deregister_script(‘autosave’);
}
add_action( ‘wp_print_scripts’, ‘disableAutoSave’ );

/*****************Auto save Section End here***********/

/**************Disable Priview button Section Start here***********/

// Admin Custom Post Page Specific CSS
function posttype_admin_css() {
global $post_type;
if($post_type == ‘meeting’ || $post_type == ‘page’ || $post_type == ‘advisory-boards’) {
echo ”;
}
}
add_action(‘admin_head’, ‘posttype_admin_css’);

/*********Disable Priview button Section End here**********/

Advertisements

Sr. Software Engineer

Posted in Wordpress

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

%d bloggers like this: