Click here to Skip to main content
14,031,365 members
Rate this:
 
Please Sign up or sign in to vote.
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 ) {
    
    	// Use current user's ID if none specified
    	if ( ! $user_id )
    		$user_id = wp_get_current_user()->ID;
    
    	// Return our user's points as an integer (sanely falls back to 0 if empty)
    	return absint( get_user_meta( $user_id, '_badgeos_points', true ) );
    }
    
    /**
     * Posts a log entry when a user earns points
     *
     * @since  1.0.0
     * @param  integer $user_id        The given user's ID
     * @param  integer $new_points     The new points the user is being awarded
     * @param  integer $admin_id       If being awarded by an admin, the admin's user ID
     * @param  integer $achievement_id The achievement that generated the points, if applicable
     * @return integer                 The user's updated point total
     */
    function badgeos_update_users_points( $user_id = 0, $new_points = 0, $admin_id = 0, $achievement_id = null ) {
    
    	// Use current user's ID if none specified
    	if ( ! $user_id )
    		$user_id = get_current_user_id();
    
    	// Grab the user's current points
    	$current_points = badgeos_get_users_points( $user_id );
    
    	// If we're getting an admin ID, $new_points is actually the final total, so subtract the current points
    	if ( $admin_id ) {
    		$new_points = $new_points - $current_points;
    	}
    
    	// Update our user's total
    	$total_points = max( $current_points + $new_points, 0 );
    	update_user_meta( $user_id, '_badgeos_points', $total_points );
    
    	// Available action for triggering other processes
    	do_action( 'badgeos_update_users_points', $user_id, $new_points, $total_points, $admin_id, $achievement_id );
    
    	// Maybe award some points-based badges
    	foreach ( badgeos_get_points_based_achievements() as $achievement ) {
    		badgeos_maybe_award_achievement_to_user( $achievement->ID, $user_id );
    	}
    
    	return $total_points;
    }
    
    /**
     * Log a user's updated points
     *
     * @since 1.2.0
     * @param integer $user_id        The user ID
     * @param integer $new_points     Points added to the user's total
     * @param integer $total_points   The user's updated total points
     * @param integer $admin_id       An admin ID (if admin-awarded)
     * @param integer $achievement_id The associated achievent ID
     */
    function badgeos_log_users_points( $user_id, $new_points, $total_points, $admin_id, $achievement_id ) {
    
    	// Setup our user objects
    	$user  = get_userdata( $user_id );
    	$admin = get_userdata( $admin_id );
    
    	// Alter our log message if this was an admin action
    	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 ) );
    
    	// Create a log entry
    	$log_entry_id = badgeos_post_log_entry( $achievement_id, $user_id, 'points', $log_message );
    
    	// Add relevant meta to our log entry
    	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 );
    
    /**
     * Award new points to a user based on logged activites and earned badges
     *
     * @since  1.0.0
     * @param  integer $user_id        The given user's ID
     * @param  integer $achievement_id The given achievement's post ID
     * @return integer                 The user's updated points total
     */
    function badgeos_award_user_points( $user_id = 0, $achievement_id = 0 ) {
    
    	// Grab our points from the provided post
    	$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...
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190419.4 | Last Updated 1 Dec 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100