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.

Full disclosure, PHP and Wordpress are not my specialties.

I've got this plugin here -> http://wordpress.org/extend/plugins/ustream-status/ that adds a Widget that will show if my Ustream.tv channel is on the air or off the air. The widget itself has fields where I enter my channel name so that it can check it for me.

What I really want to do is apply a class to my if the plugin detects that my channel is on the air. Unfortunately I cannot for the life of me figure out how to take the code from the plugin, and call upon it in the header.php file (where the is) to make use of it. The plugin itself is able to appear within widgets as many times as you want on a page. But I don't really know enough about PHP/Wordpress/Plugins to do anything else with it.

Any help on figuring out what the first step is would be greatly appreciated.

Here is the code from the plugin I'm working with.

<?php
class wp_ustream_status extends WP_Widget {

    // ============================================================
    // Constructer
    // ============================================================
    function wp_ustream_status () {
        $widget_ops = array(
        'description' => 'Display Ustream online status'
    );
    parent::WP_Widget(false, $name = 'Ustream Status',$widget_ops);
}

    // ============================================================
    // Form
    // ============================================================
    function form( $instance ) {
        //Reading the existing data from $instance
        $instance = wp_parse_args( (array) $instance, array( 'account' => 'YokosoNews', 'online' => '', 'offline' => '') );
        $account = esc_attr( $instance['account'] );
        $online = esc_attr( $instance['online'] );
        $offline = esc_attr( $instance['offline'] );
    ?>
    <!--Form-->
    <p><label for="<?php echo $this->get_field_id('account'); ?>"><?php _e('Ustream channel name or URL:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('account'); ?>" name="<?php echo $this->get_field_name('account'); ?>" type="text" value="<?php echo $account; ?>" /></label></p>

    <p><label for="<?php echo $this->get_field_id('online'); ?>"><?php _e('Online image URL:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('online'); ?>" name="<?php echo $this->get_field_name('online'); ?>" type="text" value="<?php echo $online; ?>" /></label></p>

    <p><label for="<?php echo $this->get_field_id('offline'); ?>"><?php _e('Offline image URL:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('offline'); ?>" name="<?php echo $this->get_field_name('offline'); ?>" type="text" value="<?php echo $offline; ?>" /></label></p>
    <!--/Form-->
    <?php    }


    // ============================================================
    // Update
    // ============================================================
    function update( $new_instance, $old_instance ) {
    // Old Instance and New instance
            $instance = $old_instance;
            $instance['account'] = preg_replace("#^.*/([^/]+)/?$#",'${1}',$new_instance['account']);
            $instance['online'] = strip_tags( $new_instance['online'] );
            $instance['offline'] = strip_tags( $new_instance['offline'] );
    return $instance;    
    }

    // ============================================================
    // View
    // ============================================================
    function widget( $args, $instance ) {

        extract($args);
        $account = $instance['account'];
        $online = $instance['online'];
        $offline = $instance['offline'];

        echo $before_widget;
        if ( $account )
        echo $before_title . 'Ustream Status' . $after_title;
        // ==============================
        // Ustream Status starts here
        // ==============================
        // TRANSIENT STARTS HERE
        if ( false === ( $UstStatusArray = get_transient( 'wp_ustream_status' ) ) ) {
            $opt = stream_context_create(array(
            'http' => array( 'timeout' => 3 )
            ));
            $UstStatusSerial = file_get_contents('http://api.ustream.tv/php/channel/' . $account . '/getValueOf/status',0,$opt);
            $UstStatusArray = unserialize($UstStatusSerial);
            set_transient( 'wp_ustream_status', $UstStatusArray, 120 );
        }
        // TRANSIENT ENDS HERE
            // For DEBUG
            // echo '<!--' . $UstStatusArray . '-->';
            // Decode JSON
        switch ( $UstStatusArray['results'] )
            {
            case 'live':
                $UstStatus = 1;
            break;
            case 'offline':
                $UstStatus = 2;
            break;
            case 'error':
                $UstStatus = false;
            break;
            }
        if ($UstStatus == 1) {
        ?>
            <div align="center"><a href="http://www.ustream.tv/channel/<?php echo $account;?>" alt="<?php _e('Click here to visit the Ustream channel'); ?>" target="_blank">
            <img src="<?php echo $online; ?>" alt="<?php _e('Live now'); ?>" target="_blank" />
            </a></div>
        <?php
        // ONLINE part ends here
        }
        else if ($UstStatus == 2) {
            // If not live, including when the API does not respond
            ?>
            <div align="center"><a href="http://www.ustream.tv/channel/<?php echo $account;?>" alt="<?php _e('Click here to visit the Ustream channel'); ?>" target="_blank">
            <img src="<?php echo $offline; ?>" alt="<?php _e('Offline'); ?>" />
            </a></div>
        <?php } else {
            echo _e('Error occured. We could not retrieve the data from Ustream.');
        }
        // ==============================
        // Ustream Status ends here
        // ==============================
        echo $after_widget; 
    }
}

// ============================================================
// Registering plug-ins
// ============================================================
function wpUstreamStatusInit() {
    // Registering class name
    register_widget('wp_ustream_status');
}

// ============================================================
// execute wpUstreamStatusInit()
// ============================================================
add_action('widgets_init', 'wpUstreamStatusInit');
?>
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

The correct "WordPress" way of doing it would be to use the_widget template tag to display widgets anywhere you want.

so in you case you would something like this:

$instance['account'] = 'account name'; //Ustream channel name
$instance['online'] = 'http://www.online_image.URL';
$instance['offline'] = 'http://www.offline_image.URL';

the_widget('wp_ustream_status', $instance);
share|improve this answer
    
This worked perfectly. It even works when I don't actually have the plugin placed in any of the widgets in the Dashboard. However, I did have to change what the plugin outputted when it was called. Would there be any way to edit the plugin so that it could have two DIFFERENT outputs? One that I can simply use for a clase name. And the default one that inserted a bunch of <div>'s to create a nice looking widget? I assume I'll need to edit the original plugin again and duplicate a function somewhere with an alternate name, but I'm not sure. –  jimmykup Oct 1 '11 at 13:39
    
That is the beauty of the the_widget function, you can have as many instances as you want without adding it to any sidebar meaning you can have as many different outputs as you want. As for the bunch of <div>'s take a look at the link i posted (the codex) and look at the 3rd argument this function takes which is an array you that you can use to style the output. –  Bainternet Oct 1 '11 at 16:25
add comment

I had a similar need - wanting a widget with its settings, but not in a sidebar. If the widget developer has not made a shortcode, one can actually recode as a shortcode with an options page BUT then one is out of the upgrade path of the original widget.

So...

I wrote a plugin http://webdesign.anmari.com/1649/shortcode-any-widget/ (available at wordpress) to shortcode any widget. It creates a 'shortcode' sidebar that one can drag a widget to and set the settings.

Then one can call the widget and it will use those settings.

To add in header.php:

simply add where you want it in your header.php use wordpress function http://codex.wordpress.org/Function_Reference/do_shortcode with something like:

echo do_shortcode ('[do_widget "ustream status"]')

share|improve this answer
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.