| | 2053 | /** |
| | 2054 | * Return a shortlink for a post, page, attachment, or blog. |
| | 2055 | * |
| | 2056 | * Shortlinks are not supported by default. A plugin is required to get shortlink support. |
| | 2057 | * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to |
| | 2058 | * provide the actual shortlinks. |
| | 2059 | * |
| | 2060 | * @param int $id A post or blog id. Default is 0, which means the current post or blog. |
| | 2061 | * @param string $contex Whether the id is a 'blog' id, 'post' id, or 'media' id. If 'post', the post_type of the post is consulted. If 'query', the current query is consulted to determin the id and context. Default is 'post'. |
| | 2062 | * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this. |
| | 2063 | * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled. |
| | 2064 | * @since 3.0.0. |
| | 2065 | */ |
| | 2066 | function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) { |
| | 2067 | // Allow plugins to short-circuit this function. |
| | 2068 | $shortlink = apply_filters('pre_get_shortlink', false, $id, $context, $allow_slugs); |
| | 2069 | if ( false !== $shortlink ) |
| | 2070 | return $shortlink; |
| | 2071 | |
| | 2072 | return apply_filters('get_shortlink', '', $id, $context, $allow_slugs); |
| | 2073 | } |
| | 2074 | |
| | 2075 | function wp_shortlink_wp_head() { |
| | 2076 | $shortlink = get_shortlink(0, 'query'); |
| | 2077 | |
| | 2078 | if ( empty($shortlink) ) |
| | 2079 | return; |
| | 2080 | |
| | 2081 | echo '<link rel="shortlink" href="' . $shortlink . '" />'; |
| | 2082 | } |
| | 2083 | |
| | 2084 | function wp_shortlink_header() { |
| | 2085 | if ( headers_sent() ) |
| | 2086 | return; |
| | 2087 | |
| | 2088 | $shortlink = get_shortlink(0, 'query'); |
| | 2089 | |
| | 2090 | if ( empty($shortlink) ) |
| | 2091 | return; |
| | 2092 | |
| | 2093 | header('Link: <' . $shortlink . '>; rel=shortlink'); |
| | 2094 | } |
| | 2095 | |