get_sample_permalink_html

函式
get_sample_permalink_html ( $post, $new_title = null, $new_slug = null )
引數
  • (int|WP_Post) $post Post ID or post object.
    Required:
  • (string|null) $new_title Optional. New title. Default null.
    Required:
    Default: null
  • (string|null) $new_slug Optional. New slug. Default null.
    Required:
    Default: null
返回值
  • (string) The HTML of the sample permalink slug editor.
定義位置
相關方法
get_sample_permalinkget_the_permalinkget_blog_permalinkget_post_permalinkwp_ajax_sample_permalink
引入
2.5.0
棄用
-

get_sample_permalink_html函式是一個WordPress函式,用於檢索一個特定文章的固定連結樣本的HTML標記: 這個函式把文章的ID作為引數,並返回一個樣本固定連結的HTML標記。

返回樣本固定資產slug編輯器的HTML。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_sample_permalink_html( $post, $new_title = null, $new_slug = null ) {
$post = get_post( $post );
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 );
}
}
}
// Permalinks without a post/page name placeholder don't have anything to edit.
if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) {
$return = '<strong>' . __( 'Permalink:' ) . "</strong>n";
if ( false !== $view_link ) {
$display_link = urldecode( $view_link );
$return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>n";
} else {
$return .= '<span id="sample-permalink">' . $permalink . "</span>n";
}
// Encourage a pretty permalink setting.
if ( ! get_option( 'permalink_structure' ) && current_user_can( 'manage_options' )
&& ! ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $post->ID )
) {
$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small">' . __( 'Change Permalink Structure' ) . "</a></span>n";
}
} else {
if ( mb_strlen( $post_name ) > 34 ) {
$post_name_abridged = mb_substr( $post_name, 0, 16 ) . '&hellip;' . mb_substr( $post_name, -16 );
} else {
$post_name_abridged = $post_name;
}
$post_name_html = '<span id="editable-post-name">' . esc_html( $post_name_abridged ) . '</span>';
$display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, esc_html( urldecode( $permalink ) ) );
$return = '<strong>' . __( 'Permalink:' ) . "</strong>n";
$return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>n";
$return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
$return .= '<span id="edit-slug-buttons"><button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="' . __( 'Edit permalink' ) . '">' . __( 'Edit' ) . "</button></span>n";
$return .= '<span id="editable-post-name-full">' . esc_html( $post_name ) . "</span>n";
}
/**
* Filters the sample permalink HTML markup.
*
* @since 2.9.0
* @since 4.4.0 Added `$post` parameter.
*
* @param string $return Sample permalink HTML markup.
* @param int $post_id Post ID.
* @param string $new_title New sample permalink title.
* @param string $new_slug New sample permalink slug.
* @param WP_Post $post Post object.
*/
$return = apply_filters( 'get_sample_permalink_html', $return, $post->ID, $new_title, $new_slug, $post );
return $return;
}
function get_sample_permalink_html( $post, $new_title = null, $new_slug = null ) { $post = get_post( $post ); 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 ); } } } // Permalinks without a post/page name placeholder don't have anything to edit. if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) { $return = '<strong>' . __( 'Permalink:' ) . "</strong>n"; if ( false !== $view_link ) { $display_link = urldecode( $view_link ); $return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>n"; } else { $return .= '<span id="sample-permalink">' . $permalink . "</span>n"; } // Encourage a pretty permalink setting. if ( ! get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && ! ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $post->ID ) ) { $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small">' . __( 'Change Permalink Structure' ) . "</a></span>n"; } } else { if ( mb_strlen( $post_name ) > 34 ) { $post_name_abridged = mb_substr( $post_name, 0, 16 ) . '&hellip;' . mb_substr( $post_name, -16 ); } else { $post_name_abridged = $post_name; } $post_name_html = '<span id="editable-post-name">' . esc_html( $post_name_abridged ) . '</span>'; $display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, esc_html( urldecode( $permalink ) ) ); $return = '<strong>' . __( 'Permalink:' ) . "</strong>n"; $return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>n"; $return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages. $return .= '<span id="edit-slug-buttons"><button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="' . __( 'Edit permalink' ) . '">' . __( 'Edit' ) . "</button></span>n"; $return .= '<span id="editable-post-name-full">' . esc_html( $post_name ) . "</span>n"; } /** * Filters the sample permalink HTML markup. * * @since 2.9.0 * @since 4.4.0 Added `$post` parameter. * * @param string $return Sample permalink HTML markup. * @param int $post_id Post ID. * @param string $new_title New sample permalink title. * @param string $new_slug New sample permalink slug. * @param WP_Post $post Post object. */ $return = apply_filters( 'get_sample_permalink_html', $return, $post->ID, $new_title, $new_slug, $post ); return $return; }
function get_sample_permalink_html( $post, $new_title = null, $new_slug = null ) {
	$post = get_post( $post );

	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 );
			}
		}
	}

	// Permalinks without a post/page name placeholder don't have anything to edit.
	if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) {
		$return = '<strong>' . __( 'Permalink:' ) . "</strong>n";

		if ( false !== $view_link ) {
			$display_link = urldecode( $view_link );
			$return      .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>n";
		} else {
			$return .= '<span id="sample-permalink">' . $permalink . "</span>n";
		}

		// Encourage a pretty permalink setting.
		if ( ! get_option( 'permalink_structure' ) && current_user_can( 'manage_options' )
			&& ! ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $post->ID )
		) {
			$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small">' . __( 'Change Permalink Structure' ) . "</a></span>n";
		}
	} else {
		if ( mb_strlen( $post_name ) > 34 ) {
			$post_name_abridged = mb_substr( $post_name, 0, 16 ) . '&hellip;' . mb_substr( $post_name, -16 );
		} else {
			$post_name_abridged = $post_name;
		}

		$post_name_html = '<span id="editable-post-name">' . esc_html( $post_name_abridged ) . '</span>';
		$display_link   = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, esc_html( urldecode( $permalink ) ) );

		$return  = '<strong>' . __( 'Permalink:' ) . "</strong>n";
		$return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>n";
		$return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
		$return .= '<span id="edit-slug-buttons"><button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="' . __( 'Edit permalink' ) . '">' . __( 'Edit' ) . "</button></span>n";
		$return .= '<span id="editable-post-name-full">' . esc_html( $post_name ) . "</span>n";
	}

	/**
	 * Filters the sample permalink HTML markup.
	 *
	 * @since 2.9.0
	 * @since 4.4.0 Added `$post` parameter.
	 *
	 * @param string  $return    Sample permalink HTML markup.
	 * @param int     $post_id   Post ID.
	 * @param string  $new_title New sample permalink title.
	 * @param string  $new_slug  New sample permalink slug.
	 * @param WP_Post $post      Post object.
	 */
	$return = apply_filters( 'get_sample_permalink_html', $return, $post->ID, $new_title, $new_slug, $post );

	return $return;
}

常見問題

FAQs
檢視更多 >