_fix_attachment_links

函式
_fix_attachment_links ( $post )
Access
Private
引數
  • (int|object) $post Post ID or post object.
    Required:
返回值
  • (void|int|WP_Error) Void if nothing fixed. 0 or WP_Error on update failure. The post ID on update success.
定義位置
相關方法
the_attachment_linksget_attachment_linkthe_attachment_linkwp_get_attachment_linkget_the_attachment_link
引入
2.3.0
棄用
-

fix_attachment_links: 當附件被嵌入到內容中時,這個函式用來修復附件的連結。它用於確保附件的連結在內容顯示時能正常工作。

用最新的permalinks替換附件錨的hrefs。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function _fix_attachment_links( $post ) {
$post = get_post( $post, ARRAY_A );
$content = $post['post_content'];
// Don't run if no pretty permalinks or post is not published, scheduled, or privately published.
if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ), true ) ) {
return;
}
// Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero).
if ( ! strpos( $content, '?attachment_id=' ) || ! preg_match_all( '/<a ([^>]+)>[sS]+?</a>/', $content, $link_matches ) ) {
return;
}
$site_url = get_bloginfo( 'url' );
$site_url = substr( $site_url, (int) strpos( $site_url, '://' ) ); // Remove the http(s).
$replace = '';
foreach ( $link_matches[1] as $key => $value ) {
if ( ! strpos( $value, '?attachment_id=' ) || ! strpos( $value, 'wp-att-' )
|| ! preg_match( '/href=(["'])[^"']*?attachment_id=(d+)[^"']*\1/', $value, $url_match )
|| ! preg_match( '/rel=["'][^"']*wp-att-(d+)/', $value, $rel_match ) ) {
continue;
}
$quote = $url_match[1]; // The quote (single or double).
$url_id = (int) $url_match[2];
$rel_id = (int) $rel_match[1];
if ( ! $url_id || ! $rel_id || $url_id != $rel_id || strpos( $url_match[0], $site_url ) === false ) {
continue;
}
$link = $link_matches[0][ $key ];
$replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link );
$content = str_replace( $link, $replace, $content );
}
if ( $replace ) {
$post['post_content'] = $content;
// Escape data pulled from DB.
$post = add_magic_quotes( $post );
return wp_update_post( $post );
}
}
function _fix_attachment_links( $post ) { $post = get_post( $post, ARRAY_A ); $content = $post['post_content']; // Don't run if no pretty permalinks or post is not published, scheduled, or privately published. if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ), true ) ) { return; } // Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero). if ( ! strpos( $content, '?attachment_id=' ) || ! preg_match_all( '/<a ([^>]+)>[sS]+?</a>/', $content, $link_matches ) ) { return; } $site_url = get_bloginfo( 'url' ); $site_url = substr( $site_url, (int) strpos( $site_url, '://' ) ); // Remove the http(s). $replace = ''; foreach ( $link_matches[1] as $key => $value ) { if ( ! strpos( $value, '?attachment_id=' ) || ! strpos( $value, 'wp-att-' ) || ! preg_match( '/href=(["'])[^"']*?attachment_id=(d+)[^"']*\1/', $value, $url_match ) || ! preg_match( '/rel=["'][^"']*wp-att-(d+)/', $value, $rel_match ) ) { continue; } $quote = $url_match[1]; // The quote (single or double). $url_id = (int) $url_match[2]; $rel_id = (int) $rel_match[1]; if ( ! $url_id || ! $rel_id || $url_id != $rel_id || strpos( $url_match[0], $site_url ) === false ) { continue; } $link = $link_matches[0][ $key ]; $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link ); $content = str_replace( $link, $replace, $content ); } if ( $replace ) { $post['post_content'] = $content; // Escape data pulled from DB. $post = add_magic_quotes( $post ); return wp_update_post( $post ); } }
function _fix_attachment_links( $post ) {
	$post    = get_post( $post, ARRAY_A );
	$content = $post['post_content'];

	// Don't run if no pretty permalinks or post is not published, scheduled, or privately published.
	if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ), true ) ) {
		return;
	}

	// Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero).
	if ( ! strpos( $content, '?attachment_id=' ) || ! preg_match_all( '/<a ([^>]+)>[sS]+?</a>/', $content, $link_matches ) ) {
		return;
	}

	$site_url = get_bloginfo( 'url' );
	$site_url = substr( $site_url, (int) strpos( $site_url, '://' ) ); // Remove the http(s).
	$replace  = '';

	foreach ( $link_matches[1] as $key => $value ) {
		if ( ! strpos( $value, '?attachment_id=' ) || ! strpos( $value, 'wp-att-' )
			|| ! preg_match( '/href=(["'])[^"']*?attachment_id=(d+)[^"']*\1/', $value, $url_match )
			|| ! preg_match( '/rel=["'][^"']*wp-att-(d+)/', $value, $rel_match ) ) {
				continue;
		}

		$quote  = $url_match[1]; // The quote (single or double).
		$url_id = (int) $url_match[2];
		$rel_id = (int) $rel_match[1];

		if ( ! $url_id || ! $rel_id || $url_id != $rel_id || strpos( $url_match[0], $site_url ) === false ) {
			continue;
		}

		$link    = $link_matches[0][ $key ];
		$replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link );

		$content = str_replace( $link, $replace, $content );
	}

	if ( $replace ) {
		$post['post_content'] = $content;
		// Escape data pulled from DB.
		$post = add_magic_quotes( $post );

		return wp_update_post( $post );
	}
}

常見問題

FAQs
檢視更多 >