2

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' );
5
  • How are you changing the Preview button link?
    – brasofilo
    Feb 5, 2013 at 21:26
  • Ive updated the question to include a simple version of how amending the preview link. As say it shows up correctly on the create post and can right click open in new tab correctly just clicking directly on the button it seems to get overridden.
    – Jimbo
    Feb 6, 2013 at 10:53
  • Seems to be triggered by /wp-includes/js/autosave.js. I was not able to overwrite $('#post-preview').click(function(){});. Maybe removing and replicating the post_submit_meta_box?
    – brasofilo
    Feb 6, 2013 at 17:41
  • I think this is what im going to have to work on with some more research around what your saying. Like I say right clicking and opening in a new tab it doesnt override. Click the button direct and fail.
    – Jimbo
    Feb 11, 2013 at 17:45
  • Having a quick look some jquery is ran which in fact on button click submits the form which pays no attention at all to the url of the button if opened in a new tab. Not quite sure how going to get around. Is there a way of telling wordpress you have a custom file to fire off wordpress instead of index.php such as wp-domain.php ?
    – Jimbo
    Feb 11, 2013 at 17:51

1 Answer 1

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() );
}
1
  • I gave this a bash but it didnt work. I amended it and so forth but still when you click the preview button it fails. This is something occurring with JS which overrides it I think.
    – Jimbo
    Feb 11, 2013 at 17:44

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.