3 * Edit post administration panel.
5 * Manage Post actions: post, edit, delete, etc.
8 * @subpackage Administration
11 /** WordPress Administration Bootstrap */
12 require_once( dirname( __FILE__ ) . '/admin.php' );
14 $parent_file = 'edit.php';
15 $submenu_file = 'edit.php';
17 wp_reset_vars( array( 'action' ) );
19 if ( isset( $_GET['post'] ) )
20 $post_id = $post_ID = (int) $_GET['post'];
21 elseif ( isset( $_POST['post_ID'] ) )
22 $post_id = $post_ID = (int) $_POST['post_ID'];
24 $post_id = $post_ID = 0;
26 $post = $post_type = $post_type_object = null;
29 $post = get_post( $post_id );
32 $post_type = $post->post_type;
33 $post_type_object = get_post_type_object( $post_type );
37 * Redirect to previous page.
39 * @param int $post_id Optional. Post ID.
41 function redirect_post($post_id = '') {
42 if ( isset($_POST['save']) || isset($_POST['publish']) ) {
43 $status = get_post_status( $post_id );
45 if ( isset( $_POST['publish'] ) ) {
57 $message = 'draft' == $status ? 10 : 1;
60 $location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) );
61 } elseif ( isset($_POST['addmeta']) && $_POST['addmeta'] ) {
62 $location = add_query_arg( 'message', 2, wp_get_referer() );
63 $location = explode('#', $location);
64 $location = $location[0] . '#postcustom';
65 } elseif ( isset($_POST['deletemeta']) && $_POST['deletemeta'] ) {
66 $location = add_query_arg( 'message', 3, wp_get_referer() );
67 $location = explode('#', $location);
68 $location = $location[0] . '#postcustom';
70 $location = add_query_arg( 'message', 4, get_edit_post_link( $post_id, 'url' ) );
74 * Filter the post redirect destination URL.
78 * @param string $location The destination URL.
79 * @param int $post_id The post ID.
81 wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );
85 if ( isset( $_POST['deletepost'] ) )
87 elseif ( isset($_POST['wp-preview']) && 'dopreview' == $_POST['wp-preview'] )
90 $sendback = wp_get_referer();
92 strpos( $sendback, 'post.php' ) !== false ||
93 strpos( $sendback, 'post-new.php' ) !== false ) {
94 if ( 'attachment' == $post_type ) {
95 $sendback = admin_url( 'upload.php' );
97 $sendback = admin_url( 'edit.php' );
98 $sendback .= ( ! empty( $post_type ) ) ? '?post_type=' . $post_type : '';
101 $sendback = remove_query_arg( array('trashed', 'untrashed', 'deleted', 'ids'), $sendback );
105 case 'post-quickdraft-save':
106 // Check nonce and capabilities
107 $nonce = $_REQUEST['_wpnonce'];
110 // For output of the quickdraft dashboard widget
111 require_once ABSPATH . 'wp-admin/includes/dashboard.php';
113 if ( ! wp_verify_nonce( $nonce, 'add-post' ) )
114 $error_msg = __( 'Unable to submit this form, please refresh and try again.' );
116 if ( ! current_user_can( 'edit_posts' ) )
117 $error_msg = __( 'Oops, you don’t have access to add new drafts.' );
120 return wp_dashboard_quick_press( $error_msg );
122 $post = get_post( $_REQUEST['post_ID'] );
123 check_admin_referer( 'add-' . $post->post_type );
125 $_POST['comment_status'] = get_option( 'default_comment_status' );
126 $_POST['ping_status'] = get_option( 'default_ping_status' );
129 wp_dashboard_quick_press();
134 check_admin_referer( 'add-' . $post_type );
135 $post_id = 'postajaxpost' == $action ? edit_post() : write_post();
136 redirect_post( $post_id );
142 if ( empty( $post_id ) ) {
143 wp_redirect( admin_url('post.php') );
148 wp_die( __( 'You attempted to edit an item that doesn’t exist. Perhaps it was deleted?' ) );
150 if ( ! $post_type_object )
151 wp_die( __( 'Unknown post type.' ) );
153 if ( ! current_user_can( 'edit_post', $post_id ) )
154 wp_die( __( 'You are not allowed to edit this item.' ) );
156 if ( 'trash' == $post->post_status )
157 wp_die( __( 'You can’t edit this item because it is in the Trash. Please restore it and try again.' ) );
159 if ( ! empty( $_GET['get-post-lock'] ) ) {
160 wp_set_post_lock( $post_id );
161 wp_redirect( get_edit_post_link( $post_id, 'url' ) );
165 $post_type = $post->post_type;
166 if ( 'post' == $post_type ) {
167 $parent_file = "edit.php";
168 $submenu_file = "edit.php";
169 $post_new_file = "post-new.php";
170 } elseif ( 'attachment' == $post_type ) {
171 $parent_file = 'upload.php';
172 $submenu_file = 'upload.php';
173 $post_new_file = 'media-new.php';
175 if ( isset( $post_type_object ) && $post_type_object->show_in_menu && $post_type_object->show_in_menu !== true )
176 $parent_file = $post_type_object->show_in_menu;
178 $parent_file = "edit.php?post_type=$post_type";
179 $submenu_file = "edit.php?post_type=$post_type";
180 $post_new_file = "post-new.php?post_type=$post_type";
183 if ( ! wp_check_post_lock( $post->ID ) ) {
184 $active_post_lock = wp_set_post_lock( $post->ID );
186 if ( 'attachment' !== $post_type )
187 wp_enqueue_script('autosave');
190 if ( is_multisite() ) {
191 add_action( 'admin_footer', '_admin_notice_post_locked' );
193 $check_users = get_users( array( 'fields' => 'ID', 'number' => 2 ) );
195 if ( count( $check_users ) > 1 )
196 add_action( 'admin_footer', '_admin_notice_post_locked' );
198 unset( $check_users );
201 $title = $post_type_object->labels->edit_item;
202 $post = get_post($post_id, OBJECT, 'edit');
204 if ( post_type_supports($post_type, 'comments') ) {
205 wp_enqueue_script('admin-comments');
206 enqueue_comment_hotkeys_js();
209 include( ABSPATH . 'wp-admin/edit-form-advanced.php' );
213 case 'editattachment':
214 check_admin_referer('update-post_' . $post_id);
216 // Don't let these be changed
217 unset($_POST['guid']);
218 $_POST['post_type'] = 'attachment';
220 // Update the thumbnail filename
221 $newmeta = wp_get_attachment_metadata( $post_id, true );
222 $newmeta['thumb'] = $_POST['thumb'];
224 wp_update_attachment_metadata( $post_id, $newmeta );
227 check_admin_referer('update-post_' . $post_id);
229 $post_id = edit_post();
231 // Session cookie flag that the post was saved
232 if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) {
233 setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS );
236 redirect_post($post_id); // Send user on their way while we keep working
241 check_admin_referer('trash-post_' . $post_id);
244 wp_die( __( 'The item you are trying to move to the Trash no longer exists.' ) );
246 if ( ! $post_type_object )
247 wp_die( __( 'Unknown post type.' ) );
249 if ( ! current_user_can( 'delete_post', $post_id ) )
250 wp_die( __( 'You are not allowed to move this item to the Trash.' ) );
252 if ( $user_id = wp_check_post_lock( $post_id ) ) {
253 $user = get_userdata( $user_id );
254 wp_die( sprintf( __( 'You cannot move this item to the Trash. %s is currently editing.' ), $user->display_name ) );
257 if ( ! wp_trash_post( $post_id ) )
258 wp_die( __( 'Error in moving to Trash.' ) );
260 wp_redirect( add_query_arg( array('trashed' => 1, 'ids' => $post_id), $sendback ) );
264 check_admin_referer('untrash-post_' . $post_id);
267 wp_die( __( 'The item you are trying to restore from the Trash no longer exists.' ) );
269 if ( ! $post_type_object )
270 wp_die( __( 'Unknown post type.' ) );
272 if ( ! current_user_can( 'delete_post', $post_id ) )
273 wp_die( __( 'You are not allowed to move this item out of the Trash.' ) );
275 if ( ! wp_untrash_post( $post_id ) )
276 wp_die( __( 'Error in restoring from Trash.' ) );
278 wp_redirect( add_query_arg('untrashed', 1, $sendback) );
282 check_admin_referer('delete-post_' . $post_id);
285 wp_die( __( 'This item has already been deleted.' ) );
287 if ( ! $post_type_object )
288 wp_die( __( 'Unknown post type.' ) );
290 if ( ! current_user_can( 'delete_post', $post_id ) )
291 wp_die( __( 'You are not allowed to delete this item.' ) );
293 $force = ! EMPTY_TRASH_DAYS;
294 if ( $post->post_type == 'attachment' ) {
295 $force = ( $force || ! MEDIA_TRASH );
296 if ( ! wp_delete_attachment( $post_id, $force ) )
297 wp_die( __( 'Error in deleting.' ) );
299 if ( ! wp_delete_post( $post_id, $force ) )
300 wp_die( __( 'Error in deleting.' ) );
303 wp_redirect( add_query_arg('deleted', 1, $sendback) );
307 check_admin_referer( 'update-post_' . $post_id );
309 $url = post_preview();
315 wp_redirect( admin_url('edit.php') );
318 include( ABSPATH . 'wp-admin/admin-footer.php' );