I am using the following plugin - widget No Cache AJAX Widgets to display on the sidebar a feed of rss news that is created dynamically and bypassing the cache function.

In updating the site we wish to display this feed in the main area of the home page (index.php) hardcoded in the php. Calling the shortcode on its own it works but the results are cached and are not refreshed. So I would like to add this widget functionality in the php file but I do know how

Existing php code <?php echo do_shortcode("[wp-rss-aggregator]"); ?> code of widget that I need the functionality

if (!class_exists('AJAX_Text_Widget')) {

// register widget
add_action('widgets_init', '_ajax_text_widget');
function _ajax_text_widget(){

add_action( 'wp_enqueue_scripts', 'mg_add_ajax_script' );
function mg_add_ajax_script() {

    wp_enqueue_script( 'mg-ajax-script', plugins_url( '/js/mg_ajax.js', __FILE__ ), array('jquery') );

    wp_localize_script( 'mg-ajax-script', 'mg_ajax', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

add_action( 'wp_ajax_mg_ajax_text', 'mg_ajax_text' );
add_action( 'wp_ajax_nopriv_mg_ajax_text', 'mg_ajax_text' );
function mg_ajax_text(){
    $data = $_POST['data'];
    $return = array();
    if( is_array( $data ) ){
        foreach ( $data as $key => $text ) {
            $return[$key] = do_shortcode( base64_decode( $text ) );
    echo json_encode( $return );

// extend wp widget class
class AJAX_Text_Widget extends WP_Widget {

    public function __construct() {
        // instantiate the parent object
            'ajax_text', // Base ID
            'AJAX Widget', // Name
            array('description' => 'Any tex/shortcode which you want to load using ajax')

    public function widget($args, $instance) {

        if (!$instance['title'] && !$instance['text']) return;

        $title = apply_filters('widget_title', $instance['title']);
        $text = $instance['text'];

        $text = base64_encode( $text );

        echo $before_widget; ?>

            <?php if ($title) echo $before_title . $title . $after_title; ?>
            <?php if ($text) : ?><div class="mg_ajax_widget" data-text="<?php echo $text; ?>"><img src="<?php echo plugins_url( '/img/loading.GIF', __FILE__ ); ?>" alt="loading" /></div><?php endif; ?>

        <?php echo $after_widget;

    public function form($instance) {
        $instance = wp_parse_args((array)$instance, array('title' => '', 'text' => ''));
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'ajaxwp'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($instance['title']); ?>">
            <textarea rows="10" class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $instance['text']; ?></textarea>


    public function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['text'] = $new_instance['text'];
        return $instance;

Any suggestion how can I do it ? Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.