0

I'm working on a plugin. I have a somewhat long running php process and I am trying to show a progress bar by polling a session variable with ajax and displaying on the front end. I previously had it setup with a GET request going to a separate file called "progress.php" that just echoed out the session variable. I'm trying to convert it to the WordPress way with:

'ajax_url' => admin_url('admin-ajax.php'),
add_action('wp_ajax_progress_bar', 'progress_bar');

It was working when I had it in a separate file. Will it still work as a function within my plugin?

//I have this php function in my plugin
function progress_bar()
{       
    //I previously had everything in this function in a separate file called 
    //"progress.php", but I don't think WordPress likes me to do it that way.
    session_start();
    if (!isset($_SESSION['progressbar'])) {
        $_SESSION['progressbar'] = 0;
    }
    echo ($_SESSION['progressbar']);

    if ($_SESSION['progressbar'] == 100) {
        $_SESSION['progressbar'] = 0;
    }
    session_write_close();
}



//I have this in my js
function get_progress() {
  $.ajax({
    type: 'GET',
    url: plugin_name.ajax_url,
    data: {
      'action': 'progress_bar'
    }
  }).done(function (response) {
    if (progressbar > 1) {
      $('.progress-bar span').css({
        width: response + '%'
      });
      $('.progress-percent-done').text(response + '%');
    }
    if (progressbar < 100) {
      setTimeout(function () {
        get_progress();
      }, 500);
    }
  });
}
  • I have verified with php xdebug that the session variable is being set correctly. – egauvin Aug 26 '18 at 2:36

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.