wppaste
WordPress

get_sample_permalink( int|WP_Post $post, string|null $title = null, string|null $name = null ): array

Since
2.5.0
Source
wp-admin/includes/post.php:1465
Returns a sample permalink based on the post name.

Compatibility

WordPress
since 2.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$postint|WP_Post
Post ID or post object.
$titlestring|nulloptional
Title to override the post's current title when generating the post name. Default null.Default: null
$namestring|nulloptional
Name to override the post name. Default null.Default: null

Return value

array
Array containing the sample permalink with placeholder for the post name, and the post name.
  • $0string

    The permalink with placeholder for the post name.
  • $1string

    The post name.

Performance profile

How much work a call to get_sample_permalink() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
9–149

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 152.

Plugin surface
2 hooks

Third-party callbacks on 'editable_slug', 'get_sample_permalink' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()one call below get_sample_permalink()

Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 10 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_sample_permalink() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always9get_post()
$name === null && !$ptype87get_post(), get_post_type_object(), wp_unique_post_slug(), get_permalink(), apply_filters(), apply_filters()
!$ptype97–101get_post(), get_post_type_object(), sanitize_title(), wp_unique_post_slug(), get_permalink(), apply_filters(), apply_filters()
$name === null && $ptype104–105get_post(), get_post_type_object(), wp_unique_post_slug(), get_permalink(), get_page_uri(), apply_filters(), apply_filters(), apply_filters()
$name !== null && !$ptype110–112get_post(), get_post_type_object(), sanitize_title(), sanitize_title(), wp_unique_post_slug(), get_permalink(), apply_filters(), apply_filters()
$ptype114–119get_post(), get_post_type_object(), sanitize_title(), wp_unique_post_slug(), get_permalink(), get_page_uri(), apply_filters(), apply_filters(), apply_filters()
$name === null && $ptype123–124get_post(), get_post_type_object(), wp_unique_post_slug(), get_permalink(), get_page_uri(), untrailingslashit(), strrev(), stristr(), strrev(), untrailingslashit(), apply_filters(), apply_filters(), apply_filters()
$name !== null && $ptype127–130get_post(), get_post_type_object(), sanitize_title(), sanitize_title(), wp_unique_post_slug(), get_permalink(), get_page_uri(), apply_filters(), apply_filters(), apply_filters()
$ptype133–138get_post(), get_post_type_object(), sanitize_title(), wp_unique_post_slug(), get_permalink(), get_page_uri(), untrailingslashit(), strrev(), stristr(), strrev(), untrailingslashit(), apply_filters(), apply_filters(), apply_filters()
$name !== null && $ptype146–149get_post(), get_post_type_object(), sanitize_title(), sanitize_title(), wp_unique_post_slug(), get_permalink(), get_page_uri(), untrailingslashit(), strrev(), stristr(), strrev(), untrailingslashit(), apply_filters(), apply_filters(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1529–1498
8.51529–1498
8.41529–14986 fewer instructions than PHP 8.3
8.31589–1558
8.21589–1558
8.11589–15582 fewer instructions than PHP 7.4
7.41609–1568

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 3

3 hooks fire while get_sample_permalink() runs, in this order:

  1. apply_filters( editable_slug )filterline 1512 (+47 into the body)

    Filters the editable slug for a post or term.

  2. apply_filters( editable_slug )filterline 1520 (+55 into the body)

    Filters the editable slug for a post or term.

  3. apply_filters( get_sample_permalink )filterline 1542 (+77 into the body)

    Filters the sample permalink.

Uses · 8

Used by · 2

Source code

function get_sample_permalink( $post, $title = null, $name = null ) {	$post = get_post( $post ); 	if ( ! $post ) {		return array( '', '' );	} 	$ptype = get_post_type_object( $post->post_type ); 	$original_status = $post->post_status;	$original_date   = $post->post_date;	$original_name   = $post->post_name;	$original_filter = $post->filter; 	// Hack: get_permalink() would return plain permalink for drafts, so we will fake that our post is published.	if ( in_array( $post->post_status, array( 'auto-draft', 'draft', 'pending', 'future' ), true ) ) {		$post->post_status = 'publish';		$post->post_name   = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );	} 	/*	 * If the user wants to set a new name -- override the current one.	 * Note: if empty name is supplied -- use the title instead, see #6072.	 */	if ( ! is_null( $name ) ) {		$post->post_name = sanitize_title( $name ? $name : $title, $post->ID );	} 	$post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent ); 	$post->filter = 'sample'; 	$permalink = get_permalink( $post, true ); 	// Replace custom post_type token with generic pagename token for ease of use.	$permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink ); 	// Handle page hierarchy.	if ( $ptype->hierarchical ) {		$uri = get_page_uri( $post );		if ( $uri ) {			$uri = untrailingslashit( $uri );			$uri = strrev( stristr( strrev( $uri ), '/' ) );			$uri = untrailingslashit( $uri );		} 		/** This filter is documented in wp-admin/edit-tag-form.php */		$uri = apply_filters( 'editable_slug', $uri, $post );		if ( ! empty( $uri ) ) {			$uri .= '/';		}		$permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink );	} 	/** This filter is documented in wp-admin/edit-tag-form.php */	$permalink         = array( $permalink, apply_filters( 'editable_slug', $post->post_name, $post ) );	$post->post_status = $original_status;	$post->post_date   = $original_date;	$post->post_name   = $original_name;	$post->filter      = $original_filter; 	/**	 * Filters the sample permalink.	 *	 * @since 4.4.0	 *	 * @param array   $permalink {	 *     Array containing the sample permalink with placeholder for the post name, and the post name.	 *	 *     @type string $0 The permalink with placeholder for the post name.	 *     @type string $1 The post name.	 * }	 * @param int     $post_id Post ID.	 * @param string  $title   Post title.	 * @param string  $name    Post name (slug).	 * @param WP_Post $post    Post object.	 */	return apply_filters( 'get_sample_permalink', $permalink, $post->ID, $title, $name, $post );}

Changelog

Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-admin/includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.