WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] how to restrict object embeds from excerpt (5 posts)

  1. kristinachilds
    Member
    Posted 4 years ago #

    i'm using a custom function to control excerpt lenghts in multiple places site-wide. on my archive pages there's a word limit of 55 ex: <?php echo content(55); ?>

    this is great except for that it also includes video embeds and/or images within the body text if the embedded content is within the first 55 words and it throws off the whole structure of the archive pages. is there an easy way to restrict the inclusion of any <object> tag or image in the body <p> tag? here is the function

    function content($limit) {
      $content = explode(' ', get_the_content(), $limit);
      if (count($content)>=$limit) {
        array_pop($content);
        $content = implode(" ",$content).'...';
      } else {
        $content = implode(" ",$content);
      }
      $content = preg_replace('/\[.+\]/','', $content);
      $content = apply_filters('the_content', $content);
      $content = str_replace(']]>', ']]>', $content);
      return $content;
    }

    great function, btw. i didn't write it but it works like a charm and allows for a TON of flexibility.

  2. KatyaKarski
    Member
    Posted 4 years ago #

    you might want to look at some examples on the PHP strip_tags() function reference, which will allow you to remove any HTML from your $content - there are examples there which strip embed code reliably.

    http://php.net/manual/en/function.strip-tags.php

    you might want to apply that before you trim the length to make sure you get an accurate word count and to make sure you get it all.

    ALSO it's probably improtant to note here that you're not actually using the_excerpt() function - your custom func employs the_content() to do this job, which doesn't strip any of the stuff the_excerpt usually would strip for you automatically.

  3. kristinachilds
    Member
    Posted 3 years ago #

    nevermind, i just did it with css *smacks forehead*

    .blog-list-info object, .blog-list-info img {
            display: none;
            }

    since this style ONLY applies to the various excerpt sizes i have around the site, the descendant selectors work like a charm (and is super light-weight).

    i love css

  4. kristinachilds
    Member
    Posted 3 years ago #


  5. KatyaKarski
    Member
    Posted 3 years ago #

    sending the broken tags to the browser, having the browser interpret them, and then having the browser hide code that it's interpreted isn't light weight, and doesn't really fix anything.

    The browser will still pull the image or attempt to reach the 3rd party site to do the embed, and then not display it. That's a hell of a long way from light-weight.

    swept under the rug certainly, but not exactly fixed.

Topic Closed

This topic has been closed to new replies.

About this Topic