Back to: Home
Previous Post: FREE MESHU WORKSHOP TICKETS
Next Post: CUSTOM WORDPRESS LOGIN SCREEN
HOW TO CREATE YOUR OWN TWITTER WIDGET
By admin | March 28, 2009
Topics: Blogging | No Comments »

Even if there’s a aggregation of calibre WordPress widgets available, sometimes hour meet fits what you need. In that case, you’ll hit to create your rattling possess widget. It haw good arduous at first, but it is not if you hit a lowercase planning experience.
In this tutorial, I'll exhibit you how to indite a ultimate widget to earmark your readers to care your posts on their Twitter accounts.
1 - Creating Files and the Directory
Let's move by creating a directory on your hornlike drive. On this tutorial, I'll call it Twit It. Under this directory, create a enter titled twitit.php.
Edit the twitit.php enter and adhesive the mass code, which is informing WordPress that this enter is a widget, take the plugin/widget study and edition and also provide the credits to the author:
1 2 3 4 5 6 7 8 9 |
<?php /* Plugin Name: Twit It Plugin URI: http://www.wprecipes.com Description: A ultimate "Twit It" widget for WordPress Author: Jean-baptiste Jung Version: 1 Author URI: http://www.catswhocode.com */ |
2 - Creating the Functions
Now, we hit to create the essential function, which module be titled when your widget has been additional to a widget-ready zone.
This duty module only reflexion a unification to Twitter with the place address as a parameter.
Append the cipher beneath to the twitit.php file:
1 2 3 4 5 |
function twitit() { global $wp_query; $thePostName = $wp_query->post->post_name; echo '<divcolor: #339933;" >.$thePostName.' : '.get_permalink($post->ID).'" title="Click to beam this tender to Twitter!" target="_blank">Share on Twitter</a></div>'; } |
Now, we hit to care with individual circumscribed parameters.
On the functions.php enter from their theme, users crapper delimitate whatever variables as much as $before_widget, $before_title, and so on. Theses variables are utilised to pass a bespoken image, book or html tags within widgets. For example, the $before_title uncertain earmark the users to take something to materialize before widget titles, to secure a amend thought integration.
This duty module intend every parameters from the functions.php enter in visit to attain the widget dead desegrated on the user’s theme.
1 2 3 4 5 6 7 8 |
function widget_twitit($args) { extract($args); echo $before_widget; echo $before_title; echo $after_title; twitit(); echo $after_widget; } |
We’re nearly done. We today hit to create an formatting function, and offer the widget. This saucer is rattling essential because it run the assemble of functions we meet wrote as a widget.
1 2 3 4 5 6 |
function twitit_init() { register_sidebar_widget(__('Twit It'), 'widget_twitit'); } add_action("plugins_loaded", "twitit_init"); ?> |
Save the file. You today hit a full useful "Send to Twitter" widget! You crapper establish it on your journal same some another widget.
Styling the Widget
Now that you installed the widget, you saw how useful it is, but therefore, it is not rattling visually appealing. Happily, we additional a #twitit css id on the container, which effectuation that it module be cushy to call it with CSS.
Simply adhesive the mass styles to the style.css enter from your theme. If you want, you crapper download and ingest the scenery ikon below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#twitit{ background:#fff url(images/twitit.jpg) no-repeat top left; height:120px; position:relative; width:320px; } #twitit a{ font-size:18px; font-weight:bold; position:absolute; top:30px; right:25px; } |
Complete Widget Code
Here’s the rank cipher I hit utilised for the warning widget:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php /* Plugin Name: Twit It Plugin URI: http://www.wprecipes.com Description: A ultimate "Twit It" widget for WordPress Author: Jean-baptiste Jung Version: 1 Author URI: http://www.catswhocode.com */ function twitit() { global $wp_query; $thePostName = $wp_query->post->post_name; echo '<divcolor: #339933;" >.$thePostName.' : '.get_permalink($post->ID).'" title="Click to beam this tender to Twitter!" target="_blank">Share on Twitter</a></div>'; } function widget_twitit($args) { extract($args); echo $before_widget; echo $before_title; echo $after_title; twitit(); echo $after_widget; } function twitit_init() { register_sidebar_widget(__('Twit It'), 'widget_twitit'); } add_action("plugins_loaded", "twitit_init"); ?> |
That's every for now! As you saw, creating a WordPress widget is definitely easy. The widget we created unitedly is quite basic, but I'm trusty it is a solidified foundation to intend started with widget development.
