diff --git src/wp-includes/post.php src/wp-includes/post.php
index cb5025f607..7cb9377a5b 100644
|
|
function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p |
3999 | 3999 | return $slug; |
4000 | 4000 | } |
4001 | 4001 | |
| 4002 | /** |
| 4003 | * Filter to allow setting the unique post slug before it is generated. |
| 4004 | * |
| 4005 | * Returning a non-empty value will preempt unique slug detection and |
| 4006 | * generation and will result in that value being used as the post's slug. |
| 4007 | * |
| 4008 | * @since 4.1 |
| 4009 | * |
| 4010 | * @param string $default_slug The default post slug. Default empty. |
| 4011 | * @param string $slug The desired slug (post_name). |
| 4012 | * @param int $post_ID Post ID. |
| 4013 | * @param string $post_status The post status. |
| 4014 | * @param string $post_type Post type. |
| 4015 | * @param int $post_parent Post parent ID |
| 4016 | */ |
| 4017 | $override_slug = apply_filters( 'pre_wp_unique_post_slug', '', $slug, $post_ID, $post_status, $post_type, $post_parent ); |
| 4018 | if ( $override_slug ) { |
| 4019 | return $override_slug; |
| 4020 | } |
| 4021 | |
4002 | 4022 | global $wpdb, $wp_rewrite; |
4003 | 4023 | |
4004 | 4024 | $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 | } |