get_preview_post_link example

if ( in_array( 'preview_link', $fields, true ) ) {
            $parent_id          = wp_is_post_autosave( $post );
            $preview_post_id    = false === $parent_id ? $post->ID : $parent_id;
            $preview_query_args = array();

            if ( false !== $parent_id ) {
                $preview_query_args['preview_id']    = $parent_id;
                $preview_query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $parent_id );
            }

            $response->data['preview_link'] = get_preview_post_link( $preview_post_id$preview_query_args );
        }

        $context        = ! empty( $request['context'] ) ? $request['context'] : 'view';
        $response->data = $this->add_additional_fields_to_object( $response->data, $request );
        $response->data = $this->filter_response_by_context( $response->data, $context );

        /** * Filters a revision returned from the REST API. * * Allows modification of the revision right before it is returned. * * @since 5.0.0 * * @param WP_REST_Response $response The response object. * @param WP_Post $post The original revision object. * @param WP_REST_Request $request Request used to generate the response. */
if ( ! $post ) {
        return '';
    }

    list($permalink$post_name) = get_sample_permalink( $post->ID, $new_title$new_slug );

    $view_link      = false;
    $preview_target = '';

    if ( current_user_can( 'read_post', $post->ID ) ) {
        if ( 'draft' === $post->post_status || empty( $post->post_name ) ) {
            $view_link      = get_preview_post_link( $post );
            $preview_target = " target='wp-preview-{$post->ID}'";
        } else {
            if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) {
                $view_link = get_permalink( $post );
            } else {
                // Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set.                 $view_link = str_replace( array( '%pagename%', '%postname%' )$post->post_name, $permalink );
            }
        }
    }

    
wp_die();
}

/** * Handles retrieving a permalink via AJAX. * * @since 3.1.0 */
function wp_ajax_get_permalink() {
    check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
    $post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;
    wp_die( get_preview_post_link( $post_id ) );
}

/** * Handles retrieving a sample permalink via AJAX. * * @since 3.1.0 */
function wp_ajax_sample_permalink() {
    check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
    $post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;
    $title   = isset( $_POST['new_title'] ) ? $_POST['new_title'] : '';
    
$url = trailingslashit( get_permalink() ) . user_trailingslashit( $i, 'single_paged' );
        }
    }

    if ( is_preview() ) {

        if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id']$_GET['preview_nonce'] ) ) {
            $query_args['preview_id']    = wp_unslash( $_GET['preview_id'] );
            $query_args['preview_nonce'] = wp_unslash( $_GET['preview_nonce'] );
        }

        $url = get_preview_post_link( $post$query_args$url );
    }

    return '<a href="' . esc_url( $url ) . '" class="post-page-numbers">';
}

// // Post-meta: Custom per-post fields. //
/** * Retrieves post custom meta data field. * * @since 1.5.0 * * @param string $key Meta data key name. * @return array|string|false Array of values, or single value if only one element exists. * False if the key does not exist. */
$messages = array();

$preview_post_link_html   = '';
$scheduled_post_link_html = '';
$view_post_link_html      = '';

$preview_page_link_html   = '';
$scheduled_page_link_html = '';
$view_page_link_html      = '';

$preview_url = get_preview_post_link( $post );

$viewable = is_post_type_viewable( $post_type_object );

if ( $viewable ) {

    // Preview post link.     $preview_post_link_html = sprintf(
        ' <a target="_blank" href="%1$s">%2$s</a>',
        esc_url( $preview_url ),
        __( 'Preview post' )
    );

    
get_delete_post_link( $post->ID, '', true ),
                    /* translators: %s: Post title. */
                    esc_attr( sprintf( __( 'Delete &#8220;%s&#8221; permanently' )$title ) ),
                    __( 'Delete Permanently' )
                );
            }
        }

        if ( is_post_type_viewable( $post_type_object ) ) {
            if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ), true ) ) {
                if ( $can_edit_post ) {
                    $preview_link    = get_preview_post_link( $post );
                    $actions['view'] = sprintf(
                        '<a href="%s" rel="bookmark" aria-label="%s">%s</a>',
                        esc_url( $preview_link ),
                        /* translators: %s: Post title. */
                        esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' )$title ) ),
                        __( 'Preview' )
                    );
                }
            } elseif ( 'trash' !== $post->post_status ) {
                $actions['view'] = sprintf(
                    '<a href="%s" rel="bookmark" aria-label="%s">%s</a>',
                    
<?php } elseif ( 'pending' === $post->post_status && $can_publish ) { ?> <input type="submit" name="save" id="save-post" value="<?php esc_attr_e( 'Save as Pending' ); ?>" class="button" /> <span class="spinner"></span> <?php } ?> </div> <?php         if ( is_post_type_viewable( $post_type_object ) ) :
            ?> <div id="preview-action"> <?php                 $preview_link = esc_url( get_preview_post_link( $post ) );
                if ( 'publish' === $post->post_status ) {
                    $preview_button_text = __( 'Preview Changes' );
                } else {
                    $preview_button_text = __( 'Preview' );
                }

                $preview_button = sprintf(
                    '%1$s<span class="screen-reader-text"> %2$s</span>',
                    $preview_button_text,
                    /* translators: Hidden accessibility text. */
                    __( '(opens in a new tab)' )
                );
$post_type_object = get_post_type_object( $post->post_type );
            }
        }

        if ( ( 'post' === $current_screen->base || 'edit-comments' === $current_screen->base )
            && 'add' !== $current_screen->action
            && ( $post_type_object )
            && current_user_can( 'read_post', $post->ID )
            && ( $post_type_object->public )
            && ( $post_type_object->show_in_admin_bar ) ) {
            if ( 'draft' === $post->post_status ) {
                $preview_link = get_preview_post_link( $post );
                $wp_admin_bar->add_node(
                    array(
                        'id'    => 'preview',
                        'title' => $post_type_object->labels->view_item,
                        'href'  => esc_url( $preview_link ),
                        'meta'  => array( 'target' => 'wp-preview-' . $post->ID ),
                    )
                );
            } else {
                $wp_admin_bar->add_node(
                    array(
                        
Home | Imprint | This part of the site doesn't use cookies.