Make WordPress Core


Ignore:
Timestamp:
04/30/2024 09:31:41 AM (14 months ago)
Author:
swissspidy
Message:

REST API: allow overriding excerpt length.

This can be used by the excerpt block in the editor to change the excerpt length without filtering excerpt_length in a conflicting way. This enhancement still needs a corresponding change on the Gutenberg side.

Props swissspidy, antonvlasenko, mukesh27, azaozz, andraganescu, timothyblynjacobs.
Fixes #59043.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r57648 r58065  
    9898            'context' => $this->get_context_param( array( 'default' => 'view' ) ),
    9999        );
     100        if ( isset( $schema['properties']['excerpt'] ) ) {
     101            $get_item_args['excerpt_length'] = array(
     102                'description' => __( 'Override the default excerpt length.' ),
     103                'type'        => 'integer',
     104            );
     105        }
    100106        if ( isset( $schema['properties']['password'] ) ) {
    101107            $get_item_args['password'] = array(
     
    18731879
    18741880        if ( rest_is_field_included( 'excerpt', $fields ) ) {
     1881            if ( isset( $request['excerpt_length'] ) ) {
     1882                $excerpt_length          = $request['excerpt_length'];
     1883                $override_excerpt_length = static function () use ( $excerpt_length ) {
     1884                    return $excerpt_length;
     1885                };
     1886
     1887                add_filter(
     1888                    'excerpt_length',
     1889                    $override_excerpt_length,
     1890                    20
     1891                );
     1892            }
     1893
    18751894            /** This filter is documented in wp-includes/post-template.php */
    18761895            $excerpt = apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );
     
    18841903                'protected' => (bool) $post->post_password,
    18851904            );
     1905
     1906            if ( isset( $override_excerpt_length ) ) {
     1907                remove_filter(
     1908                    'excerpt_length',
     1909                    $override_excerpt_length,
     1910                    20
     1911                );
     1912            }
    18861913        }
    18871914
Note: See TracChangeset for help on using the changeset viewer.