Advertisement
Guest User

Untitled

a guest
Jun 5th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. /**
  2. * Plugin Name: Home Page CPT
  3. **/
  4.  
  5. class WP_Homepage_CPT {
  6.  
  7. function __construct() {
  8.  
  9. // add the default homepage on plugin activation
  10. register_activation_hook( __FILE__, array( &$this, 'add_home_page_post' ) );
  11.  
  12. // register the homepage post type
  13. add_action( 'init', array( &$this, 'register_homepage_cpt' ) );
  14.  
  15. // add the menu link
  16. add_action( 'admin_menu', array( &$this, 'edit_homepage_link' ) );
  17. }
  18.  
  19. function edit_homepage_link() {
  20. global $submenu, $pagenow;
  21.  
  22. // query the homepage posts
  23. $homepage = new WP_Query( 'post_type=homepage' );
  24.  
  25. // if its new post page and we have homepage
  26. if ( $pagenow == 'post-new.php' && $homepage->have_posts() ) {
  27. wp_die('You cant add more then one homepage');
  28. }
  29.  
  30. // if we have homepage post, show the edit link else the add homepage link
  31. if ( $homepage->have_posts() ) {
  32. $homepage->the_post();
  33. $link = get_edit_post_link( get_the_ID(), 'return' );
  34. $title = 'Edit Home Page';
  35. } else {
  36. // in case if the user has deleted the default post
  37. $link = get_bloginfo( 'url' ). '/wp-admin/post-new.php?post_type=homepage';
  38. $title = 'Add Home Page';
  39. }
  40. $submenu['edit.php'] = array( array( $title, 'manage_options', $link ) ) + $submenu['edit.php'];
  41. }
  42.  
  43. function register_homepage_cpt() {
  44. $args = array(
  45. 'label' => 'homepage',
  46. 'description' => 'Home Page post type',
  47. 'public' => true,
  48. 'show_in_menu' => false
  49. );
  50. register_post_type( 'homepage', $args );
  51. }
  52.  
  53. function add_home_page_post() {
  54. // on activation first regsiter the post type
  55. $this->register_homepage_cpt();
  56.  
  57. // add the first and only post
  58. $post_data = array(
  59. 'post_title' => 'Home Page',
  60. 'post_type' => 'homepage',
  61. 'post_statue' => 'publish',
  62. 'post_author' => 1
  63. );
  64. wp_insert_post( $post_data );
  65. }
  66.  
  67. }
  68.  
  69. $GLOBALS['wp_homepage_cpt'] = new WP_Homepage_CPT;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement