plugin development - Redirection after saving a post

admin2025-01-07  8

I am developing a plugin. Here I would like to redirect to all custom posts page after saving a custom post. My code is like below.

add_action( 'save_post', [ $this, 'save_meta_values' ] );

public function save_meta_values( $post_id )
    {

       //more code here

       return $post_id;  // I would like to direct here
    }

How can I redirect to all post page like wp_redirect( admin_url( 'edit.php?post_type=news_info' ) ); ?

May be I couldn't express myself properly.

I am developing a plugin. Here I would like to redirect to all custom posts page after saving a custom post. My code is like below.

add_action( 'save_post', [ $this, 'save_meta_values' ] );

public function save_meta_values( $post_id )
    {

       //more code here

       return $post_id;  // I would like to direct here
    }

How can I redirect to all post page like wp_redirect( admin_url( 'edit.php?post_type=news_info' ) ); ?

May be I couldn't express myself properly.

Share Improve this question asked Nov 10, 2024 at 10:01 FoysalFoysal 4451 gold badge5 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

You can redirect this way:

add_action( 'save_post', 'save_meta_values' );

public function save_meta_values( $post_id ) {

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    // More code here ...

    // Check if it's a custom post type names news_info
    if ( 'news_info' === get_post_type( $post_id ) ) {
        // Redirect to the CPT listing page
        wp_redirect( admin_url( 'edit.php?post_type=product' ) );
        exit;
    }

    return $post_id;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260936a702.html

最新回复(0)