Make WordPress Core


Ignore:
Timestamp:
09/15/2015 04:14:23 AM (9 years ago)
Author:
wonderboymusic
Message:

Create a function, get_preview_post_link(), to DRY the logic for applying the 'preview_post_link' filter to a URL.

Props TomHarrigan, wonderboymusic.
Fixes #24345.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r34160 r34170  
    11601160
    11611161/**
     1162 * Retrieve preview post link.
     1163 *
     1164 * Get the preview post URL. Allow any number of query args to be appended.
     1165 *
     1166 * @since 4.4.0
     1167 *
     1168 * @param int    $post         Optional. Post ID or WP_Post object. Defaults to global post.
     1169 * @param array  $query_args   Optional. If preview query arg should be added. Or array of query args to be added.
     1170 * @param string $preview_link Optional. If a link other than the permalink should be used. Used by _wp_link_page.
     1171 * @return string
     1172 */
     1173function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) {
     1174    $post = get_post( $post );
     1175    if ( ! $post ) {
     1176        return;
     1177    }
     1178
     1179    $post_type_object = get_post_type_object( $post->post_type );
     1180    if ( is_post_type_viewable( $post_type_object ) ) {
     1181        if ( ! $preview_link ) {
     1182            $preview_link = get_permalink( $post );
     1183        }
     1184
     1185        $query_args['preview'] = true;
     1186        $preview_link = add_query_arg( $query_args, $preview_link );
     1187    }
     1188
     1189    /**
     1190     * Filter the URI of a post preview in the post submit box.
     1191     *
     1192     * @since 2.0.5
     1193     * @since 4.4.0 $post parameter was added.
     1194     *
     1195     * @param string  $preview_link URI the user will be directed to for a post preview.
     1196     * @param WP_Post $post         Post object.
     1197     */
     1198    return apply_filters( 'preview_post_link', $preview_link, $post );
     1199}
     1200
     1201/**
    11621202 * Retrieve edit posts link for post.
    11631203 *
Note: See TracChangeset for help on using the changeset viewer.