WordPress 2.2: Three Things Developers Will Like

In the newly released WordPress 2.2 the built-in widget support is likely to get most of the attention. Here are three things you as a WordPress plugin or theme developer might also find useful.

  • You can now set your site’s “home” and “siteurl” values in the wp-config.php file

    Say you have a production site and a mirror development site. Before 2.2, if you copied the database from one to the other, you’d have to edit the database to change the url options. Now, you can set the “home” and “siteurl” options in the wp-config.php file:

    define('WP_HOME', 'http://www.ilfilosofo.com');

    define('WP_SITEURL', 'https://austinmatzko.com/wordpress');

    That means that if you use different config files, you can dump the database from one site to the other without making any option changes. It’s going to be a big time-saver for me.

  • WordPress now uses PHPMailer for email

    PHPMailer is a nifty mail class. You can use it independently of the wp_mail function (but if you do that you’ll need to include the


    class-phpmailer.php
     
    and

    class-smtp.php
     
    files manually). A simpler way to use it is to access the $phpmailer object as it’s passed by reference to the ‘phpmailer_init’ action hook.

    For example, say you want to add an attachment to an email you’re about to send using wp_mail. You would set up a callback function to add the attachment, using PHPMailer’s AddAttachment method:


    function lets_add_an_attachment(&$phpmailer) {
     


    $phpmailer->AddAttachment('/path/to/attachment/picture.jpg');
     

    }

    Then hook in the callback function:


    add_action('phpmailer_init', 'lets_add_an_attachment');
     

    If that doesn’t seem easy enough, you should try using PHP’s mail function (for which wp_mail used to be basically just a wrapper) to send an attachment. First, you’d need to read in the file to be attached, encode it to base 64, split it up into chunks, then pass it to the header parameter, of course setting the correct Content-Type and MIME-Version and indicating the boundary. PHPMailer takes care of that headache for you.

  • WordPress Now Includes the jQuery JavaScript library

    WordPress has been including the robust Prototype and Scriptaculous JavaScript libraries since 2.1 for various admin panel JS effects. But now the admin panel is being transferred to jQuery. jQuery has the advantage of packing a lot of cool features into a small size (19kb).

    Although the only place jQuery appears in 2.2 is the new Blogger importer, you can use jQuery on any WordPress page. All you have to do is call wp_enqueue_script('jquery'); before the page’s header prints.

    jQuery as bundled with WordPress is running in “no-conflict” mode. That’s so that jQuery will play nice with Prototype, which would otherwise conflict over the ‘$’ function. Whereas normally you could use ‘$’ as a shortcut for ‘jQuery,’ in no-conflict mode you either need to call jQuery directly or create your own alias, like so:


    var wpJ = jQuery.noConflict();
     
    (from then on you could use wpJ instead of jQuery)

    One of my favorite features of jQuery is its compact way of selecting DOM elements. For example, the following code finds all text input fields named “s” (i.e. the WordPress search fields), sets their value to “Search text,” then clears their value if someone clicks in them.

    jQuery('input[@type="text"][@name="s"]').each(

    function() {

    jQuery(this).attr('value','Search text');

    jQuery(this).focus(function() {jQuery(this).attr('value','')});

    }

    );

    Elegant, huh?

10 Comments

  1. That config file feature is awesome. I recently switched hosts and had a hard time testing my installation because it would have been a pain in the *** to manually edit the database and then have to switch back with the nameservers finally kicked in.

  2. The new constants seem like a great idea… can’t get them to work though, and can’t find any docs :(

    Do they work if the site’s already been setup (i.e. if those option values have already been set in the DB)?

  3. Simon,

    You just define the constants in your wp-config.php file, and they override any existing db option values.

  4. wp_mail() is broken, though. It’s impossible to set the Content-Type. _Every_ plugin sending non-text mails is broken.

  5. Morty,

    For anything beyond a plain-text message, you have to use the PHPMailer API. So to set the content-type (although you probably won’t need to do that directly now), you would do something like

    $phpmailer->ContentType = "text/plain";

  6. Actually it’s

    $phpmailer->IsHTML(true)

    but I wrote a plugin to fix the wp_mail() – function. One good reason to use mp_mail() instead of the phpmailer API is, that it’s a plugable function, and it might be overwritten by a plugin. E.g. because the Admin wants to use a totally different way of sending mails.

  7. Great info. I’m disappointed that they didn’t actually include the phpmailer and smtp classes in the build for folks who wish to utilize them for plugin and theme use. Oh well!

  8. Hi,
    When will you be updating your plug-in to support WP 2.2. I’ve found that your db backup plugin, when activated, causes user registration emails to be sent with a blank body in the message. Do you have a fix or suggestions for making a fix for this?

    Thanks,

    Aaron
    http://www.takbax.com

  9. Aaron,

    If you’re having trouble with the wp-db-backup plugin, please open a ticket in my support forum (and let me know which version of the plugin you’re using).

    Thanks

  10. Using PHPmail was a very importent step for wordpress! Hope the wp-guys take the innovativ way always in the future!!

Post a Comment

Your email is never shared. Required fields are marked *

*
*

28 Trackbacks