我正在编写一个插件,希望在10秒内刷新小部件。如何做到这一点?
我想在间隔中调用小部件函数,以便刷新小部件。
以下是插件的代码
<?php
// Creating the widget
class rtk_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'rtk_widget',
// Widget name will appear in UI
__('Realtime Quote', 'rtk_widget_domain'),
// Widget description
array( 'description' => __( 'Displays Realtime share quote', 'rtk_widget_domain' ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
$stk_options = get_option('stk_settings');
//echo "ssss";
if(is_array($stk_options)&&$stk_options['enable']==1)
{
$symbols= explode(" ", $stk_options['stock-names']);
foreach ($symbols as $symbol) {
$quote = fetch('GET', $symbol);
//var_dump($quote[0]);
echo '<span class="realtime-company">'.$quote[0]->t.' </span>'.'<span class="realtime-quote">'.$quote[0]->l.'</span>';
}
}
// This is where you run the code and display the output
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'rtk_widget_domain' );
}
// Widget admin form
?>
<p>
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // Class rtk_widget ends here
// Register and load the widget
function rtk_load_widget() {
register_widget( 'rtk_widget' );
}
add_action( 'widgets_init', 'rtk_load_widget' );
?>
刷新php页面:
<?php
$url=$_SERVER['REQUEST_URI'];
header("Refresh: 5; URL='"" . $url . "'""); // redirect in 5 seconds
?>
刷新wordpress链接中的插件
您不能从php重新加载页面的一部分,您必须重新加载整个页面。但我认为这不是你想要的。您应该使用ajax每隔5秒重新加载一次脚本。