Change preview post link in WordPress admin
Here we are discussing a scenario where we have the requirement to change the URL of preview button which is showing at the right section under publish block for a particular post. Generally we do not want to modify this URL because it is better to use wordpress default preview functionality in spite of using our custom one.
Look at the image in this post, it will guide you about the link which we are trying to modify.
In some cases we do not want to proceed with the wordpress default preview, we want our custom preview page. So, for this just code for the preview page by using the post id and save it where ever you want. To make this happen, we are using a default wordpress filter that is “preview_post_link”.
This will filter the URL used for a post preview. The same filter will also work for pages, if you want to change the URL for wordpress preview button on pages then this same filter will do this for you.
Please do not use “preview_page_link” filter, this hook is no more in wordpress now, it is deprecated from the wordpress latest versions.
So, just paste the below mentioned code to your functions.php file of your current theme :
function wptricks24_post_preview_link($preview_link, $post) { if ( get_post_status ( $post->ID ) != 'draft' && get_post_status ( $post->ID ) != 'auto-draft' ) { //preview URL for all published posts return home_url()."?preview_id=".$post->ID; //put your new URL here } else { //preview URL for all posts which are in draft return home_url()."?custom_preview=".$post->ID; //put your new URL here } } add_filter( 'preview_post_link', 'wptricks24_post_preview_link', 10, 2 );
Here we are done with the change in link for preview button. We can create separate preview pages for posts for every status which exists, just need to apply the conditions to the above code as per your choice.
In the same manner we can change the URL of the preview button for pages in wordpress admin.
What if I want to keep the default WordPress preview functionality but open the preview page with an appended prefix in the path of the url? How do I achieve that?