wp_get_shortlink

Definition:
function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {}

Return a shortlink for a post, page, attachment, or blog.
This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts. Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output via the get_shortlink filter.

Parameters

  • int $id: A post or blog id. Default is 0, which means the current post or blog.
  • string $context: 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 determine the id and context. Default is ‘post’.
  • bool $allow_slugs: Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.

Return values

returns:A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.

Defined filters

  • pre_get_shortlink
    apply_filters('pre_get_shortlink', false, $id, $context, $allow_slugs)
  • get_shortlink
    apply_filters('get_shortlink', $shortlink, $id, $context, $allow_slugs)

Source code

function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {

	// Allow plugins to short-circuit this function.

	$shortlink = apply_filters('pre_get_shortlink', false, $id, $context, $allow_slugs);

	if ( false !== $shortlink )

		return $shortlink;



	global $wp_query;

	$post_id = 0;

	if ( 'query' == $context && is_single() ) {

		$post_id = $wp_query->get_queried_object_id();

	} elseif ( 'post' == $context ) {

		$post = get_post($id);

		$post_id = $post->ID;

	}



	$shortlink = '';



	// Return p= link for posts.

	if ( !empty($post_id) && '' != get_option('permalink_structure') ) {

		$post = get_post($post_id);

		if ( isset($post->post_type) && 'post' == $post->post_type )

			$shortlink = home_url('?p=' . $post->ID);

	}



	return apply_filters('get_shortlink', $shortlink, $id, $context, $allow_slugs);

}

3749

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: