Forums

add variables from a widget to wp_footer (6 posts)

  1. twothirty
    Member
    Posted 2 years ago #

    here's my scenario - i've got a widget that users enter their twitter name and the number of updates they want to display, then it puts the widget on the sidebar. however, 2 javascript files are required, and i'd like those to load in to the wp_footer, not into the sidebar (since they are from twitter, they're slow, and it stalls the rest of the sidebar + footer in loading).

    so my question is there: since the 2 javascript files require variables collected from the widget to work, how do i inject variables from an active widget into the footer?

    here's my widget:

    class Tweets_Show extends WP_Widget {
    	function Tweets_Show() {
    		$widget_ops = array('classname' => 'tweets_show', 'description' => 'Shows latest 3 tweets, enter your username.' );
    		$this->WP_Widget('Tweets_Show', 'Show Tweets', $widget_ops);
    	}
    	function form($instance) {
    	$tw_username = esc_attr($instance['tw_username']);
    	$tw_updates = esc_attr($instance['tw_updates']); ?>
    		<p><label for="<?php echo $this->get_field_id('tw_username'); ?>"><?php _e('twitter username:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('tw_username'); ?>" name="<?php echo $this->get_field_name('tw_username'); ?>" type="text" value="<?php echo $tw_username; ?>" /></label></p>
    		<p><label for="<?php echo $this->get_field_id('tw_updates'); ?>"><?php _e('number of updates to display:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('tw_updates'); ?>" name="<?php echo $this->get_field_name('tw_updates'); ?>" type="text" value="<?php echo $tw_updates; ?>" /></label></p>
    <?php }
    
    function update($new_instance, $old_instance) {
    		return $new_instance;
    	}
    
    function widget($args, $instance) {
    	extract($args, EXTR_SKIP);
    	$tw_username = $instance['tw_username'];
    	$tw_updates = $instance['tw_updates'];
    	echo $before_widget; ?>
    	<h4>Tweets from <a href="http://twitter.com/<?php echo $tw_username; ?>">@<?php echo $tw_username; ?></a></h4>
    	<ul id="twitter_update_list">
    		<li>Fetching latest tweet</li>
    	</ul>
    	<p class="button"><a href="http://twitter.com/<?php echo $tw_username; ?>">Follow on Twitter</a></p>
    
    	<script src="/wp-content/themes/NAME/library/twitter.js" type="text/javascript" charset="utf-8"></script>
    	<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/<?php echo $tw_username; ?>.json?callback=twitterCallback2&count=<?php echo $tw_updates; ?>"></script>
    <?php echo $after_widget;
    } } register_widget('Tweets_Show');

    what i'd like to have happen is the two lines that call the javascript:

    <script src="/wp-content/themes/NAME/library/twitter.js" type="text/javascript" charset="utf-8"></script>
    	<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/<?php echo $tw_username; ?>.json?callback=twitterCallback2&count=<?php echo $tw_updates; ?>"></script>

    could appear in wp_footer, (but it needs to pull through $tw_username and $tw_updates.

    how can i do this? i know it should be possible, it's just beyond what i know in php.

  2. Ishimwe
    Member
    Posted 2 years ago #

    Could you provide urls where this is taking place...

    to inject anything in the wp_footer you need to hook in there. So you would need a function in the first place.

    Something like add_action ( 'wp_footer', 'the_function_for_JS' );. Provide a link, maybe it will make sense when I see the full picture coz now I honestly don't.

  3. twothirty
    Member
    Posted 2 years ago #

    an example would be:
    http://9og.jarviswp.com/

    i want those two lines of javascript to appear in the footer, not in the widget code. i know add_action ( 'wp_footer', 'the_function_for_JS' ); is what i need, and i assume i can call that from win the widget. my issue is that how do i get those 2 variables from the widget for $tw_username and $tw_updates into the_function_for_JS?

    hope that helps explain a little better.

  4. silverks
    Member
    Posted 1 year ago #

    have this issue been resolved? I'm having the same problem..

  5. johnsamwallace
    Member
    Posted 1 year ago #

    I'm also having this problem. Used:

    function twitter_java() { ?>
    <script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
    <script type="text/javascript" src="http://twitter.com/statuses/user_timeline/<?php echo $twitterID; ?>.json?callback=twitterCallback2&count=4"></script>
    <?php
    }
    add_action('wp_footer', 'twitter_java');

    It works on my local server but not on any remote servers. Funny, when I put the java in the body, next to the call, it works on the remotes but loads slooowwwly. Any help would be appreciated.

  6. jdlintz
    Member
    Posted 1 year ago #

    Got the same question. Would love to know how to make it happen.

Topic Closed

This topic has been closed to new replies.

About this Topic