• Add to Technorati Favorites
  • Debt and Weight

    Debt
    Start (11/1/07): $51,100
    Current Debt: $41,341
    Debt Paid So Far: $9,759

    Weight
    Start 12/29/07: 245 lbs
    Current Weight: 227 lbs
    Target Weight: 185 lbs
    Pounds Lost: 18 lbs

  • Recent Posts

  • Categories

  • Pages

  • Archives

  • Spam Blocked

  • « Vacation at Last | Main | A Frugal Christmas »

    How to Add More Widgets to Wordpress

    By Jay | December 22, 2007

    I wrote yesterday that I wanted to make a few changes to my blog, and one of those changes is to have a few more widgets. Problem is, Wordpress’s default settings limit users to 9 widgets. So, as I have often had to do, I dug into the code behind my Wordpress blog.

    One of my big problems with Wordpress is how hard it is to make ad hoc customizations. Certain changes are easy, but if you want to change a default behavior you either need to have a lot of php and html skills, or have a lot of time to experiment. I don’t know much php, so I ended up using a lot of time — almost 3 hours. But now I can add up to 25 widgets to my blog, and it’s working perfectly.

    This isn’t meant to be a technical blog, but I’ll write down what I did so that you can do it in 15 minutes — and so that I’ll be able to refer to this post to change the number of widgets again without spending another frustrating 3 hours ;-)

    The instructions look scary, but it really isn’t that hard to follow them. First, ftp into your server and look for a file called widgets.php. I found the file for my blog in the wp-includes subdirectory. Copy it over to your desktop and make a copy! That way, if the changes I’m about to outline ruin your blog you’ll be able to replace the modified file with the original file that you downloaded.

    Then, open the file with a text editor like notepad. It’s a good idea to use a text editor that has a search function, as there is a lot of text in this file. Search for a function called wp_widget_text_setup. In this, you will see a line of code that says:

    if ( $number > 9 ) $number = 9;

    Change the number to match the number of widgets you want in your blog. (Warning, do not copy & paste the code below: make the edits manually within your code. You may have a different version of Wordpress than me, and your code might be slightly different.) In my case I set the number to 25:

    if ( $number > 25 ) $number = 25;

    So before the wp_widget_text_setup function looked like this:

    function wp_widget_text_setup() {
    $options = $newoptions = get_option(’widget_text’);
    if ( isset($_POST[’text-number-submit’]) ) {
    $number = (int) $_POST[’text-number’];
    if ( $number > 9 ) $number = 9;
    if ( $number < 1 ) $number = 1;
    $newoptions['number'] = $number;
    }
    if ( $options != $newoptions ) {
    $options = $newoptions;
    update_option('widget_text', $options);
    wp_widget_text_register($options['number']);
    }
    }

    Now it looks like this:

    function wp_widget_text_setup() {
    $options = $newoptions = get_option(’widget_text’);
    if ( isset($_POST[’text-number-submit’]) ) {
    $number = (int) $_POST[’text-number’];
    if ( $number > 25 ) $number = 25;
    if ( $number < 1 ) $number = 1;
    $newoptions['number'] = $number;
    }
    if ( $options != $newoptions ) {
    $options = $newoptions;
    update_option('widget_text', $options);
    wp_widget_text_register($options['number']);
    }
    }

    Now, go down to the next function, called wp_widget_text_page. In this function you’ll see the following code:

    php for ( $i = 1; $i < 10; ++$i ) echo "

    You need to change the 10 to 26. It must be one number higher than the number of widgets that you have chosen for your blog. This was the hardest part of the whole process for me to figure out.

    php for ( $i = 1; $i < 26; ++$i ) echo "

    So before the wp_widget_text_page function looked like this:


    function wp_widget_text_page() {
    $options = $newoptions = get_option(’widget_text’);
    ?>
    <div class=”wrap”>
    <form method=”POST”>
    <h2><?php _e(’Text Widgets’); ?></h2>
    <p style=”line-height: 30px;”><?php _e(’How many text

    widgets would you like?’); ?>
    <select id=”text-number” name=”text-number” value=”<?php

    echo $options[’number’]; ?>”>
    <?php for ( $i = 1; $i < 11; ++$i ) echo “<option value=’$i’

    “.($options[’number’]==$i ? “selected=’selected’” : ”).”>$i</option>”; ?>
    </select>
    <span class=”submit”><input type=”submit”

    name=”text-number-submit” id=”text-number-submit” value=”<?php echo

    attribute_escape(__(’Save’)); ?>” /></span></p>
    </form>
    </div>
    <?php
    }

    Now it looks like this:


    function wp_widget_text_page() {
    $options = $newoptions = get_option(’widget_text’);
    ?>
    <div class=”wrap”>
    <form method=”POST”>
    <h2><?php _e(’Text Widgets’); ?></h2>
    <p style=”line-height: 30px;”><?php _e(’How many text

    widgets would you like?’); ?>
    <select id=”text-number” name=”text-number” value=”<?php

    echo $options[’number’]; ?>”>
    <strong><?php for ( $i = 1; $i < 26; ++$i ) echo “<option value=’$i’</strong>
    “.($options[’number’]==$i ? “selected=’selected’” : ”).”>$i</option>”; ?>
    </select>
    <span class=”submit”><input type=”submit”

    name=”text-number-submit” id=”text-number-submit” value=”<?php echo

    attribute_escape(__(’Save’)); ?>” /></span></p>
    </form>
    </div>
    <?php
    }

    Now one more step. This is the last step, I promise.

    Search for a function called wp_widget_text_register. Wherever you see a nine, change it to the number of widgets you have chosen. Again, I chose 25.

    Before the change:

    function wp_widget_text_register() {
    $options = get_option(’widget_text’);
    $number = $options[’number’];
    if ( $number < 1 ) $number = 1;
    if ( $number > 9 ) $number = 9;
    $dims = array(’width’ => 460, ‘height’ => 350);
    $class = array(’classname’ => ‘widget_text’);
    for ($i = 1; $i <= 9; $i++) {
    $name = sprintf(__(’Text %d’), $i);
    $id = “text-$i”; // Never never never translate an id
    wp_register_sidebar_widget($id, $name, $i <= $number ?

    After the change:

    function wp_widget_text_register() {
    $options = get_option(’widget_text’);
    $number = $options[’number’];
    if ( $number < 1 ) $number = 1;
    if ( $number > 25 ) $number = 25;
    $dims = array(’width’ => 460, ‘height’ => 350);
    $class = array(’classname’ => ‘widget_text’);
    for ($i = 1; $i <= 25; $i++) {
    $name = sprintf(__(’Text %d’), $i);
    $id = “text-$i”; // Never never never translate an id
    wp_register_sidebar_widget($id, $name, $i <= $number ?

    Now, copy your file back to the server (make sure you have your backup file!) and test to make sure everything works. If you have a problem, just copy the old file with the “9’s” back over the new one.

    So now you know how I spent the first day of my vacation: I must be crazy….

    Popularity: 20% [?]

    Stumble it!

    Topics: Blogging |

    Comments