10. January 2012
von Blackbam
You do not want to use Attachment-Pages in your WordPress page? Or maybe you installed a gallery Plugin and want to transform all your Attachment URLs into File-URLs now? The following script will do this for you. Just make sure, that all relevant content blocks of your database run through this script.
function cis_file_links($content) {
 
 $content = stripslashes($content);
 
 $matches = array();
 $attachment_urls = preg_match_all("/href=\"([-A-Z0-9+&@#\/%?=~_|!:,.;]*)\?attachment_id=([0-9]*)\"/siU",$content,$matches);
 
 $urls = array();
 foreach($matches[2] as $attachment) {
  array_push($urls,wp_get_attachment_url($attachment));
 }
 
 // something went wrong with geting the attachments
 if(count($matches[2])!=count($urls)) {
  return $content;
 }
 
 for($i=0;$i<count($urls);$i++) {
  $repl = "href=\"".$urls[$i]."\"";
  $content = preg_replace("/href=\"([-A-Z0-9+&@#\/%?=~_|!:,.;]*)\?attachment_id=([0-9]*)\"/siU",$repl,$content,1);
 }
 
 return $content;
}
If you want to apply this script to every displayed content, for example, without changing any contents of your database, add this filter to your functions.php:
add_filter('the_content','cis_file_links');
Improvement ideas are welcome.
Share

Dieser Eintrag wurde am 10. January 2012 um 22:22 in der Kategorie WP Scripts veröffentlicht. You can book the comments for this article RSS 2.0. Feedback, discussion, commendation and critics are welcome: Write a comment or trackback.


Tags:

No comments yet

Kommentare abonnieren (RSS) or URL Trackback

Leave a comment: