WordPress.org

Ready to get started?Download WordPress

Forums

Page preview link on pending document doesn't work (solution) (2 posts)

  1. Tecktron
    Member
    Posted 4 years ago #

    Problem:
    When on "Edit Pages" view, try using the preview link on a document that is pending. Since it has not been publish it doesn't have an actual slug and therefore this link goes to your site's homepage (basically it doesn't work).

    If however you open the page up and use the preview button there, it does work (providing your theme support previews). You then get to a page that has a query of something like this:

    ?preview=true&preview_id=[page id]&preview_nonce=[some hash]

    I was looking for the preview link on "Edit Pages" to also give me that query in a new window.

    Solution:
    I've found that the link is being created using the

    get_permalink($page->ID)

    function. This is found on line 1671 of wp-admin/includes/template.php

    If you look at how the other preview button handles this, it used a form which first create the link like such:

    esc_url(apply_filters('preview_post_link', add_query_arg('preview', 'true', get_permalink($post->ID))));

    Line 87 in wp-admin/edit-page-form.php

    Then the on the forms post back, performs this:

    $url = post_preview();
    wp_redirect($url);

    Line 212 in wp-admin/post.php

    Follow the

    post_preview()

    function though and you find the url is generated by this bit of code:

    $nonce = wp_create_nonce('post_preview_' . $id);
    $url = add_query_arg( array( 'preview' => 'true', 'preview_id' => $id, 'preview_nonce' => $nonce ), get_permalink($id) );

    Line 1145 in wp-admin/includes/post.php

    So knowing this, we can substitute that original

    get_permalink($page->ID)

    in templates/php for this code:

    $nonce = wp_create_nonce('post_preview_' . $page->ID);
    $url = add_query_arg( array( 'preview' => 'true', 'preview_id' => $page->ID, 'preview_nonce' => $nonce ), get_permalink($page->ID) );

    Then add in the url to the markup and add in the target="wp-preview" so line 1671 in wp-admin/includes/template.php goes from this:

    $actions['view'] = '<a href="' . get_permalink($page->ID) . '" title="' . esc_attr(sprintf(__('Preview “%s”'), $title)) . '" rel="permalink">' . __('Preview') . '</a>';

    to this:

    $nonce = wp_create_nonce('post_preview_' . $page->ID);
    $url = add_query_arg( array( 'preview' => 'true', 'preview_id' => $page->ID, 'preview_nonce' => $nonce ), get_permalink($page->ID) );
    $actions['view'] = '<a href="' . $url . '" target="wp-preview" title="' . esc_attr(sprintf(__('Preview “%s”'), $title)) . '" rel="permalink">' . __('Preview') . '</a>';

    Problem Solved.
    Would love to see in the next update.

    Thanks,
    --Tecktron

  2. scribu
    Member
    Posted 4 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic