functions - Redirect to post after publish or update a published post

admin2025-04-21  1

How can I get the permalink of a post from functions.php in my theme? I know this code:

get_permalink($post->ID);

but this produces something like this:

mydomain/index.php?p=123

and I need something like this instead:

mydomain/post-name

Is it possible?

I edit to post my code:

add_filter('redirect_post_location', 'redirect_to_post_on_publish_or_save');

function redirect_to_post_on_publish_or_save($location)

{

    global $post;

    if (

        (isset($_POST['publish']) || isset($_POST['save'])) &&

        preg_match("/post=([0-9]*)/", $location, $match) &&

        $post &&

        $post->ID == $match[1] &&

        (isset($_POST['publish']) || $post->post_status == 'publish') && // Publishing draft or updating published post

        $pl = get_permalink($post->ID)

    ) {

        // Always redirect to the post

        $location = "/".$post->post_name;

    }

    return $location;

}

This are my rewrite rules:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

How can I get the permalink of a post from functions.php in my theme? I know this code:

get_permalink($post->ID);

but this produces something like this:

mydomain/index.php?p=123

and I need something like this instead:

mydomain/post-name

Is it possible?

I edit to post my code:

add_filter('redirect_post_location', 'redirect_to_post_on_publish_or_save');

function redirect_to_post_on_publish_or_save($location)

{

    global $post;

    if (

        (isset($_POST['publish']) || isset($_POST['save'])) &&

        preg_match("/post=([0-9]*)/", $location, $match) &&

        $post &&

        $post->ID == $match[1] &&

        (isset($_POST['publish']) || $post->post_status == 'publish') && // Publishing draft or updating published post

        $pl = get_permalink($post->ID)

    ) {

        // Always redirect to the post

        $location = "http://mydomain/post-type-slug/".$post->post_name;

    }

    return $location;

}

This are my rewrite rules:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
Share Improve this question edited Aug 27, 2014 at 5:46 Cain asked Aug 25, 2014 at 7:12 CainCain 11 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

After see your code, I think that you are trying to redirect the user to the post after publish or update an already published post.

Your problem is not with get_permalink() function nor your permalink settings. Your problem is with the logic of your code. See how you set the value of $pl equal to the result of get_permlink but the returned value is the value of $location. Also, you are setting the value of $pl inside the if comparison statement, which is somehting you shouldn't do.

Try this:

add_filter('redirect_post_location', 'redirect_to_post_on_publish_or_save');
function redirect_to_post_on_publish_or_save($location) {

    global $post;

    if (
        (isset($_POST['publish']) || isset($_POST['save'])) &&

        preg_match("/post=([0-9]*)/", $location, $match) &&

        $post &&

        $post->ID == $match[1] &&

        (isset($_POST['publish']) || $post->post_status == 'publish') // Publishing draft or updating published post

    ) {

        // Always redirect to the post

        $location = get_permalink($post->ID);

    }

    return $location;

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745215378a290815.html

最新回复(0)