Preview Button Custom

Ive created a simple plugin which changes the preview link to the correct location as its a custom WordPress install into an existing site framework.

Using ‘preview_post_link’ filter I can change the link successfully to say:

http://www.domain.com/wp-custom.php?p=123456&preview=true

This works perfectly upon hovering over and inspecting the a tag:

<a class="preview button" href="http://www.domain.com/wp-custom.php?p=123456&amp;preview=true" target="wp-preview" id="post-preview">Preview</a>

Right click and open in a new window/tab and it opens fine. Click the actual button though and it fails churning out the url in the address bar of:

http://www.domain.com/?p=123456&preview=true

Not quite sure what is happening or a workaround? Only thing I can think of is maybe some sort of JQuery is ran by wordpress on .preview or #post-preview which is affecting the behavior when the button is clicked directly.

Any ideas of how to remedy would be greatly appreciated. Many thanks.

Updated to show the simple method for amending the wordpress preview links:

function custom_change_preview_link($link) {
    return preg_replace('/\?/','wp-custom.php?',$link);
}
add_filter( 'preview_post_link', 'custom_change_preview_link' );
add_filter( 'preview_page_link', 'custom_change_preview_link' );

1 Answer
1

The only way I found to make it work is to implement the 2nd patch suggested in this trac ticket

I know it’s a patch in the core files, but in the next version of WP (3.6), the change is supposed to be comited, so there shouldn’t be any problems with updates.

Edit

Note/Disclaimer: The following mini-plugin was ripped out of Daniel Bachhubers “Edit-Flow” GitHub Plugin and the patch posted at the ticket. It’s not tested and wasn’t added by the person who answered this question.

<?php
defined( 'ABSPATH' ) OR exit;
/** Plugin Name: Fix Preview Link */
add_filter( 'preview_post_link', 'preview_link_fix' );
function preview_link_fix( $preview_link )
{
    $post = get_post( get_the_ID() );
    if (
        ! is_admin()
        OR 'post.php' != $GLOBALS['pagenow']
    )
        return $preview_link;

    $args = array(
         'p'       => $post->ID
        ,'preview' => 'true'
    );
    return add_query_arg( $args, home_url() );
}

Leave a Comment