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

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Wordpress v2.0.11+
Est. reading time N/A
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 www.cyberciti.biz/faq/?p=124&preview=true
// To server1.cyberciti.biz/faq/?p=124&preview=true
function nixcraft_preview_link() {
    $slug = basename(get_permalink());
    $mydomain = 'http://server1.cyberciti.biz';
    $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');

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 7 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
7 comments… add one
  • Paul Thomson Apr 1, 2013 @ 15:31

    Why on earth would you ever need to do this??

    • Mehdi Apr 8, 2013 @ 13:38

      Because sometimes you need a different url structure (for your posts or pages), so the preview url has to reflect the real url structure, or it could show a 404 instead of the post preview.

    • Alex Leonard Feb 7, 2014 @ 2:02

      I have a perfect use-case. I’m using WordPress as an API for post content but the posts are actually appearing on a Laravel driven bespoke web-app.

      As a result WordPress is not handling front-end display of post content at all.

      Thank you NixCraft for sharing this!

  • Terry May 31, 2013 @ 23:17

    I am having a problem where all of my links all show the same preview under them..

    Whatever the top post is reading as a preview is being put under all other posts, any idea why?

  • Matthias Max Jul 10, 2015 @ 20:49

    This doesn’t seem to work for me in WP 4.

    Did anyone else try it?

  • toddkh Nov 19, 2015 @ 17:01

    Thanks for sharing, this is great, I have it working like this: (in wp 4.3.1

    function update_preview_link() {
        $slug = get_the_ID();
        $mydomain = 'http://myreviseddomain.com';
        $mynewpurl = "$mydomain/preview/$slug";
        return "$mynewpurl";
    }
    add_filter( 'preview_post_link', 'update_preview_link' );
    
  • Tim Oct 13, 2016 @ 20:27

    Absolutely needed this today! We build a custom preview engine and needed to change these links globally!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum