Edit File by line
/home/barbar84/www/wp-inclu...
File: link-template.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WordPress Link Template Functions
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Template
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* Displays the permalink for the current post.
[9] Fix | Delete
*
[10] Fix | Delete
* @since 1.2.0
[11] Fix | Delete
* @since 4.4.0 Added the `$post` parameter.
[12] Fix | Delete
*
[13] Fix | Delete
* @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
[14] Fix | Delete
*/
[15] Fix | Delete
function the_permalink( $post = 0 ) {
[16] Fix | Delete
/**
[17] Fix | Delete
* Filters the display of the permalink for the current post.
[18] Fix | Delete
*
[19] Fix | Delete
* @since 1.5.0
[20] Fix | Delete
* @since 4.4.0 Added the `$post` parameter.
[21] Fix | Delete
*
[22] Fix | Delete
* @param string $permalink The permalink for the current post.
[23] Fix | Delete
* @param int|WP_Post $post Post ID, WP_Post object, or 0. Default 0.
[24] Fix | Delete
*/
[25] Fix | Delete
echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );
[26] Fix | Delete
}
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Retrieves a trailing-slashed string if the site is set for adding trailing slashes.
[30] Fix | Delete
*
[31] Fix | Delete
* Conditionally adds a trailing slash if the permalink structure has a trailing
[32] Fix | Delete
* slash, strips the trailing slash if not. The string is passed through the
[33] Fix | Delete
* {@see 'user_trailingslashit'} filter. Will remove trailing slash from string, if
[34] Fix | Delete
* site is not set to have them.
[35] Fix | Delete
*
[36] Fix | Delete
* @since 2.2.0
[37] Fix | Delete
*
[38] Fix | Delete
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
[39] Fix | Delete
*
[40] Fix | Delete
* @param string $string URL with or without a trailing slash.
[41] Fix | Delete
* @param string $type_of_url Optional. The type of URL being considered (e.g. single, category, etc)
[42] Fix | Delete
* for use in the filter. Default empty string.
[43] Fix | Delete
* @return string The URL with the trailing slash appended or stripped.
[44] Fix | Delete
*/
[45] Fix | Delete
function user_trailingslashit( $string, $type_of_url = '' ) {
[46] Fix | Delete
global $wp_rewrite;
[47] Fix | Delete
if ( $wp_rewrite->use_trailing_slashes ) {
[48] Fix | Delete
$string = trailingslashit( $string );
[49] Fix | Delete
} else {
[50] Fix | Delete
$string = untrailingslashit( $string );
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
/**
[54] Fix | Delete
* Filters the trailing-slashed string, depending on whether the site is set to use trailing slashes.
[55] Fix | Delete
*
[56] Fix | Delete
* @since 2.2.0
[57] Fix | Delete
*
[58] Fix | Delete
* @param string $string URL with or without a trailing slash.
[59] Fix | Delete
* @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',
[60] Fix | Delete
* 'single_feed', 'single_paged', 'commentpaged', 'paged', 'home', 'feed',
[61] Fix | Delete
* 'category', 'page', 'year', 'month', 'day', 'post_type_archive'.
[62] Fix | Delete
*/
[63] Fix | Delete
return apply_filters( 'user_trailingslashit', $string, $type_of_url );
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
/**
[67] Fix | Delete
* Displays the permalink anchor for the current post.
[68] Fix | Delete
*
[69] Fix | Delete
* The permalink mode title will use the post title for the 'a' element 'id'
[70] Fix | Delete
* attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
[71] Fix | Delete
*
[72] Fix | Delete
* @since 0.71
[73] Fix | Delete
*
[74] Fix | Delete
* @param string $mode Optional. Permalink mode. Accepts 'title' or 'id'. Default 'id'.
[75] Fix | Delete
*/
[76] Fix | Delete
function permalink_anchor( $mode = 'id' ) {
[77] Fix | Delete
$post = get_post();
[78] Fix | Delete
switch ( strtolower( $mode ) ) {
[79] Fix | Delete
case 'title':
[80] Fix | Delete
$title = sanitize_title( $post->post_title ) . '-' . $post->ID;
[81] Fix | Delete
echo '<a id="' . $title . '"></a>';
[82] Fix | Delete
break;
[83] Fix | Delete
case 'id':
[84] Fix | Delete
default:
[85] Fix | Delete
echo '<a id="post-' . $post->ID . '"></a>';
[86] Fix | Delete
break;
[87] Fix | Delete
}
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
/**
[91] Fix | Delete
* Determine whether post should always use a plain permalink structure.
[92] Fix | Delete
*
[93] Fix | Delete
* @since 5.7.0
[94] Fix | Delete
*
[95] Fix | Delete
* @param WP_Post|int|null $post Optional. Post ID or post object. Defaults to global $post.
[96] Fix | Delete
* @param bool|null $sample Optional. Whether to force consideration based on sample links.
[97] Fix | Delete
* If omitted, a sample link is generated if a post object is passed
[98] Fix | Delete
* with the filter property set to 'sample'.
[99] Fix | Delete
* @return bool Whether to use a plain permalink structure.
[100] Fix | Delete
*/
[101] Fix | Delete
function wp_force_plain_post_permalink( $post = null, $sample = null ) {
[102] Fix | Delete
if (
[103] Fix | Delete
null === $sample &&
[104] Fix | Delete
is_object( $post ) &&
[105] Fix | Delete
isset( $post->filter ) &&
[106] Fix | Delete
'sample' === $post->filter
[107] Fix | Delete
) {
[108] Fix | Delete
$sample = true;
[109] Fix | Delete
} else {
[110] Fix | Delete
$post = get_post( $post );
[111] Fix | Delete
$sample = null !== $sample ? $sample : false;
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
if ( ! $post ) {
[115] Fix | Delete
return true;
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
$post_status_obj = get_post_status_object( get_post_status( $post ) );
[119] Fix | Delete
$post_type_obj = get_post_type_object( get_post_type( $post ) );
[120] Fix | Delete
[121] Fix | Delete
if ( ! $post_status_obj || ! $post_type_obj ) {
[122] Fix | Delete
return true;
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
if (
[126] Fix | Delete
// Publicly viewable links never have plain permalinks.
[127] Fix | Delete
is_post_status_viewable( $post_status_obj ) ||
[128] Fix | Delete
(
[129] Fix | Delete
// Private posts don't have plain permalinks if the user can read them.
[130] Fix | Delete
$post_status_obj->private &&
[131] Fix | Delete
current_user_can( 'read_post', $post->ID )
[132] Fix | Delete
) ||
[133] Fix | Delete
// Protected posts don't have plain links if getting a sample URL.
[134] Fix | Delete
( $post_status_obj->protected && $sample )
[135] Fix | Delete
) {
[136] Fix | Delete
return false;
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
return true;
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
/**
[143] Fix | Delete
* Retrieves the full permalink for the current post or post ID.
[144] Fix | Delete
*
[145] Fix | Delete
* This function is an alias for get_permalink().
[146] Fix | Delete
*
[147] Fix | Delete
* @since 3.9.0
[148] Fix | Delete
*
[149] Fix | Delete
* @see get_permalink()
[150] Fix | Delete
*
[151] Fix | Delete
* @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
[152] Fix | Delete
* @param bool $leavename Optional. Whether to keep post name or page name. Default false.
[153] Fix | Delete
* @return string|false The permalink URL or false if post does not exist.
[154] Fix | Delete
*/
[155] Fix | Delete
function get_the_permalink( $post = 0, $leavename = false ) {
[156] Fix | Delete
return get_permalink( $post, $leavename );
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
/**
[160] Fix | Delete
* Retrieves the full permalink for the current post or post ID.
[161] Fix | Delete
*
[162] Fix | Delete
* @since 1.0.0
[163] Fix | Delete
*
[164] Fix | Delete
* @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`.
[165] Fix | Delete
* @param bool $leavename Optional. Whether to keep post name or page name. Default false.
[166] Fix | Delete
* @return string|false The permalink URL or false if post does not exist.
[167] Fix | Delete
*/
[168] Fix | Delete
function get_permalink( $post = 0, $leavename = false ) {
[169] Fix | Delete
$rewritecode = array(
[170] Fix | Delete
'%year%',
[171] Fix | Delete
'%monthnum%',
[172] Fix | Delete
'%day%',
[173] Fix | Delete
'%hour%',
[174] Fix | Delete
'%minute%',
[175] Fix | Delete
'%second%',
[176] Fix | Delete
$leavename ? '' : '%postname%',
[177] Fix | Delete
'%post_id%',
[178] Fix | Delete
'%category%',
[179] Fix | Delete
'%author%',
[180] Fix | Delete
$leavename ? '' : '%pagename%',
[181] Fix | Delete
);
[182] Fix | Delete
[183] Fix | Delete
if ( is_object( $post ) && isset( $post->filter ) && 'sample' === $post->filter ) {
[184] Fix | Delete
$sample = true;
[185] Fix | Delete
} else {
[186] Fix | Delete
$post = get_post( $post );
[187] Fix | Delete
$sample = false;
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
if ( empty( $post->ID ) ) {
[191] Fix | Delete
return false;
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
if ( 'page' === $post->post_type ) {
[195] Fix | Delete
return get_page_link( $post, $leavename, $sample );
[196] Fix | Delete
} elseif ( 'attachment' === $post->post_type ) {
[197] Fix | Delete
return get_attachment_link( $post, $leavename );
[198] Fix | Delete
} elseif ( in_array( $post->post_type, get_post_types( array( '_builtin' => false ) ), true ) ) {
[199] Fix | Delete
return get_post_permalink( $post, $leavename, $sample );
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
$permalink = get_option( 'permalink_structure' );
[203] Fix | Delete
[204] Fix | Delete
/**
[205] Fix | Delete
* Filters the permalink structure for a post before token replacement occurs.
[206] Fix | Delete
*
[207] Fix | Delete
* Only applies to posts with post_type of 'post'.
[208] Fix | Delete
*
[209] Fix | Delete
* @since 3.0.0
[210] Fix | Delete
*
[211] Fix | Delete
* @param string $permalink The site's permalink structure.
[212] Fix | Delete
* @param WP_Post $post The post in question.
[213] Fix | Delete
* @param bool $leavename Whether to keep the post name.
[214] Fix | Delete
*/
[215] Fix | Delete
$permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
[216] Fix | Delete
[217] Fix | Delete
if (
[218] Fix | Delete
$permalink &&
[219] Fix | Delete
! wp_force_plain_post_permalink( $post )
[220] Fix | Delete
) {
[221] Fix | Delete
[222] Fix | Delete
$category = '';
[223] Fix | Delete
if ( strpos( $permalink, '%category%' ) !== false ) {
[224] Fix | Delete
$cats = get_the_category( $post->ID );
[225] Fix | Delete
if ( $cats ) {
[226] Fix | Delete
$cats = wp_list_sort(
[227] Fix | Delete
$cats,
[228] Fix | Delete
array(
[229] Fix | Delete
'term_id' => 'ASC',
[230] Fix | Delete
)
[231] Fix | Delete
);
[232] Fix | Delete
[233] Fix | Delete
/**
[234] Fix | Delete
* Filters the category that gets used in the %category% permalink token.
[235] Fix | Delete
*
[236] Fix | Delete
* @since 3.5.0
[237] Fix | Delete
*
[238] Fix | Delete
* @param WP_Term $cat The category to use in the permalink.
[239] Fix | Delete
* @param array $cats Array of all categories (WP_Term objects) associated with the post.
[240] Fix | Delete
* @param WP_Post $post The post in question.
[241] Fix | Delete
*/
[242] Fix | Delete
$category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
[243] Fix | Delete
[244] Fix | Delete
$category_object = get_term( $category_object, 'category' );
[245] Fix | Delete
$category = $category_object->slug;
[246] Fix | Delete
if ( $category_object->parent ) {
[247] Fix | Delete
$category = get_category_parents( $category_object->parent, false, '/', true ) . $category;
[248] Fix | Delete
}
[249] Fix | Delete
}
[250] Fix | Delete
// Show default category in permalinks,
[251] Fix | Delete
// without having to assign it explicitly.
[252] Fix | Delete
if ( empty( $category ) ) {
[253] Fix | Delete
$default_category = get_term( get_option( 'default_category' ), 'category' );
[254] Fix | Delete
if ( $default_category && ! is_wp_error( $default_category ) ) {
[255] Fix | Delete
$category = $default_category->slug;
[256] Fix | Delete
}
[257] Fix | Delete
}
[258] Fix | Delete
}
[259] Fix | Delete
[260] Fix | Delete
$author = '';
[261] Fix | Delete
if ( strpos( $permalink, '%author%' ) !== false ) {
[262] Fix | Delete
$authordata = get_userdata( $post->post_author );
[263] Fix | Delete
$author = $authordata->user_nicename;
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
// This is not an API call because the permalink is based on the stored post_date value,
[267] Fix | Delete
// which should be parsed as local time regardless of the default PHP timezone.
[268] Fix | Delete
$date = explode( ' ', str_replace( array( '-', ':' ), ' ', $post->post_date ) );
[269] Fix | Delete
[270] Fix | Delete
$rewritereplace = array(
[271] Fix | Delete
$date[0],
[272] Fix | Delete
$date[1],
[273] Fix | Delete
$date[2],
[274] Fix | Delete
$date[3],
[275] Fix | Delete
$date[4],
[276] Fix | Delete
$date[5],
[277] Fix | Delete
$post->post_name,
[278] Fix | Delete
$post->ID,
[279] Fix | Delete
$category,
[280] Fix | Delete
$author,
[281] Fix | Delete
$post->post_name,
[282] Fix | Delete
);
[283] Fix | Delete
[284] Fix | Delete
$permalink = home_url( str_replace( $rewritecode, $rewritereplace, $permalink ) );
[285] Fix | Delete
$permalink = user_trailingslashit( $permalink, 'single' );
[286] Fix | Delete
[287] Fix | Delete
} else { // If they're not using the fancy permalink option.
[288] Fix | Delete
$permalink = home_url( '?p=' . $post->ID );
[289] Fix | Delete
}
[290] Fix | Delete
[291] Fix | Delete
/**
[292] Fix | Delete
* Filters the permalink for a post.
[293] Fix | Delete
*
[294] Fix | Delete
* Only applies to posts with post_type of 'post'.
[295] Fix | Delete
*
[296] Fix | Delete
* @since 1.5.0
[297] Fix | Delete
*
[298] Fix | Delete
* @param string $permalink The post's permalink.
[299] Fix | Delete
* @param WP_Post $post The post in question.
[300] Fix | Delete
* @param bool $leavename Whether to keep the post name.
[301] Fix | Delete
*/
[302] Fix | Delete
return apply_filters( 'post_link', $permalink, $post, $leavename );
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
/**
[306] Fix | Delete
* Retrieves the permalink for a post of a custom post type.
[307] Fix | Delete
*
[308] Fix | Delete
* @since 3.0.0
[309] Fix | Delete
*
[310] Fix | Delete
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
[311] Fix | Delete
*
[312] Fix | Delete
* @param int|WP_Post $id Optional. Post ID or post object. Default is the global `$post`.
[313] Fix | Delete
* @param bool $leavename Optional. Whether to keep post name. Default false.
[314] Fix | Delete
* @param bool $sample Optional. Is it a sample permalink. Default false.
[315] Fix | Delete
* @return string|WP_Error The post permalink.
[316] Fix | Delete
*/
[317] Fix | Delete
function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
[318] Fix | Delete
global $wp_rewrite;
[319] Fix | Delete
[320] Fix | Delete
$post = get_post( $id );
[321] Fix | Delete
[322] Fix | Delete
if ( is_wp_error( $post ) ) {
[323] Fix | Delete
return $post;
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
$post_link = $wp_rewrite->get_extra_permastruct( $post->post_type );
[327] Fix | Delete
[328] Fix | Delete
$slug = $post->post_name;
[329] Fix | Delete
[330] Fix | Delete
$force_plain_link = wp_force_plain_post_permalink( $post );
[331] Fix | Delete
[332] Fix | Delete
$post_type = get_post_type_object( $post->post_type );
[333] Fix | Delete
[334] Fix | Delete
if ( $post_type->hierarchical ) {
[335] Fix | Delete
$slug = get_page_uri( $post );
[336] Fix | Delete
}
[337] Fix | Delete
[338] Fix | Delete
if ( ! empty( $post_link ) && ( ! $force_plain_link || $sample ) ) {
[339] Fix | Delete
if ( ! $leavename ) {
[340] Fix | Delete
$post_link = str_replace( "%$post->post_type%", $slug, $post_link );
[341] Fix | Delete
}
[342] Fix | Delete
$post_link = home_url( user_trailingslashit( $post_link ) );
[343] Fix | Delete
} else {
[344] Fix | Delete
if ( $post_type->query_var && ( isset( $post->post_status ) && ! $force_plain_link ) ) {
[345] Fix | Delete
$post_link = add_query_arg( $post_type->query_var, $slug, '' );
[346] Fix | Delete
} else {
[347] Fix | Delete
$post_link = add_query_arg(
[348] Fix | Delete
array(
[349] Fix | Delete
'post_type' => $post->post_type,
[350] Fix | Delete
'p' => $post->ID,
[351] Fix | Delete
),
[352] Fix | Delete
''
[353] Fix | Delete
);
[354] Fix | Delete
}
[355] Fix | Delete
$post_link = home_url( $post_link );
[356] Fix | Delete
}
[357] Fix | Delete
[358] Fix | Delete
/**
[359] Fix | Delete
* Filters the permalink for a post of a custom post type.
[360] Fix | Delete
*
[361] Fix | Delete
* @since 3.0.0
[362] Fix | Delete
*
[363] Fix | Delete
* @param string $post_link The post's permalink.
[364] Fix | Delete
* @param WP_Post $post The post in question.
[365] Fix | Delete
* @param bool $leavename Whether to keep the post name.
[366] Fix | Delete
* @param bool $sample Is it a sample permalink.
[367] Fix | Delete
*/
[368] Fix | Delete
return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
/**
[372] Fix | Delete
* Retrieves the permalink for the current page or page ID.
[373] Fix | Delete
*
[374] Fix | Delete
* Respects page_on_front. Use this one.
[375] Fix | Delete
*
[376] Fix | Delete
* @since 1.5.0
[377] Fix | Delete
*
[378] Fix | Delete
* @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`.
[379] Fix | Delete
* @param bool $leavename Optional. Whether to keep the page name. Default false.
[380] Fix | Delete
* @param bool $sample Optional. Whether it should be treated as a sample permalink.
[381] Fix | Delete
* Default false.
[382] Fix | Delete
* @return string The page permalink.
[383] Fix | Delete
*/
[384] Fix | Delete
function get_page_link( $post = false, $leavename = false, $sample = false ) {
[385] Fix | Delete
$post = get_post( $post );
[386] Fix | Delete
[387] Fix | Delete
if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $post->ID ) {
[388] Fix | Delete
$link = home_url( '/' );
[389] Fix | Delete
} else {
[390] Fix | Delete
$link = _get_page_link( $post, $leavename, $sample );
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
/**
[394] Fix | Delete
* Filters the permalink for a page.
[395] Fix | Delete
*
[396] Fix | Delete
* @since 1.5.0
[397] Fix | Delete
*
[398] Fix | Delete
* @param string $link The page's permalink.
[399] Fix | Delete
* @param int $post_id The ID of the page.
[400] Fix | Delete
* @param bool $sample Is it a sample permalink.
[401] Fix | Delete
*/
[402] Fix | Delete
return apply_filters( 'page_link', $link, $post->ID, $sample );
[403] Fix | Delete
}
[404] Fix | Delete
[405] Fix | Delete
/**
[406] Fix | Delete
* Retrieves the page permalink.
[407] Fix | Delete
*
[408] Fix | Delete
* Ignores page_on_front. Internal use only.
[409] Fix | Delete
*
[410] Fix | Delete
* @since 2.1.0
[411] Fix | Delete
* @access private
[412] Fix | Delete
*
[413] Fix | Delete
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
[414] Fix | Delete
*
[415] Fix | Delete
* @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`.
[416] Fix | Delete
* @param bool $leavename Optional. Whether to keep the page name. Default false.
[417] Fix | Delete
* @param bool $sample Optional. Whether it should be treated as a sample permalink.
[418] Fix | Delete
* Default false.
[419] Fix | Delete
* @return string The page permalink.
[420] Fix | Delete
*/
[421] Fix | Delete
function _get_page_link( $post = false, $leavename = false, $sample = false ) {
[422] Fix | Delete
global $wp_rewrite;
[423] Fix | Delete
[424] Fix | Delete
$post = get_post( $post );
[425] Fix | Delete
[426] Fix | Delete
$force_plain_link = wp_force_plain_post_permalink( $post );
[427] Fix | Delete
[428] Fix | Delete
$link = $wp_rewrite->get_page_permastruct();
[429] Fix | Delete
[430] Fix | Delete
if ( ! empty( $link ) && ( ( isset( $post->post_status ) && ! $force_plain_link ) || $sample ) ) {
[431] Fix | Delete
if ( ! $leavename ) {
[432] Fix | Delete
$link = str_replace( '%pagename%', get_page_uri( $post ), $link );
[433] Fix | Delete
}
[434] Fix | Delete
[435] Fix | Delete
$link = home_url( $link );
[436] Fix | Delete
$link = user_trailingslashit( $link, 'page' );
[437] Fix | Delete
} else {
[438] Fix | Delete
$link = home_url( '?page_id=' . $post->ID );
[439] Fix | Delete
}
[440] Fix | Delete
[441] Fix | Delete
/**
[442] Fix | Delete
* Filters the permalink for a non-page_on_front page.
[443] Fix | Delete
*
[444] Fix | Delete
* @since 2.1.0
[445] Fix | Delete
*
[446] Fix | Delete
* @param string $link The page's permalink.
[447] Fix | Delete
* @param int $post_id The ID of the page.
[448] Fix | Delete
*/
[449] Fix | Delete
return apply_filters( '_get_page_link', $link, $post->ID );
[450] Fix | Delete
}
[451] Fix | Delete
[452] Fix | Delete
/**
[453] Fix | Delete
* Retrieves the permalink for an attachment.
[454] Fix | Delete
*
[455] Fix | Delete
* This can be used in the WordPress Loop or outside of it.
[456] Fix | Delete
*
[457] Fix | Delete
* @since 2.0.0
[458] Fix | Delete
*
[459] Fix | Delete
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
[460] Fix | Delete
*
[461] Fix | Delete
* @param int|object $post Optional. Post ID or object. Default uses the global `$post`.
[462] Fix | Delete
* @param bool $leavename Optional. Whether to keep the page name. Default false.
[463] Fix | Delete
* @return string The attachment permalink.
[464] Fix | Delete
*/
[465] Fix | Delete
function get_attachment_link( $post = null, $leavename = false ) {
[466] Fix | Delete
global $wp_rewrite;
[467] Fix | Delete
[468] Fix | Delete
$link = false;
[469] Fix | Delete
[470] Fix | Delete
$post = get_post( $post );
[471] Fix | Delete
$force_plain_link = wp_force_plain_post_permalink( $post );
[472] Fix | Delete
$parent_id = $post->post_parent;
[473] Fix | Delete
$parent = $parent_id ? get_post( $parent_id ) : false;
[474] Fix | Delete
$parent_valid = true; // Default for no parent.
[475] Fix | Delete
if (
[476] Fix | Delete
$parent_id &&
[477] Fix | Delete
(
[478] Fix | Delete
$post->post_parent === $post->ID ||
[479] Fix | Delete
! $parent ||
[480] Fix | Delete
! is_post_type_viewable( get_post_type( $parent ) )
[481] Fix | Delete
)
[482] Fix | Delete
) {
[483] Fix | Delete
// Post is either its own parent or parent post unavailable.
[484] Fix | Delete
$parent_valid = false;
[485] Fix | Delete
}
[486] Fix | Delete
[487] Fix | Delete
if ( $force_plain_link || ! $parent_valid ) {
[488] Fix | Delete
$link = false;
[489] Fix | Delete
} elseif ( $wp_rewrite->using_permalinks() && $parent ) {
[490] Fix | Delete
if ( 'page' === $parent->post_type ) {
[491] Fix | Delete
$parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front.
[492] Fix | Delete
} else {
[493] Fix | Delete
$parentlink = get_permalink( $post->post_parent );
[494] Fix | Delete
}
[495] Fix | Delete
[496] Fix | Delete
if ( is_numeric( $post->post_name ) || false !== strpos( get_option( 'permalink_structure' ), '%category%' ) ) {
[497] Fix | Delete
$name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker.
[498] Fix | Delete
} else {
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function