WordPress.org

Ready to get started?Download WordPress

Forums

adding SWF as link (bookmark) image (1 post)

  1. guitavares
    Member
    Posted 2 years ago #

    I recently developed this function to use instead of wp_list_bookmarks, in order to allow one to use a SWF file in the link image field.

    Lets say you have a list of sponsor banners in your sidebar, and you use the wordpress "links" feature to save them. You would use the "image address" field to associate the banner image to your sponsor link.

    Then you simply call wp_list_bookmarks on your sidebar, setting "show_images" to TRUE.

    But that would not work if you want to display a SWF banner insted of a regular PNG image file. In that case, you will want to use this function I named "smartLinks". It will detect wether or not the link image addresses to a SWF file and do the trick to display it.

    Before we start, remember to check if you are already calling the swfObject javascript in your header. If not, include this code before the </head> tag:

    <script type="text/javascript" src="swfobject.js"></script>

    (you can download the "swfobject.js" file from here:
    http://code.google.com/p/swfobject/downloads/list)

    Now here we go with the actual code. Paste this into your "functions.php" file:

    function smartLinks($args){ //$args is the same array of parameters you would use with wp_list_bookmarks.
    
    	$bookmarks	= get_bookmarks($args);
    	$str_list	= '';
    	$str_embeds	= '';
    	$hasSWF		= false;
    
    	foreach ($bookmarks as $bm){
    		$size = getimagesize($bm->link_image);
    		if (file_extension($bm->link_image) != 'swf'){
    			$str_list .= '
    				<li>
    					<a href="'.$bm->link_url.'" title="'.$bm->link_description.'" target="'.$bm->link_target.'">
    						<img src="'.$bm->link_image.'"  alt="'.$bm->link_description.'"  title="'.$bm->link_description.'" />
    					</a>
    				</li>';
    		}
    		else {
    			$hasSWF	= true;
    			$str_embeds .= 'swfobject.embedSWF("'.$bm->link_image.'", "swf_'.$bm->link_id.'", "'.$size[0].'", "'.$size[1].'", "9.0.0");';
    			$str_list	.= '
    				<li>
    					<div id="swf_'.$bm->link_id.'" style="height:'.$size[1].'px; width:'.$size[0].'"></div>
    				</li>';
    		}
    	}
    	if ($hasSWF){
    		$str_embeds	= '
    			<script type="text/javascript">
    				'.$str_embeds.'
    			</script>';
    	}
    	echo $str_embeds.' '.$str_list;
    }

    How it works?
    because wp_list_bookmarks would simply paste the swf url inside an <img> tag, it would not work. So smartLinks will write the proper code to place the swf file, using the swfobject dynamic method.
    From here, you can improve the code to what works best for you. You can set size limitations, add more to the "li" tags, style with css, etc.

    Hope you find it useful!

Topic Closed

This topic has been closed to new replies.

About this Topic