├── README.md
├── _notes
└── dwsync.xml
├── classes
├── _notes
│ └── dwsync.xml
├── class-cahnrswp-news-save.php
└── class-cahnrswp-news.php
├── archive
└── class-cahnrswp-news-announcement.php
└── cahnrswp-plugin-news.php
/README.md:
--------------------------------------------------------------------------------
1 | # cahnrswp-plugin-news
2 | Adds custom post types and taxonomies
3 |
--------------------------------------------------------------------------------
/_notes/dwsync.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/classes/_notes/dwsync.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/archive/class-cahnrswp-news-announcement.php:
--------------------------------------------------------------------------------
1 | true,
12 | 'label' => 'Announcement',
13 | 'supports' => array('title','editor','author','thumbnail','excerpt','revisions'),
14 | 'taxonomies' => array('post_tag','category'),
15 | );
16 |
17 | register_post_type( 'announcement', $args );
18 |
19 | } // end add_post_type
20 |
21 |
22 | }
--------------------------------------------------------------------------------
/cahnrswp-plugin-news.php:
--------------------------------------------------------------------------------
1 | init_plugin();
28 | } // end if
29 |
30 | return self::$instance;
31 |
32 | } // end get_instance
33 |
34 | /**
35 | * Called on new instance of CAHNRS_Media.
36 | */
37 | public function init_plugin(){
38 |
39 | require_once 'classes/class-cahnrswp-news-save.php';
40 |
41 | require_once 'classes/class-cahnrswp-news.php';
42 | $this->news = new CAHNRSWP_News();
43 |
44 | } // end init_plugin
45 |
46 |
47 | } // end CAHNRSWP_Media
48 |
49 | CAHNRSWP_Media::get_instance();
--------------------------------------------------------------------------------
/classes/class-cahnrswp-news-save.php:
--------------------------------------------------------------------------------
1 | array( 'default' => '' , 'clean_type' => 'text' ),
15 | '_cahnrswp_news_article_test' => array( 'default' => '' , 'clean_type' => 'text' ),
16 | );
17 |
18 | /**
19 | * Add actions and set up class
20 | */
21 | public function __construct(){
22 |
23 | // Add post type
24 | add_action( 'init' , array( $this , 'add_post_type' ), 11 );
25 |
26 | // Add edit form
27 | add_action( 'edit_form_after_title' , array( $this , 'add_editor_fields' ) );
28 |
29 | // Save meta data
30 | add_action( 'save_post_news-article' , array( $this , 'save' ), 10 , 3 );
31 |
32 | add_action( 'template_redirect', array( $this , 'redirect' ), 1 );
33 |
34 | } // end __construct
35 |
36 | /**
37 | * Gets post type
38 | * @return string Post_type slug
39 | */
40 | public function get_post_type(){ return $this->post_type;}
41 |
42 | /**
43 | * Get post type fields
44 | * @return array Post type fields
45 | */
46 | public function get_fields(){ return $this->fields;}
47 |
48 | /**
49 | * Register new post type for news
50 | */
51 | public function add_post_type(){
52 |
53 | $args = array(
54 | 'public' => true,
55 | 'label' => 'News',
56 | 'supports' => array( 'title','editor','author','revision','thumbnail' ),
57 | 'taxonomies' => array( 'category' , 'post_tag' ),
58 | );
59 |
60 |
61 | register_post_type( $this->get_post_type() , $args );
62 |
63 | } // end add_post_type
64 |
65 | /**
66 | * Adds editing fields to post type
67 | * @param object $post WP post object
68 | */
69 | public function add_editor_fields( $post ){
70 |
71 | if ( $this->get_post_type() == $post->post_type ){
72 |
73 | $settings = $this->get_settings( $post->ID );
74 |
75 | $html = '
';
76 |
77 | $html .= '
';
78 |
79 | $html .= '
';
81 |
82 | $html .= '
';
83 |
84 | $html .= '
';
85 |
86 | echo $html;
87 |
88 | } // end if
89 |
90 | } // end add_editor_fields
91 |
92 | /**
93 | * Saves meta data
94 | * @param int $post_id ID of post to save
95 | * @param object $post WP Post object
96 | * @param bool $update Whether this is an existing post being updated or not.
97 | */
98 | public function save( $post_id , $post , $update ) {
99 |
100 | $fields = $this->get_fields();
101 |
102 | foreach ( $fields as $key => $field_data ){
103 |
104 | if ( isset( $_POST[ $key ] ) ){
105 |
106 | $data = $this->clean_field( $_POST[ $key ] , $field_data['clean_type'] );
107 |
108 | update_post_meta( $post_id , $key , $data );
109 |
110 | } // end if
111 |
112 | } //end foreach
113 |
114 | } // end save
115 |
116 | /**
117 | * Get post_type settings
118 | * @param string $post_id
119 | * @return array Settings for the post_type
120 | */
121 | public function get_settings( $post_id ){
122 |
123 | $settings = array();
124 |
125 | $fields = $this->get_fields();
126 |
127 | foreach( $fields as $key => $field_data ){
128 |
129 | $meta = get_post_meta( $post_id , $key , true );
130 |
131 | if ( $meta === '' ){
132 |
133 | $settings[ $key ] = $field_data['default'];
134 |
135 | } else {
136 |
137 | $settings[ $key ] = $meta;
138 |
139 | } // end if
140 |
141 | } // end foreach
142 |
143 | return $settings;
144 |
145 | } // end get_settings
146 |
147 | /**
148 | * Redirects page based on field
149 | */
150 | public function redirect(){
151 |
152 | global $wp_query;
153 |
154 | if( is_single() ){
155 |
156 | if ( $wp_query->post->post_type == $this->get_post_type() ){
157 |
158 | $redirect = get_post_meta( $wp_query->post->ID , '_cahnrswp_news_article_redirect' , true );
159 |
160 | if ( $redirect ) {
161 |
162 | wp_redirect( $redirect );
163 |
164 | header( 'Status: 302' );
165 |
166 | exit;
167 |
168 | } // end if
169 |
170 | } // end if
171 |
172 | } // end if
173 |
174 | } // end redirect
175 |
176 | } // end CAHNRSWP_News
--------------------------------------------------------------------------------