Archive for the ‘Wordpress’ category

SB Twitter Plugin

November 21st, 2009

Let’s get this straight, I hate Twitter with a vengence on account of it being a pointless waste of time. However, I can see that some people enjoy using it and also want to share their exciting lives with other people.

I have just had the need to develop a very basic plugin to put a Twitter widget (not a Wordpress widget) on a website. I didn’t see the point in forcing the user to put masses of code onto their page each time they wanted to include the widget so created a shortcode to do the same job.

I have rather imaginatively called it SB Twitter and it allows the user of a shortcode to put the Twitter flash widget onto a post or a page.

Usage

[sb_twitter name="you_user_name"]

You can optionally include width=”" and/or height=”" to manipulate the size of the box.

Download

The download file contains a readme which essentially says to upload the file to your plugins directory, activate it and use the shortcode provided.

SB Twitter (2.29 KB)

Screenshots

Example output of SB Twitter plugin

Live Countdown Timer using Javascript

October 10th, 2009

Been done? Yes of course it has! However you don’t learn anything unless you get stuck in and have a go yourself and that is exactly what I did during an empty half hour period I had the other day (slow lecture). Whilst at university I am doing a lot of timed tasks (ten minutes etc.. as opposed to days) and the timers used are always a mobile phone timer or horrible Powerpoint countdown presentation. I wanted to write something which is simple to build on to create a JS version to ultimately integrate into online resources and lessons (Eg: this task will take you ten minutes. Click here to turn on the timer…).

The  code at the bottom of this post is the raw HTML file I wrote. I obviously made it into a Wordpress plugin as well. see below example.

Example

Yes, it’s basic but it works well for demonstrative purposes.

Countdown from:

Usage

write sb_countdown in square brackets in a post or page and you will get the above example. If you want to edit the text or style it then just open the plugin file and right near the top is a few defines to get you going. I didn’t spend a lot of time on it so didn’t create an admin page. I may well do in the future though.

The Download

SB JS Countdown Timer (1.42 KB)

Raw HTML Version

<html>
 <head>
 <title>Countdown Timer</title>

 <style>
 body {
 font-family: tahoma;
 }

 #countdown_div {
 font-weight: bold;
 font-size: 56px;
 }

 #body_wrapper {
 padding: 10px;
 margin: 20px;
 }
 </style>

 <script>
 function do_countdown() {
 var start_num = document.getElementById("value").value;
 var unit_var = document.getElementById("countdown_unit").value;

 start_num = start_num * parseInt(unit_var);

 var countdown_output = document.getElementById('countdown_div');

 if (start_num > 0) {
 countdown_output.innerHTML = format_as_time(start_num);
 var t=setTimeout("update_clock(\"countdown_div\", "+start_num+")", 1000);
 }

 return false;
 }

 function update_clock(countdown_div, new_value) {
 var countdown_output = document.getElementById(countdown_div);
 var new_value = new_value - 1;

 if (new_value > 0) {
 new_formatted_value = format_as_time(new_value);
 countdown_output.innerHTML = new_formatted_value;

 var t=setTimeout("update_clock(\"countdown_div\", "+new_value+")", 1000);
 } else {
 countdown_output.innerHTML = "And... Stop!";
 }
 }

 function format_as_time(seconds) {
 var minutes = parseInt(seconds/60);
 var seconds = seconds - (minutes*60);

 if (minutes < 10) {
 minutes = "0"+minutes;
 }

 if (seconds < 10) {
 seconds = "0"+seconds;
 }

 var return_var = minutes+':'+seconds;

 return return_var;
 }
 </script>
 </head>

 <body>
 <div id="body_wrapper">
 <form id="countdown_form" onsubmit="return do_countdown();">
 Countdown from: <input style="width: 50px;" id="value" />
 <select id="countdown_unit">
 <option value="1">Seconds</option>
 <option value="60">Minutes</option>
 <option value="3600">Hours</option>
 </select>
 <input type="submit" value="Go" />
 </form>
 <div id="countdown_div">&nbsp;</div>
 </div>
 </body>

</html>

The REAL Auto Increment

August 13th, 2009

I came across an interesting problem today. Not something that most people will realise or care about but something which seemed to absorb a good hour of my life searching for a solution.

Here goes..

MySQL tables, like any other, have an auto increment value to determine the next in line when inserting new data. How do we preempt this data though? How do we get the next insert ID before we insert the data? I can almost hear you now reeling off the following code:

SELECT MAX(id)+1 FROM table1;

You are wrong… What happens if you have 100 rows in ‘table1′.. the next insert is 101 right? wrong. We don’t have enough data to make anything other than an educated (and usually correct) guess.

Let me explain… What happens if you inserted a row by accident. This is easily done when the programmer doesn’t use single use tokens on insert queries. You then have to delete that row. What then would the next insert value be? 102 is the answer.

There is a MySQL command to allow you to get a list of table information within which is the Auto Increment value as follows:

SHOW TABLE STATUS LIKE 'table1';

There are two downsides to using this method..

  1. Your host might not allow you to use SHOW on your database meaning it would either result in an access denied or no recordset.
  2. The data is not sortable or filterable. This means you need to use some PHP logic to get the actual Auto Increment value from the recordset.

It isn’t that bad actually getting the data out of the recordset using PHP. See the following example using Wordpress database calls:

$sql = 'SHOW TABLE STATUS LIKE "wp_nmv_version"';
$status = $wpdb->get_row($sql);
echo $status->Auto_increment;

So if knowing the correct Auto Increment is vital to your system bare this article in mind before proceeding.

If anyone has any idea how to tidy this process up a bit to remove the PHP element from the filtering process then I would be glad to listen.

RSS Enclosures in Wordpress Vs Content Protection (YM)

July 31st, 2009

I have just completed an email exchange with a YM client which lasted around 50 replies and a good month. More down to my hectic schedule than anything else but I, with Googles help, have just solved it.

As you well know I work with Tim Nash over at CNMS and between us we develop and sell the Your Members plugin. The plugin protects blog content based on member levels and then provides the means to pay for access using a variety of methods. </plug>

It’s pretty standard stuff. However… What happens if you want to add some sort of streaming media to the protected section in a blog post?

It doesn’t work is the answer! Ever helpful Wordpress scans the post for the presence of certain HTML strings and adds post custom fields if it gets a hit. This is the case for podcasting and flash type plugins. Normally this would be a good thing, however, not when the Object in question is supposed to be protected. A pretty bit security hole in any content protection plugin.

At the most basic level the protection can be restored using the following code…

function delete_enclosure(){
    return '';
}

add_filter( 'get_enclosed', 'delete_enclosure' );
add_filter( 'rss_enclosure', 'delete_enclosure' );
add_filter( 'atom_enclosure', 'delete_enclosure' );

The code should be put into either a new plugin or more simply your functions.php file within your theme. Dead easy!

If you understand it, the code simply picks up on any filters and removes them. Problem solved.  For YM, the next step is not to remove the enlosure if the user should have access. Additionally we don’t want to remove the enclosure if the object is outside of the protected content area. However the function above will suffice for now :)