wp_trim_words example

$comment_excerpt_length = (int) _x( '20', 'comment_excerpt_length' );

    /** * Filters the maximum number of words used in the comment excerpt. * * @since 4.4.0 * * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt. */
    $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );

    $comment_excerpt = wp_trim_words( $comment_text$comment_excerpt_length, '…' );

    /** * Filters the retrieved comment excerpt. * * @since 1.5.0 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added. * * @param string $comment_excerpt The comment excerpt text. * @param string $comment_id The comment ID as a numeric string. * @param WP_Comment $comment The comment object. */
    
echo "<li>\n";
        printf(
            '<div class="draft-title"><a href="%s" aria-label="%s">%s</a><time datetime="%s">%s</time></div>',
            esc_url( $url ),
            /* translators: %s: Post title. */
            esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' )$title ) ),
            esc_html( $title ),
            get_the_time( 'c', $draft ),
            get_the_time( __( 'F j, Y' )$draft )
        );

        $the_content = wp_trim_words( $draft->post_content, $draft_length );

        if ( $the_content ) {
            echo '<p>' . $the_content . '</p>';
        }
        echo "</li>\n";
    }

    echo "</ul>\n";
    echo '</div>';
}

 elseif ( 'post_type_archive' === $post->type && ! empty( $post->object ) ) {
            $post->url = get_post_type_archive_link( $post->object );
        }
        if ( is_wp_error( $post->url ) ) {
            $post->url = '';
        }

        /** This filter is documented in wp-includes/nav-menu.php */
        $post->attr_title = apply_filters( 'nav_menu_attr_title', $post->attr_title );

        /** This filter is documented in wp-includes/nav-menu.php */
        $post->description = apply_filters( 'nav_menu_description', wp_trim_words( $post->description, 200 ) );

        /** This filter is documented in wp-includes/nav-menu.php */
        $post = apply_filters( 'wp_setup_nav_menu_item', $post );

        return $post;
    }

    /** * Sanitize an input. * * Note that parent::sanitize() erroneously does wp_unslash() on $value, but * we remove that in this override. * * @since 4.3.0 * @since 5.9.0 Renamed `$menu_item_value` to `$value` for PHP 8 named parameter support. * * @param array $value The menu item value to sanitize. * @return array|false|null|WP_Error Null or WP_Error if an input isn't valid. False if it is marked for deletion. * Otherwise the sanitized value. */

        $excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length );

        /** * Filters the string in the "more" link displayed after a trimmed excerpt. * * @since 2.9.0 * * @param string $more_string The string shown within the more link. */
        $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
        $text         = wp_trim_words( $text$excerpt_length$excerpt_more );

    }

    /** * Filters the trimmed excerpt string. * * @since 2.8.0 * * @param string $text The trimmed text. * @param string $raw_excerpt The text prior to trimming. */
    
        $plugin = $item;

        $fields = $this->get_fields_for_response( $request );

        // There might be multiple blocks in a plugin. Only the first block is mapped.         $block_data = reset( $plugin['blocks'] );

        // A data array containing the properties we'll return.         $block = array(
            'name'                => $block_data['name'],
            'title'               => ( $block_data['title'] ? $block_data['title'] : $plugin['name'] ),
            'description'         => wp_trim_words( $plugin['short_description'], 30, '...' ),
            'id'                  => $plugin['slug'],
            'rating'              => $plugin['rating'] / 20,
            'rating_count'        => (int) $plugin['num_ratings'],
            'active_installs'     => (int) $plugin['active_installs'],
            'author_block_rating' => $plugin['author_block_rating'] / 20,
            'author_block_count'  => (int) $plugin['author_block_count'],
            'author'              => wp_strip_all_tags( $plugin['author'] ),
            'icon'                => ( isset( $plugin['icons']['1x'] ) ? $plugin['icons']['1x'] : 'block-default' ),
            'last_updated'        => gmdate( 'Y-m-d\TH:i:s', strtotime( $plugin['last_updated'] ) ),
            'humanized_updated'   => sprintf(
                /* translators: %s: Human-readable time difference. */
                
$author = '<span class="wp-block-rss__item-author">' . sprintf(
                    /* translators: %s: the author. */
                    __( 'by %s' ),
                    esc_html( strip_tags( $author ) )
                ) . '</span>';
            }
        }

        $excerpt = '';
        if ( $attributes['displayExcerpt'] ) {
            $excerpt = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
            $excerpt = esc_attr( wp_trim_words( $excerpt$attributes['excerptLength'], ' [&hellip;]' ) );

            // Change existing [...] to [&hellip;].             if ( '[...]' === substr( $excerpt, -5 ) ) {
                $excerpt = substr( $excerpt, 0, -5 ) . '[&hellip;]';
            }

            $excerpt = '<div class="wp-block-rss__item-excerpt">' . esc_html( $excerpt ) . '</div>';
        }

        $list_items .= "<li class='wp-block-rss__item'>{$title}{$date}{$author}{$excerpt}</li>";
    }

    
 elseif ( 'post_type_archive' === $menu_item->type ) {
                $object = get_post_type_object( $menu_item->object );
                if ( $object ) {
                    $menu_item->title      = ( '' === $menu_item->post_title ) ? $object->labels->archives : $menu_item->post_title;
                    $post_type_description = $object->description;
                } else {
                    $post_type_description = '';
                    $menu_item->_invalid   = true;
                }

                $menu_item->type_label = __( 'Post Type Archive' );
                $post_content          = wp_trim_words( $menu_item->post_content, 200 );
                $post_type_description = ( '' === $post_content ) ? $post_type_description : $post_content;
                $menu_item->url        = get_post_type_archive_link( $menu_item->object );

            } elseif ( 'taxonomy' === $menu_item->type ) {
                $object = get_taxonomy( $menu_item->object );
                if ( $object ) {
                    $menu_item->type_label = $object->labels->singular_name;
                } else {
                    $menu_item->type_label = $menu_item->object;
                    $menu_item->_invalid   = true;
                }

                
while ( ! empty( $link ) && stristr( $link, 'http' ) !== $link ) {
            $link = substr( $link, 1 );
        }
        $link = esc_url( strip_tags( $link ) );

        $title = esc_html( trim( strip_tags( $item->get_title() ) ) );
        if ( empty( $title ) ) {
            $title = __( 'Untitled' );
        }

        $desc = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
        $desc = esc_attr( wp_trim_words( $desc, 55, ' [&hellip;]' ) );

        $summary = '';
        if ( $show_summary ) {
            $summary = $desc;

            // Change existing [...] to [&hellip;].             if ( str_ends_with( $summary, '[...]' ) ) {
                $summary = substr( $summary, 0, -5 ) . '[&hellip;]';
            }

            $summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>';
        }


    /* * The purpose of the excerpt length setting is to limit the length of both * automatically generated and user-created excerpts. * Because the excerpt_length filter only applies to auto generated excerpts, * wp_trim_words is used instead. */
    $excerpt_length = $attributes['excerptLength'];
    $excerpt        = get_the_excerpt( $block->context['postId'] );
    if ( isset( $excerpt_length ) ) {
        $excerpt = wp_trim_words( $excerpt$excerpt_length );
    }

    $more_text           = ! empty( $attributes['moreText'] ) ? '<a class="wp-block-post-excerpt__more-link" href="' . esc_url( get_the_permalink( $block->context['postId'] ) ) . '">' . wp_kses_post( $attributes['moreText'] ) . '</a>' : '';
    $filter_excerpt_more = static function( $more ) use ( $more_text ) {
        return empty( $more_text ) ? $more : '';
    };
    /** * Some themes might use `excerpt_more` filter to handle the * `more` link displayed after a trimmed excerpt. Since the * block has a `more text` attribute we have to check and * override if needed the return value from this filter. * So if the block's attribute is not empty override the * `excerpt_more` filter and return nothing. This will * result in showing only one `read more` link at a time. */
Home | Imprint | This part of the site doesn't use cookies.