Home / admin / Redirect user to new post when published using wp_redirect
  • How to Redirect user to new post when published using wp_redirect

    redirect-user-to-new-post-when-published-using-wp_redirect

    Adding this snippet to the functions.php of your wordpress theme will redirect a user to the new post or page after it is published.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    add_filter('redirect_post_location', 'redirect_to_post_on_publish_or_save');
    function redirect_to_post_on_publish_or_save($location){
        if (isset($_POST['save']) || isset($_POST['publish'])) {
            if (preg_match("/post=([0-9]*)/", $location, $match)) {
                $pl = get_permalink($match[1]);
                if ($pl) {
                    wp_redirect($pl);
                }
            }
        }
    }
    

    ( WordPress codex functions, hooks, in this snippet. )

    wp_redirect, get_permalink, add_filter, wp,