Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i need a help, i have a theme, where in i have a twitter widget, i just want to separate it, and keep it in different file, as if it is a plugin, here are is the complete code, what i have to do?

this code was in file admin-functions.php

/*-----------------------------------------------------------------------------------*/
/* Twitter's Blogger.js output for Twitter widgets */
/*-----------------------------------------------------------------------------------*/

function woo_twitter_script($unique_id,$username,$limit) {
?>
<script type="text/javascript">
<!--//--><![CDATA[//><!--

    function twitterCallback2(twitters) {
      var statusHTML = [];
      for (var i=0; i<twitters.length; i++){
        var username = twitters[i].user.screen_name;
        var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
          return '<a href="'+url+'">'+url+'</a>';
        }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
          return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
        });
        statusHTML.push('<li><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>');
      }
      document.getElementById('twitter_update_list_<?php echo $unique_id; ?>').innerHTML = statusHTML.join('');
    }

    function relative_time(time_value) {
      var values = time_value.split(" ");
      time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
      var parsed_date = Date.parse(time_value);
      var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
      var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
      delta = delta + (relative_to.getTimezoneOffset() * 60);

      if (delta < 60) {
        return 'less than a minute ago';
      } else if(delta < 120) {
        return 'about a minute ago';
      } else if(delta < (60*60)) {
        return (parseInt(delta / 60)).toString() + ' minutes ago';
      } else if(delta < (120*60)) {
        return 'about an hour ago';
      } else if(delta < (24*60*60)) {
        return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
      } else if(delta < (48*60*60)) {
        return '1 day ago';
      } else {
        return (parseInt(delta / 86400)).toString() + ' days ago';
      }
    }
//-->!]]>
</script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/<?php echo $username; ?>.json?callback=twitterCallback2&amp;count=<?php echo $limit; ?>"></script>

`

and this second code i found in theme-widget.php

`

/*---------------------------------------------------------------------------------*/
/* Twitter widget */
/*---------------------------------------------------------------------------------*/
class Woo_Twitter extends WP_Widget {

   function Woo_Twitter() {
       $widget_ops = array('description' => 'Add your Twitter feed to your sidebar with this widget.' );
       parent::WP_Widget(false, __('Woo - Twitter Stream', 'woothemes'),$widget_ops);      
   }

   function widget($args, $instance) {  
    extract( $args );
    $title = $instance['title'];
    $limit = $instance['limit']; if (!$limit) $limit = 5;
    $username = $instance['username'];
    $unique_id = $args['widget_id'];
    ?>
        <?php echo $before_widget; ?>
        <?php if ($title) echo $before_title . $title . $after_title; ?>
        <ul id="twitter_update_list_<?php echo $unique_id; ?>"><li></li></ul>   
        <?php echo woo_twitter_script($unique_id,$username,$limit); //Javascript output function ?>  
        <?php echo $after_widget; ?>


    <?php
   }

   function update($new_instance, $old_instance) {                
       return $new_instance;
   }

   function form($instance) {        

       $title = esc_attr($instance['title']);
       $limit = esc_attr($instance['limit']);
       $username = esc_attr($instance['username']);
       ?>
       <p>
           <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','woothemes'); ?></label>
           <input type="text" name="<?php echo $this->get_field_name('title'); ?>"  value="<?php echo $title; ?>" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" />
       </p>
       <p>
           <label for="<?php echo $this->get_field_id('username'); ?>"><?php _e('Username:','woothemes'); ?></label>
           <input type="text" name="<?php echo $this->get_field_name('username'); ?>"  value="<?php echo $username; ?>" class="widefat" id="<?php echo $this->get_field_id('username'); ?>" />
       </p>
       <p>
           <label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e('Limit:','woothemes'); ?></label>
           <input type="text" name="<?php echo $this->get_field_name('limit'); ?>"  value="<?php echo $limit; ?>" class="" size="3" id="<?php echo $this->get_field_id('limit'); ?>" />

       </p>
      <?php
   }

} 
register_widget('Woo_Twitter');

i just want this code to be a different file, just like a plugin, so that i can use this in my another theme also, can anyone help me here???

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Other than order of execution it doesn't really matter where code is run.

In general case you can move code to Functions File (functions.php) of theme or create simple plugin and it will still work.

In this specific case the code seems to be part of WooFramework so you will need to additionally check so that it isn't declared twice (causing error), for example by using function_exists().

share|improve this answer
    
ok i tried the following thing i am not good in php so i copied entire class Woo_Twitter extends WP_Widget { file to another file named as woo-twitter.php and the second file copied to woo-twit.php but when i tried to activate the plugin i get the following error "Plugin could not be activated because it triggered a fatal error. Fatal error: Cannot redeclare class Woo_Twitter in C:\wamp\www\wordpress\wp-content\plugins\woo-twitter\woo-twitter.php on line 54" can any one help me in this? –  ntechi Dec 2 '10 at 11:56
    
As above - you cannot have two declarations of same name at the same time. Path of least resistance would be changing name of your copied declarations. –  Rarst Dec 2 '10 at 12:53
    
so what i have to do? an you explain me –  ntechi Dec 2 '10 at 14:15
2  
Change functions and class names, update code accordingly so it works with renamed stuff. This is really more of PHP basics rather than WP issue. –  Rarst Dec 2 '10 at 14:21
    
i am setting this error if i am changing the functions name, "Fatal error: Call to a member function register() on a non-object in C:\wamp\www\wordpress\wp-includes\widgets . php on line 431" i changed the name of function and class –  ntechi Dec 3 '10 at 4:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.