• Resolved fabien1203

    (@fabien1203)


    When I click “Publish” after writing a new post, how can I make WP return to the admin page instead staying in the editor? ( with message ‘Post Published. View Post’ ) ?

    in ‘/wp-admin/post.php’, they are that :

    function redirect_post($post_id = '') {
        if ( isset($_POST['save']) || isset($_POST['publish']) ) {
            $status = get_post_status( $post_id );
    
            if ( isset( $_POST['publish'] ) ) {
                switch ( $status ) {
                    case 'pending':
                        $message = 8;
                        break;
                    case 'future':
                        $message = 9;
                        break;
                    default:
                        $message = 6;
                }
            } else {
                    $message = 'draft' == $status ? 10 : 1;
            }
    
            <strong>$location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) );</strong>
        } elseif ( isset($_POST['addmeta']) && $_POST['addmeta'] ) {
            $location = add_query_arg( 'message', 2, wp_get_referer() );
            $location = explode('#', $location);
            $location = $location[0] . '#postcustom';
        } elseif ( isset($_POST['deletemeta']) && $_POST['deletemeta'] ) {
            $location = add_query_arg( 'message', 3, wp_get_referer() );
            $location = explode('#', $location);
            $location = $location[0] . '#postcustom';
        } elseif ( 'post-quickpress-save-cont' == $_POST['action'] ) {
            $location = "post.php?action=edit&post=$post_id&message=7";
        } else {
            $location = add_query_arg( 'message', 4, get_edit_post_link( $post_id, 'url' ) );
        }
    
        wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );
        exit;
    }

    If I change the bold line by :
    wp_redirect( admin_url( 'edit.php' ) );
    or
    $location = add_query_arg( 'message', $message, 'index.php');
    it works but I can’t display message… :/

    (idem for the last line.. it works but no message..)

    How do I do this ?

Viewing 7 replies - 1 through 7 (of 7 total)
  • jocken

    (@jocken)

    have you tried add_filter?

    Thread Starter fabien1203

    (@fabien1203)

    no… can you tell me more?

    Thread Starter fabien1203

    (@fabien1203)

    I continued to modify the code: it works!

    I have modify

    $location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) );

    by

    $arr_params = array ( 'message' => $message, 'id_message' => $post_id );
    $location = add_query_arg( $arr_params, admin_url('index.php') );

    in ‘wp-admin/post.php’ and added

    $post_ID = absint( $_GET['id_message'] );
    $messages = array();
    $messages['post'] = array(
    	 0 => '', // Unused. Messages start at index 1.
    	 1 => sprintf( __('Post updated. <a href="%s">View post</a>'), esc_url( get_permalink($post_ID) ) ),
    	 2 => __('Custom field updated.'),
    	 3 => __('Custom field deleted.'),
    	 4 => __('Post updated.'),
    	/* translators: %s: date and time of the revision */
    	 5 => isset($_GET['revision']) ? sprintf( __('Post restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    	 6 => sprintf( __('Post published. <a href="%s">View post</a>'), esc_url( get_permalink($post_ID) ) ),
    	 7 => __('Post saved.'),
    	 8 => sprintf( __('Post submitted. <a target="_blank" href="%s">Preview post</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    	 9 => sprintf( __('Post scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview post</a>'),
    		// translators: Publish box date format, see http://php.net/date
    		date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
    	10 => sprintf( __('Post draft updated. <a target="_blank" href="%s">Preview post</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    );
    $messages = apply_filters( 'post_updated_messages', $messages );
    
    $message = false;
    if ( isset($_GET['message']) ) {
    	$_GET['message'] = absint( $_GET['message'] );
    	if ( isset($messages[$post_type][$_GET['message']]) )
    		$message = $messages[$post_type][$_GET['message']];
    	elseif ( !isset($messages[$post_type]) && isset($messages['post'][$_GET['message']]) )
    		$message = $messages['post'][$_GET['message']];
    }

    in ‘wp-admin/index.php’ ( This code comes from ‘wp-admin/edit-form-advanced.php’ )

    It works… but is secure ?

    Tim Moore

    (@tmoorewp)

    Security shouldn’t be an issue, but upgrading will be. Anytime WordPress updates, you’ll have to redo these changes and potentially update code.

    You should try using the redirect_post_location filter. Look at this thread for an example: http://wordpress.org/support/topic/redirect-to-new-post-after-publish?replies=7

    Thread Starter fabien1203

    (@fabien1203)

    I’ve tried and it works. But the problem is the same, no ? When WordPress updates, I most also update code for adding this filter, no ?

    Tim Moore

    (@tmoorewp)

    Using a filter means you won’t have to remember which Core file you hacked and where that code is in the file. Yes, you’ll still have to keep the code up to date, but using a filter is the recommended approach (hacking Core files is frowned upon, generally).

    Thread Starter fabien1203

    (@fabien1203)

    Ok, I keep this method.. ( add_filter )
    Thanks 😉

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘redirect to admin page after publishing new post’ is closed to new replies.