完全公开,PHP和Wordpress不是我的专长。
我在这里有一个插件->http://wordpress.org/extend/plugins/ustream-status/ 这将添加一个小部件,显示我的Ustream是否。电视频道正在播放或关闭。小部件本身有字段,我可以在其中输入我的频道名称,以便它可以为我检查它。
我真正想做的是,如果插件检测到我的频道正在播放,就给我应用一个类。不幸的是,我一辈子都不知道如何从插件中提取代码,并在标题中调用它。php文件(其中是)来使用它。插件本身可以在页面上根据需要多次出现在小部件中。但我对PHP/Wordpress/Plugins的了解还不够,无法用它做任何其他事情。
如果您能帮助我们了解第一步是什么,我们将不胜感激。
这是我正在使用的插件的代码。
<?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\');
?>