I have a form on my wordpress page template --

<form id="pollyform" method="post">
<textarea name="pollytext" id="text"></textarea>
<input type="submit" id="savetext" name="savetext" value="save-text" />

I am trying to get the data from my textarea field and send to my script --

$(document).ready(function() { 

// prevent the form from submitting normally
   var txt = $("#text").val();

  $.ajax ({        
         data: {
          action: 'polly_pros', 
          pollytext: txt
         type: 'post',
         url: polpro.ajax_url,
         success: function(data) {
              console.log(data); //should print out the name since you sent it along

error: function() {

return false;


In my functions file I have my scripts setup to work -

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

wp_enqueue_script( 'pol', get_stylesheet_directory_uri() . '/pol.js', array(), '1.0.0', true );

    wp_localize_script( 'pol', 'polpro', array(
        'ajax_url' => admin_url( 'admin-ajax.php' )


however my php function isn't working --

add_action('wp_ajax_polly_pros', 'polly_process');

    function polly_process() {
    // use \Aws\Polly\PollyClient; // this was moved to before get_header in my template page where my form is. 
    //require '/aws-autoloader.php'; // this was moved to before get_header in my template page where my form is.

    $the_text = $_POST['pollytext'];
        if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 
    //echo $the_text;
        $voice_id = "Joanna";
        $text = $the_text;
        $rate = "medium";

    $is_download = false;

    if(isset($_REQUEST['download']) && $_REQUEST['download']==1){

    $config = array(
                    'version' => 'latest',
                    'region' => 'us-east-1',
                    'credentials' => [
                        'key' => 'keys',
                        'secret' => 'keys',


     $client = new PollyClient($config);
      $args = array(
                    'OutputFormat' => 'mp3',
                    'Text' =>  "<speak><prosody rate='$rate'>".str_replace("&","&amp;",urldecode ($text))."</prosody></speak>",
                    'TextType'     => 'ssml',
                    'VoiceId' => $voice_id

            $result = $client->synthesizeSpeech($args);          

            $resultData = $result->get('AudioStream')->getContents();

    $size = strlen($resultData); // File size
    $length = $size;           // Content length
    $start = 0;               // Start byte
    $end = $size - 1;       // End byte

    if(!$is_download) {
    file_put_contents('test.mp3', $resultData);

    } else {
    file_put_contents('test.mp3', $resultData);

When I put the php code directly in the same page as my template, it works but only the first time and then I cant update the text and resend the data, etc.

If I change my function code to something like this ---

function polly_process() {
        $the_text = $_POST['pollytext'];
            if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 
        echo $the_text;

I can see that its working and the value is echoed. My original polly_process code I want to use though is giving me this error when submitting the form --

jquery.js:8625 POST /admin-ajax.php 500 ()
send @ jquery.js:8625
ajax @ jquery.js:8161
(anonymous) @ pol.js:8
dispatch @ jquery.js:4430
r.handle @ jquery.js:4116
pol.js:20 Error

when checking the console I can see this is highlighted in jquery.js ---

// Do send the request (this may raise an exception)
                    xhr.send( options.hasContent && options.data || null );

I've checked debug.log and didnt find any relevant errors. So what am I missing?


Checking the logs again I see this --

Uncaught Error: Class 'PollyClient' not found

The reason though why I have this code --

 use \Aws\Polly\PollyClient; 
 require '/aws-autoloader.php';

in my template file before my header is because anywhere else it causes the site to crash. Specifically -

use \Aws\Polly\PollyClient;

When I have that and my polly_process function directly in my template it works, but it's not dynamic like I need with the ajax.

So how I would be able to use that code with the ajax function?

2 Answers 2


This error is telling you that there was a 500 error in the PHP of your AJAX callback:

jquery.js:8625 POST /admin-ajax.php 500 ()

This would be caused by a syntax error or other coding mistake.

In looking at your code one obvious major problem is here:

// use \Aws\Polly\PollyClient; // this was moved to before get_header in my template page where my form is. 
    //require '/aws-autoloader.php'; // this was moved to before get_header in my template page where my form is.

And here:

$client = new PollyClient($config);

For that second piece of code to work you need to load the PHP file that contains the PollyClient class. The first bit of code suggests to me that it is not being loaded. This is because you have put the require and use in the template, but the template is not oging to be loaded on an AJAX request. You need to require the file from within your theme's functions file or within the AJAX callback function. Not doing this would cause a 500 error that would explain your issue. If your look at your error log you'll likely see something about the class PollyClient not existing.

There could be other issues in the code, I'm not certain, I just saw this one and noted the issue. But essentially the problem is that you have a PHP coding issue/s in your polly_process() function.

  • ahhhh you were right about the error log, I mustve overlooked that. I will update my question
    – 730wavy
    May 9, 2018 at 20:53

Didn't read all your question, I directly stop with

var txt = $("#pollytext").val();

That must be

var txt = $("#text").val();
  • 1
    um thanks, i will edit it, that was just a mistake when I created the question but thats not the issue. Please finish reading the question.
    – 730wavy
    May 10, 2018 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.