Hướng dẫn get the permalink filter wordpress - lấy bộ lọc permalink wordpress

Lưu ý rằng bộ lọc post_link chỉ dành cho loại bài post.

Đối với các loại bài đăng khác, các bộ lọc này có sẵn:

  • post_type_link cho các loại bài đăng tùy chỉnh
  • page_link cho trang
  • attachment_link để đính kèm

get_permalink()Function thực sự là một trình bao bọc cho:

  • add_filter( 'wpse_link', function(  $url, $post_id, $sample, $type )
    {
        return $url;
    }, 10, 4 );
    
    0
  • add_filter( 'wpse_link', function(  $url, $post_id, $sample, $type )
    {
        return $url;
    }, 10, 4 );
    
    1
  • add_filter( 'wpse_link', function(  $url, $post_id, $sample, $type )
    {
        return $url;
    }, 10, 4 );
    
    2

Trong những trường hợp đó.

Đây là một cách (chưa được kiểm tra) để tạo bộ lọc

add_filter( 'wpse_link', function(  $url, $post_id, $sample, $type )
{
    return $url;
}, 10, 4 );
3 tùy chỉnh cho tất cả các trường hợp trên get_permalink():

foreach( [ 'post', 'page', 'attachment', 'post_type' ] as $type )
{
    add_filter( $type . '_link', function ( $url, $post_id, ? bool $sample = null ) use ( $type )
    {
        return apply_filters( 'wpse_link', $url, $post_id, $sample, $type );
    }, 9999, 3 );
}

Bây giờ chúng ta có thể lọc tất cả các trường hợp với:

add_filter( 'wpse_link', function(  $url, $post_id, $sample, $type )
{
    return $url;
}, 10, 4 );

get_permalink (int | wp_post & nbsp; $ post, bool & nbsp; $ Leavename & nbsp; = & nbsp; false & nbsp;)( 'the_permalink', string $permalink, int|WP_Post $post )

Lấy toàn bộ permalink cho bài đăng hoặc bài đăng hiện tại.


add_filter( 'wpse_link', function(  $url, $post_id, $sample, $type )
{
    return $url;
}, 10, 4 );
6 int | wp_postoptionalstring

Đăng ID hoặc đăng đối tượng. Mặc định là toàn cầu

add_filter( 'wpse_link', function(  $url, $post_id, $sample, $type )
{
    return $url;
}, 10, 4 );
6.

echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );
2 Booloptionalint|WP_Post

Có nên giữ tên bài hoặc tên trang không.


Mặc định:

echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );
3

Lưu ý: Đầu ra của các hàm get_permalink () hoặc get_the_permalink () không được lọc.


Tệp:

add_filter( 'wpse_link', function(  $url, $post_id, $sample, $type )
{
    return $url;
}, 10, 4 );
8. Xem tất cả các tài liệu tham khảo

echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );

Xem trên Tracview trên GitHub View on GitHub



Thay đổi
Phiên bảnSự mô tả
4.4.0 Đã thêm tham số
add_filter( 'wpse_link', function(  $url, $post_id, $sample, $type )
{
    return $url;
}, 10, 4 );
6.
1.5.0 Được giới thiệu.

get_permalink (int | wp_post & nbsp; $ post, bool & nbsp; $ Leavename & nbsp; = & nbsp; false & nbsp;)int|WP_Post $post, bool $leavename = false ): string|false

Lấy toàn bộ permalink cho bài đăng hoặc bài đăng hiện tại.


add_filter( 'wpse_link', function(  $url, $post_id, $sample, $type )
{
    return $url;
}, 10, 4 );
6 int | wp_postoptionalint|WP_Post Optional

Đăng ID hoặc đăng đối tượng. Mặc định là toàn cầu

add_filter( 'wpse_link', function(  $url, $post_id, $sample, $type )
{
    return $url;
}, 10, 4 );
6.

echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );
2 Booloptionalbool Optional

Có nên giữ tên bài hoặc tên trang không.

Mặc định:

echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );
3


Chuỗi | Sai url permalink. Sai nếu bài viết không tồn tại. The permalink URL. False if the post does not exist.


Trong một plugin hoặc chủ đề, nó có thể được sử dụng sớm nhất là hành động

echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );
4. Bất kỳ cách sử dụng trước đó, bao gồm
echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );
5, tạo ra một lỗi nghiêm trọng.

Lưu ý rằng khi được sử dụng bên ngoài vòng lặp trên trang bài đăng (chỉ mục, lưu trữ, v.v.) mà không có tham số ID, nó sẽ trả về URL của bài đăng cuối cùng trong vòng lặp, chứ không phải permalink cho trang hiện tại.


Tệp:

add_filter( 'wpse_link', function(  $url, $post_id, $sample, $type )
{
    return $url;
}, 10, 4 );
8. Xem tất cả các tài liệu tham khảo

function get_permalink( $post = 0, $leavename = false ) {
	$rewritecode = array(
		'%year%',
		'%monthnum%',
		'%day%',
		'%hour%',
		'%minute%',
		'%second%',
		$leavename ? '' : '%postname%',
		'%post_id%',
		'%category%',
		'%author%',
		$leavename ? '' : '%pagename%',
	);

	if ( is_object( $post ) && isset( $post->filter ) && 'sample' === $post->filter ) {
		$sample = true;
	} else {
		$post   = get_post( $post );
		$sample = false;
	}

	if ( empty( $post->ID ) ) {
		return false;
	}

	if ( 'page' === $post->post_type ) {
		return get_page_link( $post, $leavename, $sample );
	} elseif ( 'attachment' === $post->post_type ) {
		return get_attachment_link( $post, $leavename );
	} elseif ( in_array( $post->post_type, get_post_types( array( '_builtin' => false ) ), true ) ) {
		return get_post_permalink( $post, $leavename, $sample );
	}

	$permalink = get_option( 'permalink_structure' );

	/**
	 * Filters the permalink structure for a post before token replacement occurs.
	 *
	 * Only applies to posts with post_type of 'post'.
	 *
	 * @since 3.0.0
	 *
	 * @param string  $permalink The site's permalink structure.
	 * @param WP_Post $post      The post in question.
	 * @param bool    $leavename Whether to keep the post name.
	 */
	$permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );

	if (
		$permalink &&
		! wp_force_plain_post_permalink( $post )
	) {

		$category = '';
		if ( strpos( $permalink, '%category%' ) !== false ) {
			$cats = get_the_category( $post->ID );
			if ( $cats ) {
				$cats = wp_list_sort(
					$cats,
					array(
						'term_id' => 'ASC',
					)
				);

				/**
				 * Filters the category that gets used in the %category% permalink token.
				 *
				 * @since 3.5.0
				 *
				 * @param WP_Term  $cat  The category to use in the permalink.
				 * @param array    $cats Array of all categories (WP_Term objects) associated with the post.
				 * @param WP_Post  $post The post in question.
				 */
				$category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );

				$category_object = get_term( $category_object, 'category' );
				$category        = $category_object->slug;
				if ( $category_object->parent ) {
					$category = get_category_parents( $category_object->parent, false, '/', true ) . $category;
				}
			}
			// Show default category in permalinks,
			// without having to assign it explicitly.
			if ( empty( $category ) ) {
				$default_category = get_term( get_option( 'default_category' ), 'category' );
				if ( $default_category && ! is_wp_error( $default_category ) ) {
					$category = $default_category->slug;
				}
			}
		}

		$author = '';
		if ( strpos( $permalink, '%author%' ) !== false ) {
			$authordata = get_userdata( $post->post_author );
			$author     = $authordata->user_nicename;
		}

		// This is not an API call because the permalink is based on the stored post_date value,
		// which should be parsed as local time regardless of the default PHP timezone.
		$date = explode( ' ', str_replace( array( '-', ':' ), ' ', $post->post_date ) );

		$rewritereplace = array(
			$date[0],
			$date[1],
			$date[2],
			$date[3],
			$date[4],
			$date[5],
			$post->post_name,
			$post->ID,
			$category,
			$author,
			$post->post_name,
		);

		$permalink = home_url( str_replace( $rewritecode, $rewritereplace, $permalink ) );
		$permalink = user_trailingslashit( $permalink, 'single' );

	} else { // If they're not using the fancy permalink option.
		$permalink = home_url( '?p=' . $post->ID );
	}

	/**
	 * Filters the permalink for a post.
	 *
	 * Only applies to posts with post_type of 'post'.
	 *
	 * @since 1.5.0
	 *
	 * @param string  $permalink The post's permalink.
	 * @param WP_Post $post      The post in question.
	 * @param bool    $leavename Whether to keep the post name.
	 */
	return apply_filters( 'post_link', $permalink, $post, $leavename );
}

Xem trên Tracview trên GitHub View on GitHub


Ứng dụng_filters ('post_link', chuỗi $ permalink, wp_post $ post, bool $ Leavename)( 'post_link', string $permalink, WP_Post $post, bool $leavename )

Lọc permalink cho một bài.

Ứng dụng_filters ('post_link_carget', wp_term $ cat, mảng $ mèo, wp_post $ post)( 'post_link_category', WP_Term $cat, array $cats, WP_Post $post )

Lọc danh mục được sử dụng trong mã thông báo % Category % Permalink.

Ứng dụng_filters ('pre_post_link', chuỗi $ permalink, wp_post $ post, bool $ Leavename)( 'pre_post_link', string $permalink, WP_Post $post, bool $leavename )

Lọc cấu trúc permalink cho một bài trước khi thay thế mã thông báo xảy ra.



Thay đổi
Phiên bảnSự mô tả
1.0.0 Được giới thiệu.