Support » Plugins » Hacks » adding SWF as link (bookmark) image

  • 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:

    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 .= '
    					<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.'" />
    		else {
    			$hasSWF	= true;
    			$str_embeds .= 'swfobject.embedSWF("'.$bm->link_image.'", "swf_'.$bm->link_id.'", "'.$size[0].'", "'.$size[1].'", "9.0.0");';
    			$str_list	.= '
    					<div id="swf_'.$bm->link_id.'" style="height:'.$size[1].'px; width:'.$size[0].'"></div>
    	if ($hasSWF){
    		$str_embeds	= '
    			<script type="text/javascript">
    	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!

  • The topic ‘adding SWF as link (bookmark) image’ is closed to new replies.