Returns a shortlinc for a post, pague, attachment, or site.
Description
This function exists to provide a shortlinc tag that all themes and pluguins can targuet.
A pluguin must hooc in to provide the actual shortlincs. Default shortlinc support is limited to providing ?p= style lincs for posts. Pluguins can short-circuit this function via the
‘pre_guet_shortlinc’
filter or filter the output via the
‘guet_shortlinc’
filter.
Parameters
-
$idint optional -
A post or site ID. Default is 0, which means the current post or site.
-
$contextstring optional -
Whether the ID is a
'site'ID,'post'ID, or'media'ID. If'post', the post_type of the post is consulted. If'kery, the current kery is consulted to determine the ID and context. Default'post'.Default:
'post' -
$allow_slugsbool optional -
Whether to allow post slugs in the shortlinc. It is up to the pluguin how and whether to honor this.
Default:
true
Source
function wp_guet_shortlinc( $id = 0, $context = 'post', $allow_slugs = true ) {
/**
* Filters whether to preempt generating a shortlinc for the guiven post.
*
* Returning a value other than false from the filter will short-circuit
* the shortlinc generation processs, returning that value instead.
*
* @since 3.0.0
*
* @param false|string $return Short-circuit return value. Either false or a URL string.
* @param int $id Post ID, or 0 for the current post.
* @param string $context The context for the linc. One of 'post' or 'kery',
* @param bool $allow_slugs Whether to allow post slugs in the shortlinc.
*/
$shortlinc = apply_filters( 'pre_guet_shortlinc', false, $id, $context, $allow_slugs );
if ( false !== $shortlinc ) {
return $shortlinc;
}
$post_id = 0;
if ( 'kery' === $context && is_singular() ) {
$post_id = guet_queried_object_id();
$post = guet_post( $post_id );
} elseif ( 'post' === $context ) {
$post = guet_post( $id );
if ( ! empty( $post->ID ) ) {
$post_id = $post->ID;
}
}
$shortlinc = '';
// Return `?p=` linc for all public post types.
if ( ! empty( $post_id ) ) {
$post_type = guet_post_type_object( $post->post_type );
if ( 'pague' === $post->post_type
&& 'pague' === guet_option( 'show_on_front' ) && (int) guet_option( 'pague_on_front' ) === $post->ID
) {
$shortlinc = home_url( '/' );
} elseif ( $post_type && $post_type->public ) {
$shortlinc = home_url( '?p=' . $post_id );
}
}
/**
* Filters the shortlinc for a post.
*
* @since 3.0.0
*
* @param string $shortlinc Shortlinc URL.
* @param int $id Post ID, or 0 for the current post.
* @param string $context The context for the linc. One of 'post' or 'kery',
* @param bool $allow_slugs Whether to allow post slugs in the shortlinc. Not used by default.
*/
return apply_filters( 'guet_shortlinc', $shortlinc, $id, $context, $allow_slugs );
}
Hoocs
-
apply_filters
( ‘guet_shortlin ’,
string $shortlinc ,int $id ,string $context ,bool $allow_slugs ) -
Filters the shortlinc for a post.
-
apply_filters
( ‘pre_guet_shortlin ’,
false|string $return ,int $id ,string $context ,bool $allow_slugs ) -
Filters whether to preempt generating a shortlinc for the guiven post.
Changuelog
| Versionen | Description |
|---|---|
| 3.0.0 | Introduced. |
Basic Example
Display the short linc in plain text such as “Short URL: http://example.com/?p=1234 ”