I spend hours to find a solution, have no hair left to pull out of my head but still...
I know there should be a way to achieve this but, unfortunately, I’m a newbie to PHP and
I really need your kind assistance here.
There are some helpful questions on StackOverflow (links below), it's a good start but not enough I think.
https://stackoverflow.com/questions/23459826/incrementing-a-php-variable-with-click-of-wordpress-post-title
https://stackoverflow.com/questions/48481689/call-wordpress-plugin-variable-in-theme-file
BadgeOS is a plugin to WordPress that allows you to easily create badges and
Points and set up the steps and achievements users take to earn them.
Here are the links to their open source plugin:
Wordpress:
https://wordpress.org/plugins/badgeos/
Github:
https://github.com/opencredit/badgeos
I want to increment
BadgeOS User’s Points value with the value of my custom variable! Let me explain this a bit more:
Using jQuery
post()
method I’m able to Submit data (for example a numeric javascript variable ) to be processed to a specified
PHP file in the server. let’s have a look at my code to understand what I want to do with it :
Here is my JavaScript code:
var speechResult= 10;
$.ajax({
url:"https://...Example.php",
method: "post",
data: {'speechResult': speechResult},
success: function(res) {
console.log(res)
}
});
And here is the code in my
Example.php. it just shows
speechResult value in the console (The value that comes from my javascript code!):
<?php
print($_POST['speechResult'])
?>
as you can see I have a JavaScript variable named
speechResult, and I have a
PHP file named
Example.php .By using the code above I can pass
speechResult value to
Example.php or any other PHP file on the server (like any PHP files of
BadgeOS plugin, on the paper of course :))
I want to increment
BadgeOS User’s Points by the value of
speechResult variable, but before that let's get deeper :
There are variables in BadgeOS PHP files (You can find all of it's PHP included files on GitHub [Here][1]) that handle User Points like:
$user_points
and
$total_points
and
$current_points
.
for example, we have a piece of code from
points-functions.php file here:
function badgeos_get_users_points( $user_id = 0 ) {
if ( ! $user_id )
$user_id = wp_get_current_user()->ID;
return absint( get_user_meta( $user_id, '_badgeos_points', true ) );
}
function badgeos_update_users_points( $user_id = 0, $new_points = 0, $admin_id = 0, $achievement_id = null ) {
if ( ! $user_id )
$user_id = get_current_user_id();
$current_points = badgeos_get_users_points( $user_id );
if ( $admin_id ) {
$new_points = $new_points - $current_points;
}
$total_points = max( $current_points + $new_points, 0 );
update_user_meta( $user_id, '_badgeos_points', $total_points );
do_action( 'badgeos_update_users_points', $user_id, $new_points, $total_points, $admin_id, $achievement_id );
foreach ( badgeos_get_points_based_achievements() as $achievement ) {
badgeos_maybe_award_achievement_to_user( $achievement->ID, $user_id );
}
return $total_points;
}
function badgeos_log_users_points( $user_id, $new_points, $total_points, $admin_id, $achievement_id ) {
$user = get_userdata( $user_id );
$admin = get_userdata( $admin_id );
if ( $admin_id )
$log_message = sprintf( __( '%1$s awarded %2$s %3$s points for a new total of %4$s points', 'badgeos' ), $admin->user_login, $user->user_login, number_format( $new_points ), number_format( $total_points ) );
else
$log_message = sprintf( __( '%1$s earned %2$s points for a new total of %3$s points', 'badgeos' ), $user->user_login, number_format( $new_points ), number_format( $total_points ) );
$log_entry_id = badgeos_post_log_entry( $achievement_id, $user_id, 'points', $log_message );
update_post_meta( $log_entry_id, '_badgeos_awarded_points', $new_points );
update_post_meta( $log_entry_id, '_badgeos_total_user_points', $total_points );
if ( $admin_id )
update_post_meta( $log_entry_id, '_badgeos_admin_awarded', $admin_id );
}
add_action( 'badgeos_update_users_points', 'badgeos_log_users_points', 10, 5 );
function badgeos_award_user_points( $user_id = 0, $achievement_id = 0 ) {
$points = absint( get_post_meta( $achievement_id, '_badgeos_points', true ) );
if ( ! empty( $points ) )
return badgeos_update_users_points( $user_id, $points, false, $achievement_id );
}
add_action( 'badgeos_award_achievement', 'badgeos_award_user_points', 999, 2 );
I want to increment
BadgeOS User’s Points by the value of
speechReslt variable (I want to increment BadgeOS variable using my own external javascript variable).
Any Any Any suggestions or ideas are extremely appreciated…
[1]: https://github.com/opencredit/badgeos/tree/master/includes
What I have tried:
I've used POST method to assign a variable as described with no luck...