Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove setup_postdata() call in ucfwp_get_excerpt() #120

Open
cjg89 opened this issue Nov 30, 2021 · 0 comments
Open

Remove setup_postdata() call in ucfwp_get_excerpt() #120

cjg89 opened this issue Nov 30, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@cjg89
Copy link
Member

cjg89 commented Nov 30, 2021

Description

function ucfwp_get_excerpt( $post, $length=0 ) {
if ( ! ( $post instanceof WP_Post ) ) return '';
if ( $length === 0 ) {
$length = apply_filters( 'excerpt_length', $length );
}
// Allow custom excerpt modification short-circuit
$excerpt = apply_filters( 'ucfwp_get_excerpt_before', '', $post, $length );
if ( $excerpt ) {
return $excerpt;
}
setup_postdata( $post );
$custom_filter = function( $l ) use ( $length ) {
return $length;
};
add_filter( 'excerpt_length', $custom_filter, 999 );
$excerpt = wp_strip_all_tags( get_the_excerpt( $post ) );
remove_filter( 'excerpt_length', $custom_filter, 999 );
return apply_filters( 'ucfwp_get_excerpt', $excerpt, $post, $length );
}

This setup_postdata() call in this function should not be necessary and should be removed (assuming that doing so doesn't result in breakage).

Why it's Important

This line was originally added after reading this in the documentation for get_the_excerpt(): https://developer.wordpress.org/reference/functions/get_the_excerpt/#comment-content-2457 However, this comment is out of date--as of WP 5.2, get_the_excerpt() and wp_trim_excerpt() pass along the $post variable to get_the_content() when no excerpt is defined, eliminating the need for handling global post data manually. Additionally, it doesn't look like setup_postdata() sets up a global $post object to begin with, so calling setup_postdata() is not accomplishing its intended goal anyway.

Alternatives

n/a

Possible Implementation

Remove it and test.

Additional context

n/a

@cjg89 cjg89 added the enhancement New feature or request label Nov 30, 2021
@cjg89 cjg89 added this to the backlog milestone Nov 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants