Category Archives: WordPress

All about my love for WordPress. BTW, I am self-claimed WordPress Super star.

Using WordPress built-in tag auto complete script in your Plugins

When I released my Posts By Tag WordPress Plugin, I promised to write a post explaining how to use the built-in auto complete tags script in WordPress, and so here is how you do it. ๐Ÿ˜‰

jQuery Plugin

WordPress has a jQuery Plugin called suggest, which will do the heavy lifting for us. To use this Plugin, you have to first enqueue the script using the wp_enqueue_script() function

// Register hooks
add_action('admin_print_scripts', 'add_script');

/**
 * Add script to admin page
 */
function add_script() {
    // Build in tag auto complete script
    wp_enqueue_script( 'suggest' );
}

Once the script is included, you have to just call a method called suggest on your jQuery collection and everything else will be taken care for you.

<?php
/**
 * add script to admin page
 */
function add_script_config() {
?>

    <script type="text/javascript" >
    // Function to add auto suggest
    function setSuggest(id) {
        jQuery('#' + id).suggest("<?php echo get_bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php?action=ajax-tag-search&tax=post_tag");
    }
    </script>
<?php
}
?>

If you want to support multiple tags, then you can use the following options.

  • multiple – to enable multiple tags to be entered, pass true
  • multipleSep – The separator symbol for multiple tags

so the code will be

<?php
/**
 * add script to admin page
 */
function add_script_config() {
?>

    <script type="text/javascript" >
    // Function to add auto suggest
    function setSuggest(id) {
        jQuery('#' + id).suggest("<?php echo get_bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php?action=ajax-tag-search&tax=post_tag", {multiple:true, multipleSep: ","});
    }
    </script>
<?php
}
?>

You can also use this script to auto complete other custom taxonomies, which were introduced in WordPress 2.8. For that you have to change the tax parameter in the url. The following are the default taxonomies that are available.

  • post_tag
  • link_category
  • category

In addition to them you can also use your custom taxonomies.

So the complete code is

<?php
// Register hooks
add_action('admin_print_scripts', 'add_script');
add_action('admin_head', 'add_script_config');

/**
 * Add script to admin page
 */
function add_script() {
    // Build in tag auto complete script
    wp_enqueue_script( 'suggest' );
}

/**
 * add script to admin page
 */
function add_script_config() {
?>

    <script type="text/javascript" >
    // Function to add auto suggest
    function setSuggest(id) {
        jQuery('#' + id).suggest("<?php echo get_bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php?action=ajax-tag-search&tax=post_tag", {multiple:true, multipleSep: ","});
    }
    </script>
<?php
}
?>


The only cavet to this method is that right now the admin-ajax.php file needs you to be logged in and therefore can only be used in admin pages. But in WordPress 2.9 even anonymous users can load admin-ajax.php file. If you need use auto tag completing in blog pages, then you may have to wait till 2.9 is released ๐Ÿ™‚

Posted in WordPress | Tagged , , , | 26 Comments

Associating urls created with bit.ly API to your account

Recently while working on adding the feature to enter your own bit.ly API key to my Easy Retweet WordPress Plugin, I found out that by default all short urls created using bit.ly API (both REST and JavaScript APIโ€™s) are not associated with your account.

Upon further researching I found that there is an undocumented way of associating the urls created with your account. The undocumented way is to add an additional parameter called history with value 1 to the API URL.

Using the history parameter in bit.lyโ€™s REST API

So for REST API, you have to use the following url.

http://api.bit.ly/shorten?version=2.0.1&longUrl=http://sudarmuthu.com&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&format=json&history=1

If you are using PHP, then code would be

function get_bitly_shorturl($longurl) {
	$url = "http://api.bit.ly/shorten?version=2.0.1&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&format=json&history=1" . "&longurl=$longurl";

	//using curl
	$curlObject = curl_init();
	curl_setopt($curlObject,CURLOPT_URL,$url);
	curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
	curl_setopt($curlObject,CURLOPT_HEADER,false);

	$result_json = curl_exec($curlObject);
	curl_close($curlObject);

	//decode JSON. Assumes that it is PHP5
	$result = json_decode($result_json);

	return $result['results'][shortUrl];
}

If you are going to use it in WordPress, then you can use the inbuilt WP_Http class instead of curl as suggested by Ozh. The following code shows you how it can be done in WordPress

function get_bitly_shorturl($longurl) {
	$url = "http://api.bit.ly/shorten?version=2.0.1&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&format=json&history=1" . "&longurl=$longurl";

	//using WP_Http present in WordPress
	$request = new WP_Http;
	$result_json = $request->request($url);

	$result = json_decode($result_json);

	return $result['results'][shortUrl];
}

Using the history parameter in bit.lyโ€™s JavaScript API

If you are using bit.lyโ€™s JavaScript API, then it is not as straight forward as the REST API. Instead of using the provided shorten method, you have to use the low level call method.

The following code shows you how you can do it in JavaScript API.

BitlyClient.call('shorten', {'longUrl':'http://sudarmuthu.com', 'history':'1'}, 'BitlyCB.shortenResponse');

I am not sure why bit.ly is not associating the created shorturls automatically with your account, when you provide the API Key, it is the expected default behavior. Or at least they could have documented about this history variable in their API. I guess only someone from bit.ly can answer this. ๐Ÿ™‚

Posted in API's/Mashup, JavaScript/jQuery, WordPress | Tagged , , | 8 Comments

Proper way to display logout link in WordPress

Recently, I was debugging an issue with logout for a friend who was running a WordPress blog. His theme had a logout button in the sidebar, which stopped working after he upgraded to the latest version of WordPress from a pretty old version.

After some debugging, I found that the nonce value was missing from the logout link. In older versions of WordPress, the nonce value was needed, but in newer versions, we need to pass the nonce value to perform the logout.

wp_logout_url

We need not append the nonce value manually, instead we can use build-in function wp_logout_url(). This function will automatically append the nonce value to the logout link.

If you want the user to be redirected to the current page he is viewing, after logging out, you can use the following code snippet.

<a href="<?php echo wp_logout_url();?>&redirect_to=<?php echo $_SERVER['REQUEST_URI']; ?>"><?php _e('Logout'); ?></a>

Posted in WordPress | Tagged , | 1 Comment

Time to update. WordPress 2.8.3 have been released

The title says it all. WordPress 2.8.3 have been released and itโ€™s time to click your update button one more time.

WordPress 2.8.3 fixes the privilege escalation issues, which was not fixed properly in WordPress 2.8.1, which affects blogs that have multiple authors, with some of them having limited access to screens in the admin UI.

Since it is a security release, it is highly recommended that you update. Happy updating ๐Ÿ˜‰

Posted in WordPress | Tagged | 2 Comments

Help me win the WordPress Plugin competition

As you would have probably known by now, I am participating in the WordPress Plugin competition and have submitted 5 Plugins for it. (Not sure if it is the highest by an individual and also if they have a prize for it ๐Ÿ˜‰ )

The following are the list of Plugins that I have released for the competition, with their description and voting url.

Easy Retweet

Easy ReTweet is a WordPress Plugin, which letโ€™s you add retweet or Tweet this buttons for your WordPress posts, together with the retweet count.

Geo Mark

Geo Mark is a WordPress Plugin which will automatically locate Geo information in your WordPress posts using Yahoo Placemaker and YQL APIโ€™s.

Posts By Tag

Posts by Tag WordPress Plugin, provides sidebar widgets which can be used to display posts from a specific set of tags in the sidebar.

WP IRC

Well WP-IRC Plugin can fetch the number of users online in any IRC channel and can be scheduled to auto refresh it every x.minutes.

Tweetbacks Helper

Tweetbacks Helper is a helper Plugin for Tweetbacks Plugin to help it detect more tweets.

So if you have used any of these Plugins and like it, then please do vote for them at the Plugin Competition blog and help me win the competition. ๐Ÿ™‚

I am planning to continue develop them and so if you have any feedback/comments or feature requests then do leave a comment in the particular Pluginโ€™s page.

Posted in WordPress | Tagged , , | 8 Comments

WordPress 2.8.2

Just a quick note guys, WordPress 2.8.2 has been released a couple of hours back.

It fixes an XSS vulnerability. Comment author URL was not fully sanitized when displayed in the admin console.

Since it is a security update, it is highly recommended that you update as soon as possible. Even if you have not updated to 2.8.1, it is highly recommended that you update to WordPress 2.8.2

Posted in WordPress | Tagged | 3 Comments

WordPress 2.8.1

WordPress 2.8.1 had been released today. It is the bug fix version of WordPress 2.8 which was released last month.

WordPress 2.8.1 fixes a couple of security and performance related bugs. You can see the complete list of bugs that were fixed in the official announcement blog post.

Even though the bugs that were fixed affect only a small subset of blogs, it is highly recommended that you upgrade to the latest version. So if you maintain, WordPress blogs, then itโ€™s time to click the upgrade button. ๐Ÿ™‚

Posted in WordPress | Tagged | 3 Comments

WordPress Plugin readme generator now supports changelog

One of the most missed features in the official WordPress Plugin repository is the ability to specify the change log for your Plugins. There was no unified way for the Plugin developers to specify the changes that were made to the Plugin in each version.

Thanks to some great work done by Peter Westwood and Mdawaffe, we now have support for changelogs in the WordPress Plugin Readme standard. The information specified in the changelog will be displayed in a separate tab in the WordPress extend pages like shown in the below screenshot from WTC.

Changelog in Action

Since it has become as part of the readme file standard, I have added support for changelogs to my WordPress Plugin readme generator which I have released sometime ago. Now you can create readme files for your Plugins with changelogs using the WordPress Plugin readme generator. Try it out and let me know if you face any issues or have any suggestions.

Posted in WordPress | Tagged , , , | 3 Comments

Exciting new features for developers in WordPress 2.8

Update: WordPress 2.8 is out now. Update at your earliest and enjoy ๐Ÿ™‚

WordPress 2.8 is tentatively scheduled to be released on June 10. Even though there are no major new features, there are certain features that will be exciting for WordPress developers like me ๐Ÿ™‚grey-l

I am listing out some of them below.

Custom Taxonomies

WordPress 2.8 is going to support custom taxonomies in addition to the three built-in taxonomies category, tag and link_category. I am very excited about this, because when a new taxonomy is added, WordPress 2.8 will automatically add meta boxes in the Write Post page and will also add new Admin menus to handle them. Justin Tadlock has written article where he describes custom Taxonomies in detail.

New Widget API

There is going to be a new and improved API for writing widgets in WordPress 2.8. Right now creating widgets, especially multi-instance widgets is complex.

In WordPress 2.8, there is going to be a new class called WP_Widget which will abstract most of the complexity involved in creating widgets. This new class supports both single and multi-instance widgets. Justin Tadlock has also written an excellent guide which explains how widgets can be created using the new widget API.

Enhanced code Editor

The Plugin and theme editors have been enhanced tremendously. They now have support for syntax highlighting and function lookup. Personally, this is a great news for me because I edit most my Plugins using this editor and syntax highlighting will surely help me to do it quickly.

wordpress-plugin-editor

Promotion to migrate to PHP 5

Even though WordPress 2.8 will continue to support PHP 4, the automatic upgrader will suggest those running PHP 4 to switch to PHP 5. It will also link to a Codex page describing how to switch for various hosts should be provided. At last there are symptoms of light at the end of the tunnel ๐Ÿ˜‰

Authentication more pluggable

Even though the support for oAuth has been moved to 2.9, the initial steps towards the integration have been done and now the authentication code of WordPress is more pluggable.

SimplePie in core

SimplePie, the well know PHP library for RSS parsing is going to be included in the core. This is going to be great news because, this will make available the power of RSS parsing to all Plugins without installing the SimplePie core Plugin.

Loading JavaScript in footer

The high performance website rule states that we have to push JavaScript to the footer for better performance. WordPress 2.8 now makes it possible for Plugins to specify whether a script should be included in the header or in the footer. Isnโ€™t that exciting? ๐Ÿ˜‰

Other minor exciting features

In addition to the above major features, WordPress 2.8 also has the following features which may be of interest to developers.

  • Filters for AIM, Yahoo, and Jabber IM labels, in user profile, so that, they can be changed via Plugins.
  • New hook “after_db_upgrade”, which you can use to add upgrade functions for your Plugin.
  • Added support for blocking all outbound HTTP requests via Plugins.
  • Ability for a Plugin to control how many posts are displayed on edit pages.
  • Support for proxy. Helpful, if your installation is in an intranet.
  • Menus can be reordered via Plugin

Update libraries bundled with WordPress

The following are the libraries (with their version numbers) that are updated in WordPress 2.8

  • TinyMCE – 3.2.4.1
  • jQuery – 1.3.2
  • Jcrop – 0.9.8
  • pclzip – 2.8
  • PHPMailer – 2.0.4
  • SWFUpload – 2.2.0.1

I am pretty excited about WordPress 2.8 and eagerly waiting for it. If you want to get your hands dirty with it right now, you can try out WordPress 2.8 Release candidate 1.

Update: WordPress 2.8 is out now. Update at your earliest and enjoy ๐Ÿ™‚

Posted in WordPress | Tagged | 2 Comments

Making Yahoo Pipes feed work in wp-o-matic

Recently I was playing with wp-o-matic WordPress Plugin using a feed from Yahoo pipes. For the uninitiated, wp-o-matic is a WordPress Plugin which enables you to create new posts automatically from RSS or Atom feeds and Yahoo pipes is an interactive feed aggregator and manipulator.

After configuring everything, wp-o-matic kept on saying โ€œ0 posts fetchedโ€, even though there were some posts in the feed generated by Yahoo pipes. After some research, I found out that I was not the only one facing this issue and there seems to be a bug in wp-o-matic which breaks feed urls having ampersand (&) character.

After digging into the code further, I found out the url is HTML encoded and stored in the database and is not decoded while retrieved. This occurs in the addCampaignFeed() function at line 1011 in the wpomatic.php file.

function addCampaignFeed($id, $feed)
{
    global $wpdb;
    $simplepie = $this->fetchFeed($feed, true);
    $url = $wpdb->escape($simplepie->subscribe_url());
    // code continues

In the above code, the function $simplepie->subscribe_url() returns the html encoded url. So to fix this, we have to pass the url to the htmlspecialchars_decode function. So to fix the bug we have to use the below code.

function addCampaignFeed($id, $feed)
{
    global $wpdb;
    $simplepie = $this->fetchFeed($feed, true);
    $url = $wpdb->escape(htmlspecialchars_decode($simplepie->subscribe_url()));
    // code continues

I have also notified the Plugin author about this and hopefully should be fixed soon, till then you can use the above code change if you are using feed from Yahoo pipes.

Posted in WordPress | Tagged , , | 21 Comments