Version Description
Download this release
Release Info
Developer | johnjamesjacoby |
Plugin | Post Type Switcher |
Version | 1.1 |
Comparing to | |
See all releases |
Code changes from version 0.3 to 1.1
- post-type-switcher.php +205 -159
- readme.txt +21 -11
post-type-switcher.php
CHANGED
@@ -1,159 +1,205 @@
|
|
1 |
-
<?php
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
*
|
14 |
-
*
|
15 |
-
*
|
16 |
-
*
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
}
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
/**
|
4 |
+
* Post Type Switcher
|
5 |
+
*
|
6 |
+
* Allow switching of a post type while editing a post (in post publish section)
|
7 |
+
*
|
8 |
+
* @package PostTypeSwitcher
|
9 |
+
* @subpackage Main
|
10 |
+
*/
|
11 |
+
|
12 |
+
/**
|
13 |
+
* Plugin Name: Post Type Switcher
|
14 |
+
* Plugin URI: http://wordpress.org/extend/post-type-switcher/
|
15 |
+
* Description: Allow switching of a post type while editing a post (in post publish section)
|
16 |
+
* Version: 1.1
|
17 |
+
* Author: johnjamesjacoby
|
18 |
+
* Author URI: http://johnjamesjacoby.com
|
19 |
+
*/
|
20 |
+
|
21 |
+
// Exit if accessed directly
|
22 |
+
if ( !defined( 'ABSPATH' ) ) exit;
|
23 |
+
|
24 |
+
/**
|
25 |
+
* The main post type switcher class
|
26 |
+
*
|
27 |
+
* @package PostTypeSwitcher
|
28 |
+
*/
|
29 |
+
final class Post_Type_Switcher {
|
30 |
+
|
31 |
+
/**
|
32 |
+
* Setup the actions needed to execute class methods where needed
|
33 |
+
*
|
34 |
+
* @since PostTypeSwitcher (1.1)
|
35 |
+
*/
|
36 |
+
public function __construct() {
|
37 |
+
|
38 |
+
if ( ! $this->is_allowed_page() )
|
39 |
+
return;
|
40 |
+
|
41 |
+
add_action( 'post_submitbox_misc_actions', array( $this, 'metabox' ) );
|
42 |
+
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
|
43 |
+
add_action( 'admin_head', array( $this, 'admin_head' ) );
|
44 |
+
}
|
45 |
+
|
46 |
+
/**
|
47 |
+
* pts_metabox()
|
48 |
+
*
|
49 |
+
* Adds post_publish metabox to allow changing post_type
|
50 |
+
*
|
51 |
+
* @since PostTypeSwitcher (0.3)
|
52 |
+
*/
|
53 |
+
public function metabox() {
|
54 |
+
|
55 |
+
// Allow types to be filtered, just incase you really need to switch
|
56 |
+
// between crazy types of posts.
|
57 |
+
$args = (array) apply_filters( 'pts_post_type_filter', array(
|
58 |
+
'public' => true,
|
59 |
+
'show_ui' => true
|
60 |
+
) );
|
61 |
+
$post_types = get_post_types( $args, 'objects' );
|
62 |
+
$cpt_object = get_post_type_object( get_post_type() );
|
63 |
+
|
64 |
+
// Bail if object is dirty
|
65 |
+
if ( empty( $cpt_object ) || is_wp_error( $cpt_object ) )
|
66 |
+
return; ?>
|
67 |
+
|
68 |
+
<div class="misc-pub-section misc-pub-section-last post-type-switcher">
|
69 |
+
<label for="pts_post_type"><?php _e( 'Post Type:' ); ?></label>
|
70 |
+
<span id="post-type-display"><?php echo $cpt_object->labels->singular_name; ?></span>
|
71 |
+
|
72 |
+
<?php if ( current_user_can( $cpt_object->cap->publish_posts ) ) : ?>
|
73 |
+
|
74 |
+
<a href="#" id="edit-post-type-switcher" class="hide-if-no-js"><?php _e( 'Edit' ); ?></a>
|
75 |
+
|
76 |
+
<?php wp_nonce_field( 'post-type-selector', 'pts-nonce-select' ); ?>
|
77 |
+
|
78 |
+
<div id="post-type-select">
|
79 |
+
<select name="pts_post_type" id="pts_post_type">
|
80 |
+
|
81 |
+
<?php foreach ( $post_types as $post_type => $pt ) :
|
82 |
+
if ( ! current_user_can( $pt->cap->publish_posts ) )
|
83 |
+
continue;
|
84 |
+
|
85 |
+
echo '<option value="' . esc_attr( $pt->name ) . '"' . selected( get_post_type(), $post_type, false ) . '>' . $pt->labels->singular_name . "</option>\n";
|
86 |
+
endforeach; ?>
|
87 |
+
|
88 |
+
</select>
|
89 |
+
<a href="#" id="save-post-type-switcher" class="hide-if-no-js button"><?php _e( 'OK' ); ?></a>
|
90 |
+
<a href="#" id="cancel-post-type-switcher" class="hide-if-no-js"><?php _e( 'Cancel' ); ?></a>
|
91 |
+
</div>
|
92 |
+
|
93 |
+
<?php endif; ?>
|
94 |
+
|
95 |
+
</div>
|
96 |
+
|
97 |
+
<?php
|
98 |
+
}
|
99 |
+
|
100 |
+
/**
|
101 |
+
* Set the post type on save_post but only when editing
|
102 |
+
*
|
103 |
+
* @since PostTypeSwitcher (0.3)
|
104 |
+
* @param int $post_id
|
105 |
+
* @param object $post
|
106 |
+
* @return If any number of condtions are met
|
107 |
+
*/
|
108 |
+
function save_post( $post_id, $post ) {
|
109 |
+
|
110 |
+
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
|
111 |
+
return;
|
112 |
+
|
113 |
+
if ( ! isset( $_POST['pts-nonce-select'] ) )
|
114 |
+
return;
|
115 |
+
|
116 |
+
if ( ! wp_verify_nonce( $_POST['pts-nonce-select'], 'post-type-selector' ) )
|
117 |
+
return;
|
118 |
+
|
119 |
+
if ( ! current_user_can( 'edit_post', $post_id ) )
|
120 |
+
return;
|
121 |
+
|
122 |
+
if ( empty( $_POST['pts_post_type'] ) )
|
123 |
+
return;
|
124 |
+
|
125 |
+
if ( $_POST['pts_post_type'] == $post->post_type )
|
126 |
+
return;
|
127 |
+
|
128 |
+
if ( ! $new_post_type_object = get_post_type_object( $_POST['pts_post_type'] ) )
|
129 |
+
return;
|
130 |
+
|
131 |
+
if ( ! current_user_can( $new_post_type_object->cap->publish_posts ) )
|
132 |
+
return;
|
133 |
+
|
134 |
+
if ( 'revision' == $post->post_type )
|
135 |
+
return;
|
136 |
+
|
137 |
+
set_post_type( $post_id, $new_post_type_object->name );
|
138 |
+
}
|
139 |
+
|
140 |
+
/**
|
141 |
+
* Adds needed JS and CSS to admin header
|
142 |
+
*
|
143 |
+
* @since PostTypeSwitcher (0.3)
|
144 |
+
* @return If on post-new.php
|
145 |
+
*/
|
146 |
+
function admin_head() {
|
147 |
+
?>
|
148 |
+
|
149 |
+
<script type="text/javascript">
|
150 |
+
jQuery( document ).ready( function( $ ) {
|
151 |
+
jQuery( '.misc-pub-section.curtime.misc-pub-section-last' ).removeClass( 'misc-pub-section-last' );
|
152 |
+
jQuery( '#edit-post-type-switcher' ).click( function(e) {
|
153 |
+
jQuery( this ).hide();
|
154 |
+
jQuery( '#post-type-select' ).slideDown();
|
155 |
+
e.preventDefault();
|
156 |
+
});
|
157 |
+
|
158 |
+
jQuery( '#save-post-type-switcher' ).click( function(e) {
|
159 |
+
jQuery( '#post-type-select' ).slideUp();
|
160 |
+
jQuery( '#edit-post-type-switcher' ).show();
|
161 |
+
jQuery( '#post-type-display' ).text( jQuery( '#pts_post_type :selected' ).text() );
|
162 |
+
e.preventDefault();
|
163 |
+
});
|
164 |
+
|
165 |
+
jQuery( '#cancel-post-type-switcher' ).click( function(e) {
|
166 |
+
jQuery( '#post-type-select' ).slideUp();
|
167 |
+
jQuery( '#edit-post-type-switcher' ).show();
|
168 |
+
e.preventDefault();
|
169 |
+
});
|
170 |
+
});
|
171 |
+
</script>
|
172 |
+
<style type="text/css">
|
173 |
+
#post-type-select {
|
174 |
+
line-height: 2.5em;
|
175 |
+
margin-top: 3px;
|
176 |
+
display: none;
|
177 |
+
}
|
178 |
+
#post-type-display {
|
179 |
+
font-weight: bold;
|
180 |
+
}
|
181 |
+
</style>
|
182 |
+
|
183 |
+
<?php
|
184 |
+
}
|
185 |
+
|
186 |
+
/**
|
187 |
+
* Whether or not the current file requires the post type switcher
|
188 |
+
*
|
189 |
+
* @since PostTypeSwitcher (1.1)
|
190 |
+
* @return bool True if it should load, false if not
|
191 |
+
*/
|
192 |
+
private static function is_allowed_page() {
|
193 |
+
global $pagenow;
|
194 |
+
|
195 |
+
$pages = apply_filters( 'pts_allowed_pages', array(
|
196 |
+
'post.php'
|
197 |
+
) );
|
198 |
+
|
199 |
+
// Only show switcher when editing
|
200 |
+
return (bool) in_array( $pagenow, $pages );
|
201 |
+
}
|
202 |
+
}
|
203 |
+
new Post_Type_Switcher();
|
204 |
+
|
205 |
+
?>
|
readme.txt
CHANGED
@@ -2,24 +2,34 @@
|
|
2 |
Contributors: johnjamesjacoby
|
3 |
Tags: post type
|
4 |
Requires at least: 3.0
|
5 |
-
Tested up to: 3.
|
6 |
-
Stable tag:
|
7 |
|
8 |
A simple way to change a post type in WordPress.
|
9 |
|
10 |
== Description ==
|
11 |
|
12 |
-
|
13 |
|
14 |
-
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
Post to Page
|
18 |
-
Page to Attachment
|
19 |
-
Post to Custom
|
20 |
|
21 |
== Changelog ==
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
= Version 0.3 =
|
24 |
* Use the API to change the post type, fixing a conflict with persistent object caches
|
25 |
* No longer requires JavaScript
|
@@ -32,14 +42,14 @@ Post to Custom
|
|
32 |
|
33 |
== Installation ==
|
34 |
|
35 |
-
* Install the plugin into the plugins/post-type-swticher directory, and activate
|
36 |
* From the post edit screen, above the "Publish" button is the "Post Type" interface.
|
37 |
* Change post types as needed.
|
38 |
|
39 |
== Frequently Asked Questions ==
|
40 |
|
41 |
= Why would I need this? =
|
42 |
-
|
43 |
|
44 |
= Does this ruin my taxonomy associations? =
|
45 |
-
It
|
2 |
Contributors: johnjamesjacoby
|
3 |
Tags: post type
|
4 |
Requires at least: 3.0
|
5 |
+
Tested up to: 3.4
|
6 |
+
Stable tag: 1.1
|
7 |
|
8 |
A simple way to change a post type in WordPress.
|
9 |
|
10 |
== Description ==
|
11 |
|
12 |
+
Any combination is possible, even custom post types:
|
13 |
|
14 |
+
* Page to Post
|
15 |
+
* Post to Page
|
16 |
+
* Page to Attachment
|
17 |
+
* Post to Custom
|
18 |
|
19 |
+
Note: Invisible post types (revisions, menus, etc...) are purposely excluded. Filter 'pts_post_type_filter' to adjust the boundaries.
|
|
|
|
|
|
|
20 |
|
21 |
== Changelog ==
|
22 |
|
23 |
+
= Version 1.1 =
|
24 |
+
* Fix revisions being nooped
|
25 |
+
* Fix malformed HTML for some user roles
|
26 |
+
* Classificationate
|
27 |
+
|
28 |
+
= Version 1.0 =
|
29 |
+
* Fix JS bugs
|
30 |
+
* Audit post save bail conditions
|
31 |
+
* Tweak UI for WordPress 3.3
|
32 |
+
|
33 |
= Version 0.3 =
|
34 |
* Use the API to change the post type, fixing a conflict with persistent object caches
|
35 |
* No longer requires JavaScript
|
42 |
|
43 |
== Installation ==
|
44 |
|
45 |
+
* Install the plugin into the plugins/post-type-swticher directory, and activate.
|
46 |
* From the post edit screen, above the "Publish" button is the "Post Type" interface.
|
47 |
* Change post types as needed.
|
48 |
|
49 |
== Frequently Asked Questions ==
|
50 |
|
51 |
= Why would I need this? =
|
52 |
+
You need to selectively change a posts type from one to another.
|
53 |
|
54 |
= Does this ruin my taxonomy associations? =
|
55 |
+
It should not. This plugin only changes the 'post_type' property of a post.
|