Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Test Plug
- Plugin URI: http://www.nebraskadigital.com/2010/12/27/plugin-organizer/
- Description: Test Plugin
- Version: 0.1
- Author: Jeff Sterup
- Author URI: http://www.jsterup.com
- */
- /* ****************************************** START CUSTOM POST TYPES *************************************************** */
- add_action( 'init', 'add_post_type' );
- function add_post_type() {
- $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' ),
- );
- $settings = array(
- 'labels' => $labels,
- '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,
- 'capability_type' => 'review',
- '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( 'book_category', 'other_books'),
- 'can_export' => true,
- //Permalink endpoint mask, whatever that is.
- );
- register_post_type('review',$settings );
- }
- /* ******************************************* END CUSTOM POST TYPES ************************************************ */
- /* ************************************** START MAPPING META CAPABILITIES ******************************************* */
- /* ************************************** 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 **************************************************** */
- function add_new_roles() {
- //remove_role('reviewer');
- $AdaptRole = get_role('subscriber');
- $caps = $AdaptRole->capabilities;
- $AdaptRole = add_role('reviewer', 'Reviewer', $caps);
- if (!$AdaptRole) {
- $AdaptRole = get_role('reviewer');
- }
- //$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_review');
- $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_review');
- $AdaptRole -> add_cap('edit_reviews');
- $AdaptRole -> add_cap('read_reviews');
- //$AdaptRole -> add_cap('edit_posts');
- $AdaptRole -> add_cap('upload_files');
- $AdaptRole -> add_cap('manage_book_category');
- $AdaptRole -> add_cap('manage_other_books');
- $AdaptRole2 = get_role('administrator'); // So the administrator has access as well :-)
- $AdaptRole2 -> add_cap('delete_others_reviews');
- $AdaptRole2 -> add_cap('delete_review');
- $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_review');
- $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');
- $AdaptRole2 -> add_cap('read_reviews');
- $AdaptRole2 -> add_cap('manage_other_books');
- }
- add_action( 'init', 'add_new_roles'); // Having created your taxonomy with the function create_my_taxonomies this line calls it.
- /* ***************************************** END ADDING ROLES *************************************************** */
- /* *********************************** START CUSTOM TAXONOMIES ************************************************* */
- function create_my_taxonomies() {
- $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' ),
- );
- $settings = array(
- 'public' => true,
- 'show_ui' => true,
- 'show_tagcloud' => true,
- 'show_in_nav_menus' => true,
- 'hierarchical' => false,
- 'labels' => $labels,
- 'query_var' => 'other_books',
- 'rewrite' => array( 'slug' => 'otherbookslug', 'with_front' => false),
- 'capability_type' => 'other_books',
- 'capabilities' => array('assign_terms'=>'edit_reviews','manage_terms' => 'manage_other_books','edit_terms' => 'manage_other_books','delete_terms' => 'manage_other_books'),
- );
- register_taxonomy( 'other_books', array( 'review' ), $settings);
- $labels = array(
- 'name' => __( 'Book Categories' ),
- 'singular_name' => __( 'Book Category' ),
- 'search_items' => __( 'Search Book Categories' ),
- 'popular_items' => __( 'Popular Book Categories' ),
- 'all_items' => __( 'All Book Categories' ),
- 'parent_item' => __( 'Parent Book Category' ),
- 'parent_item_colon' => __( 'Parent Book Category:' ),
- 'edit_item' => __( 'Edit This Book Category' ),
- 'update_item' => __( 'Update Book Category' ),
- 'add_new_item' => __( 'Add Another Book Category' ),
- 'new_item_name' => __( 'New Book Category' ),
- );
- $settings = array(
- 'public' => true,
- 'show_ui' => true,
- 'hierarchical' => true,
- 'labels' => $labels,
- 'rewrite' => array( 'slug' => 'book_category'),
- 'capability_type' => 'book_category',
- 'capabilities' => array('assign_terms'=>'edit_reviews','manage_terms' => 'manage_book_category','edit_terms' => 'manage_book_category','delete_terms' => 'manage_book_category'),
- );
- register_taxonomy( 'book_category', array( 'review' ), $settings);
- }
- 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