Random Sentences, your first WordPress plugin
Posted by Alberto - 16/01/11 at 12:01:00 amI have been reading about WordPress plugin development, and I have written a dummy example. This plugin allows you to include random sentences in your blog. This is used in a lot of sites, for example, to change a phrase that appears under the main title. I felt very creative, so I called this plugin “Random Sentences” (hurray for me!).
First, we have to create a random-sentences.php file in our wp-content/plugins folder, and write the plugin info in it.
<?php /* Plugin Name: Random Sentences Version: 0.1 Plugin URI: http://www.albertobustamante.com Description: Basic plugin that shows a random sentence Author: Alberto Bustamante Author URI: http://www.albertobustamante.com */ ?>
If you check your plugins in your WordPress installation, you will see our great brand new plugin with the information you wrote in the file:

But right now, our plugin is kind of boring, isn’t? Lets write the function that will choose a random sentence. I have called it get_random_sentence():
<?php
/*
Plugin Name: Random Sentences
Version: 0.1
Plugin URI: http://www.albertobustamante.com
Description: Basic plugin that shows a random sentence
Author: Alberto Bustamante
Author URI: http://www.albertobustamante.com
*/
function get_random_sentence(){
$all_sentences = array(
1 => array( "sentence" => "This is the sentence 1",
"author" => "Anonymous 1" ),
2 => array( "sentence" => "This is the sentence 2",
"author" => "Anonymous 2" ),
3 => array( "sentence" => "This is the sentence 3",
"author" => "Anonymous 3" ),
);
$min = 1;
$max = 3;
$index = rand($min,$max);
$output =
'<div id="RS_div">
<p id="RS_sentence">'
.$all_sentences[$index]["sentence"].
'</p>
<p id="RS_author">'
.$all_sentences[$index]["author"].
'</p>
</div>';
echo $output;
}
?>
As you can see, its a very simple function. It uses an array to store the sentences and its authors. I will try to store the sentences in the data base in the next version.
If you want to use this code, save the file, activate the plugin and include this line where you want the random sentences to appear:
<?php get_random_sentence(); />
Finally, write your own CSS code to adapt the output to your theme (I included ids in the html tags to do it).
Fixing WordPress Error 500
Posted by Alberto - 10/12/09 at 09:12:17 pmAfter this, I’ve learned that its convenient to deactivate all your plugins before updating to the last version of WordPress. Im not sure when I did the last update, but it seems this blog have been offline since then.
This morning I tried to read an article of the blog, and I got an “Error 500″ message. First of all, I checked that my WordPress was working, because I could access to my dashboard. Next step: deactivate all the plugins. But it didnt work. So lets take a look into the Apache log. What I found was:
SoftException in Application.cpp:252: File “/home/xxxxx/public_html/blog/index.php” is writeable by group
Thats it: a permissions problem. So I used Cyberduck (but you can use any other FTP application) to fix it. Delete the “write” permissions of Group and Others:

Note: be sure to apply this to all your files and directories. In my case, as you can see in the image, I used the option “Apply changes recursively”. Your FTP application should have the option to do something like this.
Fixing WordPress Error 406
Posted by Alberto - 14/05/09 at 09:05:06 pmLet me guess: you are trying to edit posts or pages in your WordPress blog and you got “Error 406, Not Acceptable. An appropriate representation of the requested resource /wp-admin/post.php could not be found on this server”. Ok, dont panic, lets fix it.
In the root directory of your WordPress installation, you should have a file called “.htaccess”. You have to edit this file, adding the following line:
SecFilterEngine off
With this single line, your blog will work perfectly.
Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds.
Valid XHTML and CSS.


