img

WordPress Change Preview Post Link Button URL Via preview_post_link Filter

September 8, 2019

How do change the default “preview” button when posting using wordpress user/admin panel?

You need use the hook or filter called preview_post_link under wordpress to change the default “preview” button when posting. According to wordpress filter documentation:

  1. preview_page_link – Applied to the link on the page editing screen that shows the page preview at the bottom (or right) of the screen.
  2. preview_post_link – Applied to the link on the post editing screen that shows the post preview at the bottom (or right) of the screen.

You need to use functions.php a functions file, which resides in the theme subdirectory. This file acts like a plugin, and if it is present in the theme you are using, it is automatically loaded during WordPress initialization. Edit file using a text editor such as vim:
$ vi functions.php
Append the following code:

// Change post preview button url 
// Change sxi.io/?p=124&preview=true
// To server1.sxi.io/faq/?p=124&preview=true
function nixcraft_preview_link() {
    $slug = basename(get_permalink());
    $mydomain = 'http://server1.sxi.io';
    $mydir = '/faq/'; 
    $mynewpurl = "$mydomain$mydir$slug&preview=true";
    return "$mynewpurl";
}
add_filter( 'preview_post_link', 'nixcraft_preview_link' );

Save and close the file. Another example:

function nixcraft_update_post_link($link) {
        //replace www part with server1 using the following php function
        //preg_replace ( patter, replace, subject ) syntax
        $link = preg_replace('/www/', 'server1', $link); 
        return $link;
}
add_filter('preview_post_link', 'nixcraft_update_post_link');

You can use the same filter when previewing to posts from a secure page (taken from secure-admin plugin):

function sa_post_link($link) {
	global $pagenow;
	if ( ('on' == $_SERVER['HTTPS']) && ('wp-comments-post.php' != $pagenow) )
		$link = preg_replace('/^https?/', 'https', $link);
	return $link;
}
add_filter('preview_post_link', 'sa_post_link');
add_filter('preview_page_link', 'sa_post_link');

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Article Tags:
Article Categories:
How To

Comments are closed.