wp_force_plain_post_permalink() WordPress Function

The wp_force_plain_post_permalink() function is used to force the WordPress permalink system to return a plain post permalink, without any fancy rewriting. This is useful for plugins and themes that need to generate a plain permalink, without worrying about the current permalink settings.

wp_force_plain_post_permalink( WP_Post|int|null $post = null, bool|null $sample = null ) #

Determine whether post should always use a plain permalink structure.


Parameters

$post

(WP_Post|int|null)(Optional) Post ID or post object. Defaults to global $post.

Default value: null

$sample

(bool|null)(Optional) Whether to force consideration based on sample links. If omitted, a sample link is generated if a post object is passed with the filter property set to 'sample'.

Default value: null


Top ↑

Return

(bool) Whether to use a plain permalink structure.


Top ↑

Source

File: wp-includes/link-template.php

function wp_force_plain_post_permalink( $post = null, $sample = null ) {
	if (
		null === $sample &&
		is_object( $post ) &&
		isset( $post->filter ) &&
		'sample' === $post->filter
	) {
		$sample = true;
	} else {
		$post   = get_post( $post );
		$sample = null !== $sample ? $sample : false;
	}

	if ( ! $post ) {
		return true;
	}

	$post_status_obj = get_post_status_object( get_post_status( $post ) );
	$post_type_obj   = get_post_type_object( get_post_type( $post ) );

	if ( ! $post_status_obj || ! $post_type_obj ) {
		return true;
	}

	if (
		// Publicly viewable links never have plain permalinks.
		is_post_status_viewable( $post_status_obj ) ||
		(
			// Private posts don't have plain permalinks if the user can read them.
			$post_status_obj->private &&
			current_user_can( 'read_post', $post->ID )
		) ||
		// Protected posts don't have plain links if getting a sample URL.
		( $post_status_obj->protected && $sample )
	) {
		return false;
	}

	return true;
}


Top ↑

Changelog

Changelog
VersionDescription
5.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More