Make WordPress Core

Changeset 44454


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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r44347 r44454  
    41184118    }
    41194119
     4120    /**
     4121     * Filters the post slug before it is generated to be unique.
     4122     *
     4123     * Returning a non-null value will short-circuit the
     4124     * unique slug generation, returning the passed value instead.
     4125     *
     4126     * @since 5.1.0
     4127     *
     4128     * @param string $override_slug Short-circuit return value.
     4129     * @param string $slug          The desired slug (post_name).
     4130     * @param int    $post_ID       Post ID.
     4131     * @param string $post_status   The post status.
     4132     * @param string $post_type     Post type.
     4133     * @param int    $post_parent   Post parent ID.
     4134     */
     4135    $override_slug = apply_filters( 'pre_wp_unique_post_slug', null, $slug, $post_ID, $post_status, $post_type, $post_parent );
     4136    if ( null !== $override_slug ) {
     4137        return $override_slug;
     4138    }
     4139
    41204140    global $wpdb, $wp_rewrite;
    41214141
  • 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.