WordPress.org

Ready to get started?Download WordPress

Forums

How do you strip or filter attributes from a Post's tags? (2 posts)

  1. rgbk
    Member
    Posted 4 years ago #

    Hi

    I want to filter and edit attributes that WordPress automatically places in my Posts <img> tags.

    I have found a very thorough php function already. However I don't know how to make this talk with WordPress. I'd really appreciate it if someone can help me with this.

    Aim:
    Utilise The image uploaders 'Thumbnail', 'Medium' and 'Large' settings, but designate it's attributes to my styles.css template file.

    Example:
    <img class="alignnone size-large wp-image-379" title="8" src="http://mysite.com/wp-content/uploads/image.jpg" alt="image" width="1024" height="640" />

    Should become:
    <img class="imgLarge" title="8" src="http://mysite.com/wp-content/uploads/image.jpg" alt="image" />

    Here is the function I found which seems to address the issue (I just dont know how to make this talk with WordPress). The code comes from this page.

    <?php
    function strip_attributes($msg, $tag, $attr, $suffix = "")
    {
         $lengthfirst = 0;
         while (strstr(substr($msg, $lengthfirst), "<$tag ") != "") {
              $tag_start = $lengthfirst + strpos(substr($msg, $lengthfirst), "<$tag ");
    
              $partafterwith = substr($msg, $tag_start);
    
              $img = substr($partafterwith, 0, strpos($partafterwith, ">") + 1);
              $img = str_replace(" =", "=", $img);
    
              $out = "<$tag";
              for($i = 0; $i < count($attr); $i++) {
                   if (empty($attr[$i])) {
                        continue;
                   }
                   $long_val =
                   (strpos($img, " ", strpos($img, $attr[$i] . "=")) === false) ?
                   strpos($img, ">", strpos($img, $attr[$i] . "=")) - (strpos($img, $attr[$i] . "=") + strlen($attr[$i]) + 1) :
                   strpos($img, " ", strpos($img, $attr[$i] . "=")) - (strpos($img, $attr[$i] . "=") + strlen($attr[$i]) + 1);
                   $val = substr($img, strpos($img, $attr[$i] . "=") + strlen($attr[$i]) + 1, $long_val);
                   if (!empty($val)) {
                        $out .= " " . $attr[$i] . "=" . $val;
                   }
              }
              if (!empty($suffix)) {
                   $out .= " " . $suffix;
              }
    
              $out .= ">";
              $partafter = substr($partafterwith, strpos($partafterwith, ">") + 1);
              $msg = substr($msg, 0, $tag_start) . $out . $partafter;
              $lengthfirst = $tag_start + 3;
         }
         return $msg;
    }
    
    ?>

    THANKS!

  2. totomobile
    Member
    Posted 4 years ago #

    No need for plugins. I use this to clean up the width and height filter from my post 'featured image'.

    In your functions file:

    function clean_wp_width_height($string){
    	return preg_replace('/\<(.*?)(width="(.*?)")(.*?)(height="(.*?)")(.*?)\>/i', '<$1$4$7>',$string);
    }

    In your code:
    <?php echo clean_wp_width_height(get_the_post_thumbnail(get_the_ID(),'large')); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.