• Resolved obradinho

    (@obradinho)


    I really think it would be beneficial to everyone if the HTML inside Excerpt would work.

    Can you please help me out with this? I know its possible, so if you could point me to where it would be awesome!

    Thanks!

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Support Alex

    (@fellan91)

    Hello,

    Thank you for your patience.

    For security reasons, we don’t recommend this approach. However, if needed, you can implement this customization by adding code through the Code Snippets plugin.

    /**
    * Add HTML support to Visual Portfolio excerpts
    * Add this code to your theme's functions.php file
    */

    /**
    * Custom function to trim words while preserving HTML tags
    *
    * @param string $text The text to trim
    * @param int $num_words Number of words to keep
    * @param string $more What to append if text is trimmed
    * @return string
    */
    function vpf_trim_words_with_html( $text, $num_words = 55, $more = '...' ) {
    if ( empty( $text ) ) {
    return '';
    }

    // Remove shortcodes but keep HTML
    $text = strip_shortcodes( $text );

    // Split into words while preserving HTML tags
    $words_array = preg_split( '/(\s+)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
    $word_count = 0;
    $result = '';

    foreach ( $words_array as $word ) {
    // Skip whitespace and HTML tags in word count
    if ( trim( $word ) && ! preg_match( '/<[^>]*>/', $word ) ) {
    $word_count++;
    }

    $result .= $word;

    if ( $word_count >= $num_words ) {
    if ( $word_count < count( array_filter( $words_array, function( $w ) {
    return trim( $w ) && ! preg_match( '/<[^>]*>/', $w );
    } ) ) ) {
    $result .= $more;
    }
    break;
    }
    }

    // Ensure HTML tags are properly closed
    $result = force_balance_tags( $result );

    return $result;
    }

    /**
    * Modify image item args to support HTML in excerpts
    *
    * @param array $args Item arguments
    * @param array $img Image data
    * @return array Modified arguments
    */
    function vpf_modify_image_item_excerpt( $args, $img ) {
    // Check if excerpt should be shown and content exists
    if ( isset( $args['opts']['show_excerpt'] ) && $args['opts']['show_excerpt'] && ! empty( $args['content'] ) ) {

    // Get excerpt word count from options, default to 25
    $excerpt_words_count = isset( $args['opts']['excerpt_words_count'] ) ? (int) $args['opts']['excerpt_words_count'] : 25;

    // Generate excerpt with HTML support
    $args['excerpt'] = vpf_trim_words_with_html( $args['content'], $excerpt_words_count, '...' );
    }

    return $args;
    }
    add_filter( 'vpf_image_item_args', 'vpf_modify_image_item_excerpt', 10, 2 );

    /**
    * Modify post item args to support HTML in excerpts
    *
    * @param array $args Item arguments
    * @param int $post_id Post ID
    * @return array Modified arguments
    */
    function vpf_modify_post_item_excerpt( $args, $post_id ) {
    // Check if excerpt should be shown
    if ( isset( $args['opts']['show_excerpt'] ) && $args['opts']['show_excerpt'] ) {

    // Get the content to use for excerpt
    $content_for_excerpt = '';

    if ( has_excerpt( $post_id ) ) {
    $content_for_excerpt = get_the_excerpt( $post_id );
    } else {
    $content_for_excerpt = $args['content'];
    }

    // Process shortcodes in content
    $content_for_excerpt = do_shortcode( $content_for_excerpt );

    if ( ! empty( $content_for_excerpt ) ) {
    // Get excerpt word count from options, default to 25
    $excerpt_words_count = isset( $args['opts']['excerpt_words_count'] ) ? (int) $args['opts']['excerpt_words_count'] : 25;

    // Generate excerpt with HTML support
    $args['excerpt'] = vpf_trim_words_with_html( $content_for_excerpt, $excerpt_words_count, '...' );
    }
    }

    return $args;
    }
    add_filter( 'vpf_post_item_args', 'vpf_modify_post_item_excerpt', 10, 2 );

    /**
    * Optional: Filter to allow specific HTML tags in excerpts
    * This adds an additional layer of security by allowing only specific HTML tags
    */
    function vpf_allowed_excerpt_html_tags( $args, $post_id_or_img ) {
    if ( isset( $args['excerpt'] ) && ! empty( $args['excerpt'] ) ) {
    // Define allowed HTML tags and attributes
    $allowed_html = array(
    'p' => array(),
    'br' => array(),
    'strong' => array(),
    'b' => array(),
    'em' => array(),
    'i' => array(),
    'u' => array(),
    'span' => array(
    'class' => array(),
    'style' => array(),
    ),
    'div' => array(
    'class' => array(),
    ),
    'a' => array(
    'href' => array(),
    'title' => array(),
    'target' => array(),
    ),
    'ul' => array(),
    'ol' => array(),
    'li' => array(),
    'blockquote' => array(),
    );

    // Sanitize the excerpt while keeping allowed HTML
    $args['excerpt'] = wp_kses( $args['excerpt'], $allowed_html );
    }

    return $args;
    }

    // Apply HTML filtering to both image and post items
    add_filter( 'vpf_image_item_args', 'vpf_allowed_excerpt_html_tags', 20, 2 );
    add_filter( 'vpf_post_item_args', 'vpf_allowed_excerpt_html_tags', 20, 2 );

    This implementation lets you add and display HTML tags specified in the vpf_allowed_excerpt_html_tags function. You can control which tags appear in the output by allowing or restricting specific HTML elements.

    Regards,
    Alex

Viewing 1 replies (of 1 total)

The topic ‘HTML in Excerpt’ is closed to new replies.