messing around with preg_replace….

I am probably trying to reinvent the wheel here but…..

I wanted to get the top 20 or so tags from a wordpress blog and to use the tags for a loop of 3 posts from each tag.

The only function i could find to get the most used tags was wp_tag_cloud

by taking it into an array i could get the top 20 tags.

$tags = wp_tag_cloud( array( 'number' => '20', 'order' => 'DESC', 'echo' => 'false', 'format' => 'array' ) );

I looped through this array using:

 foreach($tags as $tag)

unfortunately the wp_tag_cloud did not return a list of tag id’s for me to  loop through.

each $tag was in the format of a url,  i.e. <a id=”” href=”http://domain.com/tag/post_slug” style=””>tag name</a>

So i needed to extract data from this to find the tag.  i chose to extract the tag slug which would be in the url.

$tagslug= preg_replace(‘%<a .*? ?href=[“\’]([^”\’]*).*?>.*?</a>%i’, ‘$1’, $tag);

returned the url for me.  Unfortunately my regular expression skills are pretty poor and couldn’t work out how to extract the slug straight away using markers. (my university professor would not be proud.)

so i took the url and ran another preg_replace on it to get the tag slug

$tagslug = preg_replace(“/.*\.(com|gov|org|net|mil|edu)\/tag\/([a-z_\-]+).*/i”,’$2′,$tagslug);

This finally returned the tag’s slug for me,  I had a wee function called get_tag_id_by_slug i made so i called that.

$thetagid= get_tag_id_by_slug($tagslug);

which returned the tag id number for me, so finally i could use
$term = get_tag($thetagid);

to get the actual term to use in my wordpress loop

$new_query = new WP_Query(‘r_sortby=highest_rated&r_orderby=desc&posts_per_page=4&tag=’. $term->slug);
while ( $new_query->have_posts() ) : $new_query->the_post();

 

My function to get the id from the slug is:

function get_tag_id_by_slug($tag_slug) {
global $wpdb;
$tag_ID = $wpdb->get_var(“SELECT * FROM “.$wpdb->terms.” WHERE `slug` = ‘”.$tag_slug.”‘”);
return $tag_ID;}

 

After doing all this i decided that its all very well to automate things but some of my most used tags were very similar and i didn’t want a loop containing the same 4 posts over and over with a new title.  So in the end i just went with.

$tags=get_tags(‘include=7,8,11,30,45’);

and looped that!

DOH!

Flash Games

I have made a plugin for wordpress to display up to 200 flash games within your wordpress blog.

It is a relatively simple plugin that pulls data from my arcade website Lingolux flash games.

At the backend Lingolux uses a simple wordpress WP_Query loop to pull data from the site as specified by the plugin.
This allows the user to select the number of games and whether to display new games, top games, or both.

Although most blogs will cache the page themselves i did not want my plugin to overwhelm my website so i implemented a simple caching system in php.
I found the information here


";
exit;
}
ob_start(); // start the output buffer
?>
I placed the wp query here

This provided some simple caching of the backend.

The plugin also uses a simple lightbox to display the flash.
You can see the plugin in operation here and you can download it from the wordpress site here
Flash Games Page