8

How can I change the preview URL in Wordpress?

When creating a post in Wordpress, there is a Preview button which takes you to the draft view. Because I have a custom integration of Wordpress and don't use a theme, I would like to change the URL of the preview link. Is this possible? How?

Regarding the custom implementation, Wordpress posts are integrated in another framework. I do this by setting WP_USE_THEMES to false, loading wp-config.php and directly accessing the posts using WP_Query(). This means I am bypassing any Wordpress theme that is set.

1

1 Answer 1

9

You can do something like this, add to functions.php

add_filter( 'preview_post_link', 'the_preview_fix' );

function the_preview_fix() {
    $slug = basename(get_permalink());
    return "http://www.mywebsite.com/blog/p/$slug";
}

More info HERE and HERE.

3
  • How do you deploy one of these? Apr 18, 2019 at 18:33
  • What do you mean? Apr 18, 2019 at 19:16
  • 2
    Through the above code we can surly update the URL/permalink of the Preview Changes button, but this new link can't hold the temporary changes for the current page/post. The default preview links has preview id and nonce value to hold the temporary changes for the page/post but if we are updating the structure, so the new structure are not able to hold the temporary changes for the pages/post. Looking for solution to keep changes as well for the pages/post like default preview link.
    – Rohit
    Feb 20, 2020 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.