diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
index 7feb284e85..587e85a898 100644
a
|
b
|
function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p |
4053 | 4053 | return $slug; |
4054 | 4054 | } |
4055 | 4055 | |
| 4056 | /** |
| 4057 | * Filters the post slug before it is generated to be unique. |
| 4058 | * |
| 4059 | * Returning a non-null value will short-circuit the |
| 4060 | * unique slug generation, returning the passed value instead. |
| 4061 | * |
| 4062 | * @since 5.1.0 |
| 4063 | * |
| 4064 | * @param string $override_slug Short-circuit return value. |
| 4065 | * @param string $slug The desired slug (post_name). |
| 4066 | * @param int $post_ID Post ID. |
| 4067 | * @param string $post_status The post status. |
| 4068 | * @param string $post_type Post type. |
| 4069 | * @param int $post_parent Post parent ID. |
| 4070 | */ |
| 4071 | $override_slug = apply_filters( 'pre_wp_unique_post_slug', null, $slug, $post_ID, $post_status, $post_type, $post_parent ); |
| 4072 | if ( null !== $override_slug ) { |
| 4073 | return $override_slug; |
| 4074 | } |
| 4075 | |
4056 | 4076 | global $wpdb, $wp_rewrite; |
4057 | 4077 | |
4058 | 4078 | $original_slug = $slug; |
diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php
index 3239170a09..953bb5405c 100644
a
|
b
|
class Tests_Post extends WP_UnitTestCase { |
1357 | 1357 | $this->assertEquals( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) ); |
1358 | 1358 | } |
1359 | 1359 | |
| 1360 | /** |
| 1361 | * Test ensuring that the post_slug can be filtered with a custom value short circuiting the built in |
| 1362 | * function that tries to create a unique name based on the post name. |
| 1363 | * |
| 1364 | * @see wp_unique_post_slug() |
| 1365 | * @ticket 21112 |
| 1366 | */ |
| 1367 | function test_pre_wp_unique_post_slug_filter() { |
| 1368 | add_filter( 'pre_wp_unique_post_slug', array( $this, 'filter_pre_wp_unique_post_slug' ), 10, 6 ); |
| 1369 | |
| 1370 | $post_id = $this->factory->post->create( array( 'title' => 'An example', 'post_status' => 'publish', 'post_type' => 'page' ) ); |
| 1371 | $post = get_post( $post_id ); |
| 1372 | $this->assertEquals( 'override-slug-' . $post->post_type, $post->post_name ); |
| 1373 | |
| 1374 | remove_filter( 'pre_wp_unique_post_slug', array( $this, 'filter_pre_wp_unique_post_slug' ), 10, 6 ); |
| 1375 | } |
| 1376 | |
| 1377 | function filter_pre_wp_unique_post_slug( $default, $slug, $post_ID, $post_status, $post_type, $post_parent ) { |
| 1378 | return 'override-slug-' . $post_type; |
| 1379 | } |
1360 | 1380 | } |