Archive for the ‘PHP’ 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

PHP File Rename Script

September 26th, 2009

Ever downloaded an archive of files and all of them had file names about 200 characters wrong with the same string in each? It’s more common that you might think… If you look at any audio archives you may have acquired, the chances are that the file names will have the quality in it (192 or 128 etc..). Additionally if you have video files the resolution or quality might be present.

I, for one, don’t really care what the quality is in the file name and would prefer it to be readable. I spend a lot of time removing this extraneous data so decided to make a short PHP script to remove certain strings on mass.

See the following code which can be run from the command line using the following format:

php -f <filename> <text_to_remove> [int live_mode=0]

Code

#!/usr/bin/php

  [int live_mode=0]';
    exit;
} else {
    $remove = $argv[1];
}

$live_mode = (int)$argv[2];

if ($dir = scandir($cwd)) {
    foreach ($dir as $file) {
        $path = $cwd . '/' . $file;

        if (!in_array($file, $ignore)) {
            if (!is_link($path) && !is_dir($path)) {
                if (strpos($file, $remove) !== false && $file != $_SERVER['SCRIPT_FILENAME'] && $file != $remove) {
                    $output .= 'Renamed "' . $path . '" to "' . $cwd . '/' . trim(str_replace($remove, '', $file)) . '"' . "\n";

                    if ($live_mode) {
                        $new_path = $cwd . '/' . trim(str_replace($remove, '', $file));
                        rename($path, $new_path);
                    }
                }
            }
        }
    }

    if ($output) {
        $output = ($live_mode ? 'LIVE':'TEST') . ' MODE' . "\n\n" . 'Text to remove from filenames was "' . $remove . '"' . "\n\n" . $output;
    } else {
        $output = 'There were no matches by your criteria: "' . $remove . '"';
    }    

    echo $output . "\n\n";
}

?>

Download

To download the code in file form click the following link: File Rename Script (783 bytes)

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