I´m trying for a custom post type to make changes on the admin edit post page by a link with params. The problem is when I´m trying to redirect to the page without these custom params thanks wp_redirect().

This is my code in a class:

    add_action('admin_init',array( $this, 'foo_actions' ) );

    function  foo_actions(){
         if( isset( $_GET['foo_action'] ) ){
                // ob_clean();
                // ob_start();
                $order_id = (int) $_GET['post'];
                switch ( $_GET['foo_action'] ) {
                    case ACTION_RENEW_PLAN :
                        Orders::renew_plan($order_id);
                        break;
                    case ACTION_UNRENEW_PLAN :
                        Orders::unrenew_plan($order_id);
                        break;
                    case ACTION_ENABLE_AUTO_PLAN_RENEWED :
                        Orders::enable_auto_plan_renewed($order_id);
                        break;
                    case ACTION_DISABLE_AUTO_PLAN_RENEWED :
                        Orders::disable_auto_plan_renewed($order_id);
                        break;
                } 
                $edit_post_link = get_edit_post_link($order_id);
                wp_redirect( $edit_post_link );
                exit();
         }
    }

This code redirect to /wp-admin/edit.php instead of $edit_post_link. How can I fix this ?

有帮助吗?

解决方案

You just need to change the second parameter for get_edit_post_link(), which is $context and defaults to display. And that parameter determines how to output the ampersand character (i.e. &), which defaults to being encoded as & (because the default context is that the URL is being displayed, hence the & is encoded).

  • When using the URL with redirection, you would want to use the edit (or anything other than display) context:

    $edit_post_link = get_edit_post_link( $order_id, 'edit' );
    wp_redirect( $edit_post_link );
    
  • When displaying the URL, you would use the display context:

    $edit_post_link = get_edit_post_link( $order_id, 'display' );
    
    // Or you can simply ignore the second parameter:
    $edit_post_link = get_edit_post_link( $order_id );
    
    var_dump( $edit_post_link ); // & becomes &
    
许可以下: CC-BY-SA归因
scroll top