Make WordPress Core


Ignore:
Timestamp:
01/08/2019 03:46:58 AM (6 years ago)
Author:
pento
Message:

Permalinks: Add a pre_wp_unique_post_slug filter.

Returning a non-null value on this fillter will cause wp_unique_post_slug() to return early with that value, skipping potentially expensive database queries on some sites.

Props coffee2code, javorszky, iCaleb.
Fixes #21112.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post.php

    r43571 r44454  
    13581358    }
    13591359
     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(
     1371            array(
     1372                'title'       => 'An example',
     1373                'post_status' => 'publish',
     1374                'post_type'   => 'page',
     1375            )
     1376        );
     1377        $post    = get_post( $post_id );
     1378        $this->assertEquals( 'override-slug-' . $post->post_type, $post->post_name );
     1379
     1380        remove_filter( 'pre_wp_unique_post_slug', array( $this, 'filter_pre_wp_unique_post_slug' ), 10, 6 );
     1381    }
     1382
     1383    function filter_pre_wp_unique_post_slug( $default, $slug, $post_ID, $post_status, $post_type, $post_parent ) {
     1384        return 'override-slug-' . $post_type;
     1385    }
    13601386}
Note: See TracChangeset for help on using the changeset viewer.