Advertisement
Guest User

1

a guest
Jan 20th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Movie Reviews
  4. Plugin URI: http://wp.tutsplus.com/
  5. Description: Declares a plugin that will create a custom post type
  6. Version: 1.0
  7. Author: Soumitra Chakraborty
  8. Author URI: http://wp.tutsplus.com/
  9. License: GPLv2
  10. */
  11.  
  12. add_action( 'init', 'create_movie_review' );
  13.  
  14.  
  15. function create_movie_review() {
  16. register_post_type( 'movie_reviews',
  17. array(
  18. 'labels' => array(
  19. 'name' => 'Movie Reviews',
  20. 'singular_name' => 'Movie Review',
  21. 'add_new' => 'Add New',
  22. 'add_new_item' => 'Add New Movie Review',
  23. 'edit' => 'Edit',
  24. 'edit_item' => 'Edit Movie Review',
  25. 'new_item' => 'New Movie Review',
  26. 'view' => 'View',
  27. 'view_item' => 'View Movie Review',
  28. 'search_items' => 'Search Movie Reviews',
  29. 'not_found' => 'No Movie Reviews found',
  30. 'not_found_in_trash' =>
  31. 'No Movie Reviews found in Trash',
  32. 'parent' => 'Parent Movie Review'
  33. ),
  34. 'public' => true,
  35. 'menu_position' => 15,
  36. 'supports' =>
  37. array( 'title', 'editor', 'comments',
  38. 'thumbnail', ),
  39. 'taxonomies' => array( '' ),
  40. 'menu_icon' =>
  41. plugins_url( 'images/image.png', __FILE__ ),
  42. 'has_archive' => true
  43. )
  44. );
  45. }
  46.  
  47. add_action( 'admin_init', 'my_admin' );
  48.  
  49.  
  50.  
  51.  
  52. function my_admin() {
  53. add_meta_box( 'movie_review_meta_box',
  54. 'Movie Review Details',
  55. 'display_movie_review_meta_box',
  56. 'movie_reviews', 'normal', 'high' );
  57. }
  58.  
  59. function display_movie_review_meta_box( $movie_review ) {
  60. // Retrieve current name of the Director and Movie Rating based on review ID
  61. $movie_director =
  62. esc_html( get_post_meta( $movie_review->ID,
  63. 'movie_director', true ) );
  64. $movie_rating =
  65. intval( get_post_meta( $movie_review->ID,
  66. 'movie_rating', true ) );
  67. ?>
  68. <table>
  69. <tr>
  70. <td style="width: 100%">Movie Director</td>
  71. <td><input type="text" size="80"
  72. name="movie_review_director_name"
  73. value="<?php echo $movie_director; ?>" /></td>
  74. </tr>
  75. <tr>
  76. <td style="width: 150px">Movie Rating</td>
  77. <td>
  78. <select style="width: 100px"
  79. name="movie_review_rating">
  80. <?php
  81. // Generate all items of drop-down list
  82. for ( $rating = 5; $rating >= 1; $rating -- ) {
  83. ?>
  84. <option value="<?php echo $rating; ?>"
  85. <?php echo selected( $rating,
  86. $movie_rating ); ?>>
  87. <?php echo $rating; ?> stars
  88. <?php } ?>
  89. </select>
  90. </td>
  91. </tr>
  92. </table>
  93. <?php }
  94.  
  95.  
  96. add_action( 'save_post',
  97. 'add_movie_review_fields', 10, 2 );
  98.  
  99.  
  100. function add_movie_review_fields( $movie_review_id,
  101. $movie_review ) {
  102. // Check post type for movie reviews
  103. if ( $movie_review->post_type == 'movie_reviews' ) {
  104. // Store data in post meta table if present in post data
  105. if ( isset( $_POST['movie_review_director_name'] ) &&
  106. $_POST['movie_review_director_name'] != '' ) {
  107. update_post_meta( $movie_review_id, 'movie_director',
  108. $_POST['movie_review_director_name'] );
  109. }
  110. if ( isset( $_POST['movie_review_rating'] ) &&
  111. $_POST['movie_review_rating'] != '' ) {
  112. update_post_meta( $movie_review_id, 'movie_rating',
  113. $_POST['movie_review_rating'] );
  114. }
  115. }
  116. }
  117.  
  118.  
  119. add_filter( 'template_include',
  120. 'include_template_function', 1 );
  121.  
  122.  
  123. function include_template_function( $template_path ) {
  124. if ( get_post_type() == 'movie_reviews' ) {
  125. if ( is_single() ) {
  126. // checks if the file exists in the theme first,
  127. // otherwise serve the file from the plugin
  128. if ( $theme_file = locate_template( array
  129. ( 'single-movie_reviews.php' ) ) ) {
  130. $template_path = $theme_file;
  131. } else {
  132. $template_path = plugin_dir_path( __FILE__ ) .
  133. '/single-movie_reviews.php';
  134. }
  135. }
  136. }
  137. return $template_path;
  138. }
  139.  
  140.  
  141. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement