0

I've written a script that is running once an hour on my site but it's not working. I've been trying to debug and unfortunately I haven't gotten very far. I'm getting the following error:

Uncaught Error: Call to undefined function wp_remote_post() 

Here's a condensed version of my code:

<?php
/**
 * Update User Hours
 *
 * @package WordPress
 *
 */
define( 'SHORTINIT', true );
require( '../wp-load.php' );

function update_user_hours(){
  $emails = get_all_subscriber_emails();
  $hours_data = get_updated_hours($emails);
  update_hours($hours_data);
}

function get_all_subscriber_emails(){
  global $wpdb;
  $email_list = "";
  $sql = "SELECT wp_users.user_email
          FROM wp_users INNER JOIN wp_usermeta
          ON wp_users.ID = wp_usermeta.user_id
          WHERE wp_usermeta.meta_key = 'wp_capabilities'
          AND wp_usermeta.meta_value LIKE '%subscriber%'
          ORDER BY wp_users.user_email";

  $results = $wpdb->get_results($sql);
    foreach($results as $result){
    $email_list = $email_list.$result->user_email.",";
    }
  return $email_list;
}

function get_updated_hours($emails){

  $url = 'https://demo/api/hours?key=123';
   //CRASHES HERE 
  $data = wp_remote_post($url, array(
      "body" => array(
          "emails"    => $emails
        )
    ));
  $result = json_decode($data, true);
  return $result["data"];
}

function update_hours($results){
  global $wpdb;
  //do stuff
  }
}

update_user_hours();
?>

I don't know why wp_remote_post isn't getting included. Is there a different way I should be calling my API?

Thank you!

  • 2
    You shouldn't put your code in a separate PHP file and then load WordPress inside it. Instead, you can create a plugin and use the default WP cron scheduler. – Jack Johansson Oct 30 '18 at 6:54

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.