$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>html - Excerpt keep stripping <a> tags|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

html - Excerpt keep stripping <a> tags

matteradmin6PV0评论

I have the following function to display my excerpt

  <?php function pietergoosen_custom_wp_trim_excerpt($text) {
global $post;
$raw_excerpt = $text;
if ( '' == $text ) {
    $text = get_the_content('');

    $text = strip_shortcodes( $text );

    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]&gt;', $text);

    //Add the allowed HTML tags separated by a comma.
    $allowed_tags = '<p>,<a>,<em>,<strong>,<img src />,<audio><video>';  
    $text = strip_tags($text, $allowed_tags);

    //Change the excerpt word count.
    $excerpt_word_count = 105; 
    $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); 

    //Change the excerpt ending.
    $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&hellip;' . __( 'Read more about this article <span class="meta-nav">&rarr;</span>', 'pietergoosen' ) . '</a>'; 
    $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);

    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    if ( count($words) > $excerpt_length ) {
        array_pop($words);
        $text = implode(' ', $words);
        $text = $text . $excerpt_more;
    } else {
        $text = implode(' ', $words);
    }
}
    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'pietergoosen_custom_wp_trim_excerpt'); ?>

My problem is, my <a> tags still get stripped. I followed a couple examples, and all suggest adding the <a> tag to the $allowed_tags, but even that doesn't solve the problem. Any suggestions?

I have the following function to display my excerpt

  <?php function pietergoosen_custom_wp_trim_excerpt($text) {
global $post;
$raw_excerpt = $text;
if ( '' == $text ) {
    $text = get_the_content('');

    $text = strip_shortcodes( $text );

    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]&gt;', $text);

    //Add the allowed HTML tags separated by a comma.
    $allowed_tags = '<p>,<a>,<em>,<strong>,<img src />,<audio><video>';  
    $text = strip_tags($text, $allowed_tags);

    //Change the excerpt word count.
    $excerpt_word_count = 105; 
    $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); 

    //Change the excerpt ending.
    $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&hellip;' . __( 'Read more about this article <span class="meta-nav">&rarr;</span>', 'pietergoosen' ) . '</a>'; 
    $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);

    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    if ( count($words) > $excerpt_length ) {
        array_pop($words);
        $text = implode(' ', $words);
        $text = $text . $excerpt_more;
    } else {
        $text = implode(' ', $words);
    }
}
    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'pietergoosen_custom_wp_trim_excerpt'); ?>

My problem is, my <a> tags still get stripped. I followed a couple examples, and all suggest adding the <a> tag to the $allowed_tags, but even that doesn't solve the problem. Any suggestions?

Share Improve this question asked Feb 15, 2014 at 14:55 Pieter GoosenPieter Goosen 55.5k23 gold badges117 silver badges211 bronze badges 1
  • I can't duplicate the problem. – s_ha_dum Commented Feb 15, 2014 at 15:14
Add a comment  | 

1 Answer 1

Reset to default 1
add_filter( 'get_the_content_limit_allowedtags', 'get_the_content_limit_custom_allowedtags' );

function get_the_content_limit_custom_allowedtags() {
// Add custom tags to this string
return '<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>'; 
}

You can also add this code in your functions file and customize which tags you want to allow in excerpts.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far