• rgbk

    (@rgbk)


    Hi
    I want to strip attributes that WordPress automatically gives my images.

    I’m assuming this can be done after the post has been published, in my templates single.php and functions.php

    I have found a great script for this, but my PHP skills are lacking, so I can’t figure out how to apply this function to the content. (the script is below)

    What I want to do:
    Use ‘Thumbnail’, ‘Medium’, ‘Large’ settings in the image uploader.

    But:
    Filter out the automatic width and height parameters, and simplify the unique styles WP gives the posts and just give each relative image it’s own class.

    Example:
    class=”imgThumb”
    class=”imgMedium”
    class=”imgLarge”

    So:
    <img class=”alignnone size-large wp-image-379″ title=”8″ src=”http://site.com/wp-content/uploads/image.jpg&#8221; alt=”image” width=”1024″ height=”640″ />

    Becomes:
    <img class=”imgLarge” title=”8″ src=”http://site.com/wp-content/uploads/image.jpg&#8221; alt=”image” />

    Would reaaaaalllly appreciate someone helping me!

    Thanks,
    Marc

    Here is the code I think could do most of the work, but i dont know how to make this talk to WP yet. I got it from here

    <?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;
    }
    
    ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter rgbk

    (@rgbk)

    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&#8221; alt=”image” width=”1024″ height=”640″ />

    Should become:
    <img class=”imgLarge” title=”8″ src=”http://mysite.com/wp-content/uploads/image.jpg&#8221; 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!

    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')); ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How do you strip or filter attributes from a Post’s tags?’ is closed to new replies.