简体   繁体   中英

Wordpress: remove un-wanted html tag from WP REST API JSON file

I am new to Wordpress, and using WP REST API to get JSON data to be used in another project. However, inside the JSON data, there are some adjustment I want to make so that it's easy to use these data. For example, in excerpt.rendered section, I just want pure words, without additional <p> or \\n or </p> and other html tags.

在此处输入图片说明

I know this may have something with the php document, but I am new to WP, so which file I need to make some changes so that I get the excerpt as I want?

There is a relatively straightforward way to to this.

To remove the wrapping <p> tags from excerpts, put the following line in your functions.php file:

remove_filter('the_excerpt', 'wpautop');

To remove the wrapping tags from the content:

remove_filter ('the_content', 'wpautop');

More details: https://developer.wordpress.org/reference/functions/remove_filter/

If there are other HTML tags you want to remove that are automatically being added, see if there is a related filter you can disable. Otherwise you'll have to update your PHP routes to manually remove the offending HTML.

Use a <WebView/> for display content. WebView displays content with hiding HTML tag.

Can't you just use strip_tags before returning your response to the other project?

ie

$excerpt = $json[excerpt][rendered];
return strip_tags($excerpt);

This should remove all HTML tags and entities and return just the raw content.

Alternatively, if you need the whole JSON response, I imagine stripping the tags before the excerpt is serialised should work.

Try replacing:

/**
 * Check the post excerpt and prepare it for single post output.
 *
 * @param string       $excerpt
 * @return string|null $excerpt
 */
protected function prepare_excerpt_response( $excerpt ) {
    if ( post_password_required() ) {
        return __( 'There is no excerpt because this is a protected post.' );
    }

    /** This filter is documented in wp-includes/post-template.php */
    $excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $excerpt ) );

    if ( empty( $excerpt ) ) {
        return '';
    }

    return $excerpt;
}

With:

/**
 * Check the post excerpt and prepare it for single post output.
 *
 * @param string       $excerpt
 * @return string|null $excerpt
 */
protected function prepare_excerpt_response( $excerpt ) {
    if ( post_password_required() ) {
        return __( 'There is no excerpt because this is a protected post.' );
    }

    /** This filter is documented in wp-includes/post-template.php */
    $excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $excerpt ) );

    if ( empty( $excerpt ) ) {
        return '';
    }

    return strip_tags($excerpt);
}

in class-wp-rest-posts-controller.php Line 651

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM