Posts Tagged ‘Wordpress’

Your Captcha Adverts

November 25th, 2008

I have written another wordpress plugin called Your Captcha Adverts which basically does what it says on the tin. It is a Captcha plugin for wordpress that asks the user to write an answer to something relating to an advert that you upload.

Features include:

  • Support for Registration & Comment forms (Inbuilt support for adding more)
  • Advanced advert display management (optionally set your own times and dates for on and off)
  • Multiple questions per advert
  • Accessibility support (alt tags and hint option)

We will be releasing it on or after 25/12/08 on Newmedias so keep an eye out and let me know what you think!

Your Classifieds – What is it

November 10th, 2008

I started writing a Classified Adverts plugin for wordpress recently and then handed over the reigns to another programmer under my watchful eye. We will shortly be releasing the plugin once the documentation is complete and its all bundled up.

The plugin will be FREE and you will also have access to the Newmedias support forums where myself and Tim Nash are available most of the time to answer any questions and sort out and problems for you.

The plugin allows for category groups to be created and adverts (or more categories) created within each one. Adverts can either be created by an admin exclusively or by any site member using the form on the front end.

Give it a go and let us know what you think. Ask Tim for the beta if you are interested or wait for the product version. Available at www.newmedias.co.uk

Deals Press

November 10th, 2008

Ever been writing a post for your blog and when talking about a product you would like to give a list of suppliers and their prices (maybe with some affiliate links so you make some money!), well if it wasn’t written already then it is now.

Gaj from www.gaj-it.com asked CNMS to write this very plugin a few weeks ago and he is in the process of using it on his website at the moment.

Deals Press, as it’s been branded, allows you to create products on your blog and store prices for each from various suppliers. It is highly configurable and templated so you can make your adverts look as clever as you like.

Price histories are kept in the Database and a stats module is planned to show you prices over time and cheapest suppliers, etc…

I don’t yet have a URL for it although I will update this post when I know.

SACK of AJAX

September 30th, 2008

For those of you that don’t know what AJAX is, please read my simple tutorial here. For those of you that do, carry on reading…

I wanted to use some AJAX within a wordpress plugin i’m writing called Your Responder to handle a form submission and feedback without having to refresh the page.

I was reading through the Codex looking for anything AJAX related and came across a bundled script called SACK. It actually stands for Simple Ajax Code Kit and i’m happy to say it does exactly what it says on the tin. I haven’t even looked at the advanced features yet but a basic ajax request can be made in a few small lines of easy to follow code.

The full wordpress example is here but I thought I would give a explanation of my own…

The first stage is to incorporate the SACK library. Of course there is a special way to do this in wordpress but it’s very simple:

wp_print_scripts(array('sack'));

Copy that somewhere into your plugin and it does the same as putting a ‘<script src=”" />’ into the head of the html page.

Next comes the actual call:

  var mysack = new sack("www.yoursite.com/wp-content/plugins/myplugin_ajax.php");    

  mysack.execute = 1;
  mysack.method = 'POST';
  mysack.setVar("vote", vote_field.value);
  mysack.onError = function() { alert('Ajax error in voting'); };
  mysack.runAJAX();

  return true;

Does it look complicated? If your answer is yes then I urge you to have a look at the actual AJAX code to accomplish a POST request. I shall go through this line by line to help it make a little more sense:

The first line sets up a new variable called mysack which is actually an occurence of the mysack object. It allows you to call any function from the library and set up variables within it without interfering with any other SACK requests on the same page. The url in the call is an absolute link (I have yet to test with relative links) to the PHP script that the request will be sent to, much like the ‘<form action=”">’ tag.

Ignore the execute line. For a simple example just appreciate it has to be there. The method line is directly the same as ‘<form action=”">’ so can accept, to my knowlege,  either GET or POST.

The next line ‘setVar’ you can duplicate for each field you want to send to your back end script. So if you wanted to send something that will appear to the server as $_POST['field1'], you simply write:

mysack.setVar("field1", document.getElementById("input_field_id").value);

Remember this is a JS function so where I put my document.getElement… call, you can write anything that javascript understands (although to keep it tidy, it’s probably best to sort it out earlier and store in a variable).

On error is probably best to leave empty unless you want to show a div on the screen saying something like ‘invalid request’. Remember this will be shown to your site users to an alert box probably isn’t the best approach.

Finally we fire off the request using runAJAX().

The PHP backend

At this stage your user is looking at your site and has, in some way or another, fired off an AJAX request. If the request was intentional (ie: not triggered by something passive) then they most likely expect something like a message back from the server or event to occur.

In the above example we declared a new instance of the SACK library with the url we want to send a request to (www.yoursite.com/wp-content/plugins/myplugin_ajax.php). The PHP script we need to write obviously needs to be at this location.

There are two things you really need to remember with this:

  1. AJAX requests accept anything echoed as it’s response and will throw an error if it’s not sent proper JS code.
  2. Use a PHP ‘die’ statement to send your response to the client.

All the first one means is that, when testing, a print_r($_POST); will not work properly and if you can see AJAX/JS errors (via Firefox and Firebug or similar) then you will most likely be prompted to fix your code.

The second one is simply a recommendation from the WordPress development team which I agree with. If you concatenate your response JS code into a variable and then run a ‘die’ with your variable as the argument then it’s likely to work.

See the following example of what would be your ‘myplugin_ajax.php’ file:

<?php
if (isset($_POST['vote']) {
    //Perform some DB bits here
    $feedback = 'alert("Your vote was successfully recorded");';
    die($feedback);
}
?>

If you now understand what to do then I recommend trying it before reading the next section because it will only confuse matters if you are just getting to know either AJAX or WordPress.

We are checking in $_POST for a variable called ‘vote’ and notice there is no ‘else’ statement. We don’t need an else unless you want to report error messages back to the server which in this case I don’t.

WordPress doesn’t allow immediate access to any files inside it’s directory structure so things like Database access and shared function usage won’t be possible. The only way we can make this work is to incorporate the handing code into the main plugin itself. Simply add your version of this code to your main plugin PHP file which will be called from within wordpress.

This also means that your setup URL for SACK will simply be the homepage of your blog. This will work because wordpress scans it’s plugin directory when it initiates its framework looking for hooks. As long as your plugin is active it will run your logic. The die statement will also stop that instance of the script from running hence the if statement with no else.

Good luck and let me know if you need any help, spot a mistake or just want to say something :)

bbPress Email notification on post

September 9th, 2008

At Newmedias, we use bbPress and have been using the email notification plugin written by thomasklaiber. It sends an email to those registered for forum notifications every time a post is left on a thread that they had registered for.

http://bbpress.org/plugins/topic/post-notification/

I often look at my emails when im away from home on my pda/phone and find it a real pain to have to open the web link in each email to read it. This also means that I can’t download them in my email client for reading later.

My solution to this was blindingly simple,

Put the body of the most recent post into the email itself!

This feature was not available at the time of writing and works very well. The mod is very simple, here’s how i did it.

note: Before doing this to your version of the plugin, make sure that the original author hasn’t added his own version of the same thing!

1) Open notification.php and find the notification_new_post function
2) Paste the following code after the get_topic() call but before the $message=… call

$posts = get_thread($topic_id, 1, 1);
$posts = array_reverse($posts);
$last = array_pop($posts);
$last_text = strip_tags($last->post_text);

3) Overwrite the $message variable to include the new string at the end as follows:

$message = __("There is a new post on: %1\$s \nReply by: %2\$s \n\n%3\$s \n\n%4\$s");

4) Overwrite the sprintf call to the following:

sprintf($message, $topic->topic_title, get_user_name($bb_current_user->ID), get_topic_link($topic_id), $last_text)

(if you are more familiar with php and sprintf then just add the $last_text variable to the end of the list of variables within the existing call)

5) Save, Backup, Upload and test! It should now send the post body with the email

Optionally you can improve the code within this function somewhat…

1) The get_topic call doesn’t need to be in the foreach loop because the topic_id doesn’t change anywhere in the function. Although bbPress uses cacheing, it would still be best practise to move that line and the lines from my step 2 to the line before the foreach loop.

2) Convert the email to HTML by adding the content-type header and setting it to text/html. If you do this then the \n’s will also need to be changed to <br /> and you can optionally remove the strip_tags() from the last line in my step 2

Good luck and let me know about any problems you may have.

Your Minder – Single user login protection for WordPress

August 9th, 2008

I have just finished work on a new WordPress plugin to add to the Your Brand group of products. We have called it Your Minder and basically what it does it stops people account sharing.

The idea of giving your login details to a friend so they can benefit from your increased access level is not uncommon in any system, especially those you have to pay for. We decided to write it as the perfect companion for the Your Members plugin which turns your wordpress blog into an (optionally) paid membership site.

Your Minder works by stopping logins from multiple IP addresses over a period of time. If multiple logins are detected in the (fully configurable) time period then you have the option for it to perform one of many actions including lock out both accounts for either a time period or until the activation link (sent via email) is clicked among others.

Yes, like many of the plugins i’ve written it’s not free but it certainly won’t break the bank at under a tenner (09/08/08) and will ensure a greater income from your members over time.

Take a look at the full spec and screenshots on newmedias to find out more! http://www.newmedias.co.uk/your-minder/

WordPress Session Management

July 14th, 2008

I’ve been working on YourMembers for some time now and have started selling it on newmedias with Tim Nash and I wanted to get some useful stats on the current state of the website. Luckily the selling site is running on WordPress so I put together a simple plugin to give me some information on my last days visitors.

Believe it or not I call it ‘session manager’ and It’s freely available through this site and via the wordpress plugin directory (when I get around to uploading it). It gives you the following information:

  • The number of visitors in the last day (configurable)
  • The number of pages each visitor looked at
  • Which they were
  • The times each page was visited

That’s it!

I find that a lot of stats plugins give what I would call too much information (ie pretty graphs, information on screen resolution and things like which browser the client is using). Session Manager differs because it provides is a very light weight single table implementation giving 90% of the functionality for 10% of the overhead.

I have been using it on this site and newmedias for the last week or two and it has been instrumental in giving me enough stats data to satisfy that ‘whose looking at my site’ itch. Exactly what I needed!

Any feedback would be greatly appreciated and if anyone fancies having a look at the code and trying to make something more of it then i would be keen to hear about it. I have written it in very clear coherent PHP that anyone could understand.

See the Session Manager page for the download link.