Currently Browsing: Home » Simple Snippet: Generate the DZone Button with PHP (Save HTTP Requests!)

Simple Snippet: Generate the DZone Button with PHP (Save HTTP Requests!)

The folks at DZone provide a great way for developers to share links in one, concentrated area. URLs which rank highly in their system (those that receive many “up” votes) often get quite a bit of traffic. As a result, many developers use the DZone button to provide their visitors a way to rate articles directly from their own website.

DZone

Do you use the DZone button on your website? If not, you should definitely consider it. Unfortunately, each one of these buttons requires the inclusion of a JavaScript file, which adds an HTTP request (and thus slows down the page). With some quick studying of the included JavaScript, it’s possible (not to mention quite easy) to generate this same button with only PHP, thus saving a request.

Here’s the function to do so:

function dzone_button( $style, $url, $title, $description = false )
{
	$html = '<iframe src="http://widgets.dzone.com/links/widgets/zoneit.html?t=' . $style;
	$html .= '&url=' . urlencode( $url ) . '&title=' . urlencode( $title );

	if( $description !== false )
		$html .= '&description=' . $description;

	if( $style == 1 )
		$html .= '" height="70" width="50" scrolling="no" frameborder="0"></iframe>';
	else
		$html .= '" height="25" width="155" scrolling="no" frameborder="0"></iframe>';

	return $html;
}

It requires the same parameters are the JavaScript file: the type of button (1 for tall, 2 for wide), URL, title, and description (optional).

To use it, just plug, chug, and echo:

echo dzone_button( 1, 'http://example.com', 'Example', 'Example description' );

If you’re using WordPress, consider the following snippet:

echo dzone_button( 1, get_permalink(), get_the_title() );

If you want a description, use get_the_excerpt:

echo dzone_button( 1, get_permalink(), get_the_title(), get_the_excerpt() );

That’s all there is to it; you’ve generated the same button without the JavaScript. The only real difference is that you’ll have an iframe directly in the HTML page rather than one generated by JavaScript. Happy DZoning!

This entry was posted on Friday, June 25th, 2010 at 22:20:54. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Simple Snippet: Generate the DZone Button with PHP (Save HTTP Requests!)”

  1. Thanks for sharing your knowledge. I achieved the same by simply copying the Code from the external JavaScript over my html/php page in WordPress.

    Unfortunately there are still 4 additional HTTP requests being made.

    http://widgets.dzone.com/links/widgets/zoneit.css
    http://widgets.dzone.com/links/scripts/prototype.js.h-1594678674.pack
    http://widgets.dzone.com/links/combined.js.h-1924966038.pack
    http://widgets.dzone.com/links/dwr/interface/LinkManager.js

    Do you have an idea how to get rid of them? I tried combining those scripts into one within a reverse engineered dZone iFrame without luck.

Leave a Reply

Want to be notified when someone replies? Subscribe to this post's comment RSS feed.
Any field marked with a * is required.