Warning: Parameter 2 to qtranxf_postsFilter() expected to be a reference, value given in /home/.sites/609/site1266/web/blackbams-blog/wp-includes/class-wp-hook.php on line 286 Warning: count(): Parameter must be an array or an object that implements Countable in /home/.sites/609/site1266/web/blackbams-blog/wp-includes/post-template.php on line 284 Convert WordPress attachment URLs into file URLs • url conversion • Blackbams Blog
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

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:

Warning: Use of undefined constant Ext_related_links - assumed 'Ext_related_links' (this will throw an Error in a future version of PHP) in /home/.sites/609/site1266/web/blackbams-blog/wp-content/themes/SilentWoodsByBlackbam/single.php on line 75

No comments yet

Kommentare abonnieren (RSS) or URL Trackback

Leave a comment: