How do you strip or filter attributes from a Post’s tags?
-
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” alt=”image” width=”1024″ height=”640″ />Becomes:
<img class=”imgLarge” title=”8″ src=”http://site.com/wp-content/uploads/image.jpg” alt=”image” />Would reaaaaalllly appreciate someone helping me!
Thanks,
MarcHere 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; } ?>
- The topic ‘How do you strip or filter attributes from a Post’s tags?’ is closed to new replies.