Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ****************************************** START CUSTOM POST TYPES *************************************************** */
- add_action( 'init', 'add_post_type' );
- function add_post_type() {
- register_post_type('review',
- array(
- 'labels' => array(
- 'name' => __( 'Reviews + Custom Fields' ),
- 'singular_name' => __( 'Post Your Review' ),
- 'add_new' => __( 'Add A New Review' ),
- 'add_new_item' => __( 'Add A New Review' ),
- 'edit' => __( 'Edit Reviews' ),
- 'edit_item' => __( 'Edit Reviews' ),
- 'new_item' => __( 'New Review' ),
- 'view' => __( 'View This Review' ),
- 'view_item' => __( 'View This Review' ),
- 'search_items' => __( 'Search Reviews' ),
- 'not_found' => __( 'No Reviews Found' ),
- 'not_found_in_trash' => __( 'No Reviews Found In Trash' ),
- 'parent' => __( 'Parent Review' ),
- ),
- 'show_ui' => true,
- 'publicly_queryable' => true,
- 'exclude_from_search' => false,
- 'description' => __( 'Brilliant Reviews ' ),
- 'menu_position' => 5,
- 'menu_icon' => get_stylesheet_directory_uri() . '/images/review.png',
- 'hierarchical' => false,
- 'query_var' => true,
- 'capabilities' => array(
- // 'delete_others_posts' => 'delete_others_reviews',
- 'delete_post' => 'delete_review', //METACAPABILITY - DON'T ASSIGN TO A ROLE
- 'delete_posts' => 'delete_reviews',
- 'delete_private_posts' => 'delete_private_reviews',
- 'delete_published_posts' => 'delete_published_reviews',
- // 'edit_others_posts' => 'edit_others_reviews',
- 'edit_post' => 'edit_review', //METACAPABILITY - DON'T ASSIGN TO A ROLE
- 'edit_posts' => 'edit_reviews',
- 'edit_private_posts' => 'edit_private_posts',
- 'edit_published_posts' => 'edit_published_reviews',
- 'publish_posts' => 'publish_reviews',
- 'read_post' => 'read_review', //METACAPABILITY - DON'T ASSIGN TO A ROLE
- 'read_private_posts' => 'read_private_reviews',
- ),
- 'supports' => array( 'title', 'editor', 'comments','trackbacks', 'revisions','author', 'excerpt', 'thumbnail','custom-fields', 'page-attributes' ),
- '_builtin' => false,
- 'rewrite' => array( 'slug' => 'blog', 'with_front' => false ),
- 'taxonomies' => array('post_tag', 'category', 'other_books'),
- 'can_export' => true,
- 'permalink_epmask' => EP_PERMALINK,
- //Permalink endpoint mask, whatever that is.
- )
- );
- }
- /* ******************************************* END CUSTOM POST TYPES ************************************************ */
- /* ************************************** START MAPPING META CAPABILITIES ******************************************* */
- add_filter( 'map_meta_cap', 'my_map_meta_cap', 10, 4 );
- function my_map_meta_cap( $caps, $cap, $user_id, $args ) {
- // If editing, deleting, or reading a review, get the post and post type object.
- if ( 'edit_review' == $cap || 'delete_review' == $cap || 'read_review' == $cap ) {
- $post = get_post( $args[0] );
- $post_type = get_post_type_object( $post->post_type );
- // Set an empty array for the caps.
- $caps = array();
- }
- // If editing a review, assign the required capability.
- if ( 'edit_review' == $cap ) {
- if ( $user_id == $post->post_author )
- $caps[] = $post_type->cap->edit_posts;
- else
- $caps[] = $post_type->cap->edit_others_posts;
- }
- // If deleting a review, assign the required capability.
- elseif ( 'delete_review' == $cap ) {
- if ( $user_id == $post->post_author )
- $caps[] = $post_type->cap->delete_posts;
- else
- $caps[] = $post_type->cap->delete_others_posts;
- }
- // If reading a private review, assign the required capability.
- elseif ( 'read_review' == $cap ) {
- if ( 'private' != $post->post_status )
- $caps[] = 'read';
- elseif ( $user_id == $post->post_author )
- $caps[] = 'read';
- else
- $caps[] = $post_type->cap->read_private_posts;
- }
- // Return the capabilities required by the user.
- return $caps;
- }
- // With thanks to: http://justintadlock.com/archives/2010/07/10/meta-capabilities-for-custom-post-types#comment-232645
- /* ************************************ END MAPPING META CAPABILITIES ****************************************** */
- /* ************************************ START ADDING ROLES **************************************************** */
- //remove_role('reviewer');
- $AdaptRole = get_role('subscriber');
- $caps = $AdaptRole->capabilities;
- add_role('reviewer', 'Reviewer', $caps);
- $AdaptRole = get_role('reviewer'); // Get the role Reviewer, and then add some of the capabilities below.
- // $AdaptRole -> add_cap('delete_others_reviews');
- $AdaptRole -> add_cap('delete_reviews');
- //$AdaptRole -> add_cap('delete_private_reviews');
- //$AdaptRole -> add_cap('delete_published_reviews');
- // $AdaptRole -> add_cap('edit_others_reviews');
- // $AdaptRole -> add_cap('edit_private_reviews');
- //$AdaptRole -> add_cap('edit_published_reviews');
- //$AdaptRole -> add_cap('publish_reviews');
- //$AdaptRole -> add_cap('read_private_reviews');
- $AdaptRole -> add_cap('edit_reviews');
- $AdaptRole -> add_cap('edit_posts');
- $AdaptRole -> add_cap('upload_files');
- $AdaptRole2 = get_role('administrator'); // So the administrator has access as well :-)
- $AdaptRole2 -> add_cap('delete_others_reviews');
- $AdaptRole2 -> add_cap('delete_reviews');
- $AdaptRole2 -> add_cap('delete_private_reviews');
- $AdaptRole2 -> add_cap('delete_published_reviews');
- $AdaptRole2 -> add_cap('edit_others_reviews');
- $AdaptRole2 -> add_cap('edit_reviews');
- $AdaptRole2 -> add_cap('edit_private_reviews');
- $AdaptRole2 -> add_cap('edit_published_reviews');
- $AdaptRole2 -> add_cap('publish_reviews');
- $AdaptRole2 -> add_cap('read_private_reviews');
- /* ***************************************** END ADDING ROLES *************************************************** */
- /* *********************************** START CUSTOM TAXONOMIES ************************************************* */
- function create_my_taxonomies() {
- register_taxonomy(
- 'other_books',
- array( 'post', 'page', 'review' ),
- array(
- 'public' => true,
- 'show_ui' => true,
- 'show_tagcloud' => true,
- 'show_in_nav_menus' => true,
- 'hierarchical' => false,
- 'labels' => array(
- 'name' => __( 'Other Books By This Author' ),
- 'singular_name' => __( 'Another Book By This Author' ),
- 'search_items' => __( 'Search All Other Books By This Author' ),
- 'popular_items' => __( 'Popular Books By This Author' ),
- 'all_items' => __( 'All Other Books' ),
- 'parent_item' => __( 'Parent Term' ),
- 'parent_item_colon' => __( 'Parent Other Books:' ),
- 'edit_item' => __( 'Edit This Term' ),
- 'update_item' => __( 'Update Other Books' ),
- 'add_new_item' => __( 'Add Another Book By This Author' ),
- 'new_item_name' => __( 'New Other Books Name' ),
- ),
- 'query_var' => 'other_books',
- 'rewrite' => array( 'slug' => 'otherbookslug', 'with_front' => false),
- )
- );
- }
- add_action( 'init', 'create_my_taxonomies', 0 ); // Having created your taxonomy with the function create_my_taxonomies this line calls it.
- remove_filter( 'pre_term_description', 'wp_filter_kses' );
- // Allow XHTML in term descriptions e.g. the terms in a taxonomy can have a description, and this allows XHTML (such as an image) in that description.
- /* ******* Start sending specific terms to a single post (where comments are allowed) ********************* */
- add_action( 'template_redirect', 'my_redirect_term_to_post' );
- function my_redirect_term_to_post() {
- global $wp_query;
- if ( is_tax() ) {
- $term = $wp_query->get_queried_object();
- if ( 'other_books' == $term->taxonomy ) {
- $post_id = my_get_post_id_by_slug( $term->slug, 'post' ); // single-default.php will be used to display the content of the post
- // Change post to page or your custom post type e.g. review e.g. $post_id = my_get_post_id_by_slug( $term->slug, 'review' );
- // single-review.php would be used to display the content of the post
- if ( !empty( $post_id ) )
- wp_redirect( get_permalink( $post_id ), 301 );
- }
- }
- }
- function my_get_post_id_by_slug( $slug, $post_type ) {
- global $wpdb;
- $slug = rawurlencode( urldecode( $slug ) );
- $slug = sanitize_title( basename( $slug ) );
- $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = %s", $slug, $post_type ) );
- if ( is_array( $post_id ) )
- return $post_id[0];
- elseif ( !empty( $post_id ) );
- return $post_id;
- return false;
- }
- /* ******* End sending specific terms to a single post (where comments are allowed) ********************* */
- /* ***************************************** END CUSTOM TAXONOMIES ************************************** */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement