1

I'm developing a site which will have various Unity games hosted on it, and I want to be able to record the scores the users get in their profiles. I've seen various plugins for adding custom fields, but I'm not sure how to modify the fields from scripting, and how to prevent the user from modifying the data manually. I can run Javascript methods on the site from the Unity app. Any suggestions or insight that you can offer would be greatly appreciated. Thanks!

  • Why do you mean by scripting? What have you tried? Can you post some code? – Ciprian Aug 3 '18 at 8:26
  • I'm afraid I'm not sure where to start really so I haven't got any code so far. But I was wondering if there's a Javascript or PHP method I can call from within the Unity app that can modify the profile data of the currently logged in user. Are there hooks for modifying that sort of thing in code rather than the user manually entering data into the profile fields? – JPG Aug 3 '18 at 12:11
  • How do you know which user is logged in? You should allow for a custom field in your unity profile which will be linked to the WordPress profile. You could then run a JavaScript function to call an AJAX script to execute a PHP file inside WordPress to update a specific profile. – Ciprian Aug 3 '18 at 12:31
0

You should allow for a custom field in your unity profile which will be linked to the WordPress profile. You could then run a JavaScript function to call an AJAX script to execute a PHP file inside WordPress to update a specific profile.

Here's some pseudo-code:

JavaScript

function updateProfile(parameters) {
    var xhttp = new XMLHttpRequest();

    xhttp.open('GET', 'https://www.example.com/receiver.php' + parameters, true);
    xhttp.send();
}

// Usage
updateProfile('?username=whatever&points=4');

PHP (receiver.php):

<?php
global $wpdb;

$username = $_POST['username'];
$points = (int) $_POST['points'];

$wpdb->query("UPDATE some_table SET points = points + $points WHERE username = $username");

Note you'll need to sanitise your input and you'll neet to link the pints to the username. You can add add_user_meta() function for this.

Note that this is a basic approach. The receiver.php file can be part of a plugin, so you'll actually call example.com/wp-content/plugins/unity-plugin/receiver.php in your AJAX call.

  • Spectacular! That is just what I needed, thank you so much! – JPG Aug 3 '18 at 12:38
  • I've been having some difficulty getting this to work. I always get an "Internal Server Error" in the console at the xhttp.send(); line. However I don't think the error is in the php file since it executes correctly when called from within a custom plugin I've written. Any clue why I might be getting this error? – JPG Aug 9 '18 at 11:30
  • Add error reporting and you will see the error. It's most likely a 500 error. – Ciprian Aug 9 '18 at 12:50
  • Yeah it is a 500 error. I've discovered the reason is because I'm using a wordpress function that requires the core to be loaded, so I think I'll need to use admin-ajax.php for that to work correctly. Still trying to figure that out but I should be good, hopefully :) – JPG Aug 9 '18 at 12:51
  • Or, if this project is for you only, include wp-load.php in your PHP file - include '../../../wp-load.php'; - make sure the path is correct. – Ciprian Aug 9 '18 at 14:01

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.