I have a site with about 100 registered users. Some of the people will be receiving digital scales. The scales send their weight data to a 3rd party server that then sends a JSON Payload as an HTTP Post to a URL endpoint (my server). The message can include Basic authentication. Can anyone recommend the best approach to get the data into the WP database. Initially I just want to add the weight to their profile with user_metadata. Eventually I will process the dat more. I'm good with Wordpress but can't figure out how to jump from http post into wp or even just mysql.


Your exact code would depend on how the HTTP notification is sent to your server and on the structure of the JSON, but the first building block is to listen for the incoming post request.

There are lots of WP hooks that would be suitable. One is init, e.g.:

add_action('init', function() {

     $user = 'the_correct_basic_auth_username';
     $password = 'the_correct_basic_auth_password';

    $has_supplied_credentials = !(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW']));

    $is_not_authenticated = (
        !$has_supplied_credentials ||
        $_SERVER['PHP_AUTH_USER'] != $AUTH_USER ||
        $_SERVER['PHP_AUTH_PW']   != $AUTH_PASS
    if ($is_not_authenticated) {

    /* the rest will depend on how the data is sent and the structure of the JSON. 
    Once you have the data in the structure you want it, you can use the update_user_meta() function to add usermeta. Or, depending on what you're saving, use $wpdb or a wrapper function may suite (such as wp_insert_post()) */


I took the basic auth checking code from here - https://gist.github.com/rchrd2/c94eb4701da57ce9a0ad4d2b00794131

| improve this answer | |
  • Thanks! would I need to preface this with: require_once('wp-load.php'); $post = $_POST; – Johnnyboy Gomez May 4 at 12:53
  • No, just put the code inside functions.php (if in a theme) or within a plugin's file (if you're making a plugin) and you won't need to require wp-load.php because you'd be using a hook that is fired after all that stuff is loaded. You don't need to set the $post variable/object, it's not to do with $_POST. – Dan. May 4 at 13:21
  • The scale can send the post request to any URL I specify. So I was planning to make a regular wp page and put the script in it. The username and password are hardcoded in this function? If this hook is in functions.php how is it tied to the incoming post request. Isn't it kind of universal? When init fires how does it know to refer to the correct URL?What if I had another device sending requests to a different URL? Thank you – Johnnyboy Gomez May 4 at 15:25
  • @JohnnyboyGomez yes, making a new page and putting the code in the relevant template's file is a way you could go about it. But I personally prefer my way, seems less messy than creating a page, as pages are meant for things that website users would see and so you may have to take care of other things - e.g. if you have an SEO plugin genrating a sitemap, it'll include that page in the sitemap and so you'd have to remove it from the sitemap. That's one example of a side effect, I think there'll be more. – Dan. May 4 at 18:16
  • @JohnnyboyGomez yes, as it stands in my answer, the hooked function will be run every for every request. My answer contains a minimal approach. You should add some more logic at the start of the function. E.g. check for a URL parameter (and add that URL parameter to the URL the third-party send data to). Or check for something else that the third-party might include in their requests, e.g. a custom HTTP header. – Dan. May 4 at 18:19

Your Answer

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

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