diff --git src/wp-includes/post.php src/wp-includes/post.php
index 98ad57fbd0..ac748820be 100644
|
|
function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p |
3984 | 3984 | return $slug; |
3985 | 3985 | } |
3986 | 3986 | |
| 3987 | /** |
| 3988 | * Filter to allow setting the unique post slug before it is generated. |
| 3989 | * |
| 3990 | * Returning a non-empty value will preempt unique slug detection and |
| 3991 | * generation and will result in that value being used as the post's slug. |
| 3992 | * |
| 3993 | * @since 5.0 |
| 3994 | * |
| 3995 | * @param string $default_slug The default post slug. Default empty. |
| 3996 | * @param string $slug The desired slug (post_name). |
| 3997 | * @param int $post_ID Post ID. |
| 3998 | * @param string $post_status The post status. |
| 3999 | * @param string $post_type Post type. |
| 4000 | * @param int $post_parent Post parent ID |
| 4001 | */ |
| 4002 | $override_slug = apply_filters( 'pre_wp_unique_post_slug', '', $slug, $post_ID, $post_status, $post_type, $post_parent ); |
| 4003 | if ( $override_slug ) { |
| 4004 | return $override_slug; |
| 4005 | } |
| 4006 | |
3987 | 4007 | global $wpdb, $wp_rewrite; |
3988 | 4008 | |
3989 | 4009 | $original_slug = $slug; |
diff --git tests/phpunit/tests/post.php tests/phpunit/tests/post.php
index c0b20bb2a7..9786f5f2fa 100644
|
|
class Tests_Post extends WP_UnitTestCase { |
1354 | 1354 | $this->assertEquals( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) ); |
1355 | 1355 | } |
1356 | 1356 | |
| 1357 | /** |
| 1358 | * Test ensuring that the post_slug can be filtered with a custom value short circuiting the built in |
| 1359 | * function that tries to create a unique name based on the post name. |
| 1360 | * |
| 1361 | * @see wp_unique_post_slug() |
| 1362 | * @ticket 21112 |
| 1363 | */ |
| 1364 | function test_pre_wp_unique_post_slug_filter() { |
| 1365 | add_filter( 'pre_wp_unique_post_slug', array( $this, 'filter_pre_wp_unique_post_slug' ), 10, 6 ); |
| 1366 | |
| 1367 | $post_id = $this->factory->post->create( array( 'title' => 'An example', 'post_status' => 'publish', 'post_type' => 'page' ) ); |
| 1368 | $post = get_post( $post_id ); |
| 1369 | $this->assertEquals( 'override-slug-' . $post->post_type, $post->post_name ); |
| 1370 | |
| 1371 | remove_filter( 'pre_wp_unique_post_slug', array( $this, 'filter_pre_wp_unique_post_slug' ), 10, 6 ); |
| 1372 | } |
| 1373 | |
| 1374 | function filter_pre_wp_unique_post_slug( $default, $slug, $post_ID, $post_status, $post_type, $post_parent ) { |
| 1375 | return 'override-slug-' . $post_type; |
| 1376 | } |
1357 | 1377 | } |