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:1462
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.
$0stringThe permalink with placeholder for the post name.$1stringThe 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
- Scaling
- Constant
- Instructions
- 9–149
- Plugin surface
- 2 hooks
- Called by
- 2
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 152.
Third-party callbacks on 'editable_slug', 'get_sample_permalink' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 9 | get_post() |
$name === null && !$ptype | 87 | get_post(), get_post_type_object(), wp_unique_post_slug(), get_permalink(), apply_filters(), apply_filters() |
!$ptype | 97–101 | get_post(), get_post_type_object(), sanitize_title(), wp_unique_post_slug(), get_permalink(), apply_filters(), apply_filters() |
$name === null && $ptype | 104–105 | get_post(), get_post_type_object(), wp_unique_post_slug(), get_permalink(), get_page_uri(), apply_filters(), apply_filters(), apply_filters() |
$name !== null && !$ptype | 110–112 | get_post(), get_post_type_object(), sanitize_title(), sanitize_title(), wp_unique_post_slug(), get_permalink(), apply_filters(), apply_filters() |
$ptype | 114–119 | get_post(), get_post_type_object(), sanitize_title(), wp_unique_post_slug(), get_permalink(), get_page_uri(), apply_filters(), apply_filters(), apply_filters() |
$name === null && $ptype | 123–124 | get_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 && $ptype | 127–130 | get_post(), get_post_type_object(), sanitize_title(), sanitize_title(), wp_unique_post_slug(), get_permalink(), get_page_uri(), apply_filters(), apply_filters(), apply_filters() |
$ptype | 133–138 | get_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 && $ptype | 146–149 | get_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 152 | 9–149 | 8 | |
| 8.5 | 152 | 9–149 | 8 | |
| 8.4 | 152 | 9–149 | 8 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 158 | 9–155 | 8 | |
| 8.2 | 158 | 9–155 | 8 | |
| 8.1 | 158 | 9–155 | 8 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 160 | 9–156 | 8 |
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:
- apply_filters( editable_slug )filterline 1509 (+47 into the body)
Filters the editable slug for a post or term.
- apply_filters( editable_slug )filterline 1517 (+55 into the body)
Filters the editable slug for a post or term.
- apply_filters( get_sample_permalink )filterline 1539 (+77 into the body)
Filters the sample permalink.
Uses · 8
- get_post()Retrieves post data given a post ID or post object.
- get_post_type_object()Retrieves a post type object by name.
- sanitize_title()Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
- wp_unique_post_slug()Computes a unique slug for the post, when given the desired slug and some post details.
- get_permalink()Retrieves the full permalink for the current post or post ID.
- get_page_uri()Builds the URI path for a page.
- untrailingslashit()Removes trailing forward slashes and backslashes if they exist.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 2
- WP_REST_Posts_Controller::prepare_item_for_response()Prepares a single post output for response.
- get_sample_permalink_html()Returns the HTML of the sample permalink slug editor.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.