myCred是WordPress的插件,可让您轻松创建积分余额并设置用户赚取积分的步骤。
以下是他们的开源插件的链接:
Github:https ://github.com/wp-plugins/mycred
myCred 网站:https ://mycred.me/
WordPress:https ://wordpress.org/plugins/mycred/
准备好!在这里:我找不到解决方案如何将我自己$amount
的添加$current_balance
到$new_balance
?
我已经尝试过这样的事情,但没有运气:
add_filter('filter_update_users_balance', 'add_my_own_amount');
function add_my_own_amount($amount){
return 10;
}
让我们看一下mycred-functions.php文件。
首先,它获得了这样的用户余额:(我不确定是否有必要阅读以解决我的问题,我建议现在跳过它:))
/**
* Get users balance
* Returns the users balance unformated.
*
* @param $user_id (int), required user id
* @param $type (string), optional cred type to check for
* @returns zero if user id is not set or if no creds were found, else returns amount
* @since 0.1
* @version 1.4.1
*/
public function get_users_balance( $user_id = NULL, $type = NULL ) {
if ( $user_id === NULL ) return $this->zero();
// Type
$point_types = mycred_get_types();
if ( $type === NULL || ! array_key_exists( $type, $point_types ) ) $type = $this->get_cred_id();
$balance = mycred_get_user_meta( $user_id, $type, '', true );
if ( $balance == '' ) $balance = $this->zero();
// Let others play
$balance = apply_filters( 'mycred_get_users_cred', $balance, $this, $user_id, $type );
return $this->number( $balance );
}
// Replaces
public function get_users_cred( $user_id = NULL, $type = NULL ) {
return $this->get_users_balance( $user_id, $type );
}
之后它会像这样更新用户余额:
在这里,我们有这行代码$new_balance = $current_balance+$amount;
,向我们展示了插件如何返回用户的新余额。在代码的最底部,它是return $this->number( $new_balance );
这样做的,我们可以稍后根据价值设置用户余额。$new_balance
/**
* Update users balance
* Returns the updated balance of the given user.
*
* @param $user_id (int), required user id
* @param $amount (int|float), amount to add/deduct from users balance. This value must be pre-formated.
* @param $type (string), optional point type key to adjust instead of the current one.
* @returns the new balance.
* @since 0.1
* @version 1.4.2
*/
public function update_users_balance( $user_id = NULL, $amount = NULL, $type = NULL ) {
// Minimum Requirements: User id and amount can not be null
if ( $user_id === NULL || $amount === NULL ) return $amount;
// Type
$point_types = mycred_get_types();
if ( $type === NULL || ! array_key_exists( $type, $point_types ) ) $type = $this->get_cred_id();
// Enforce max
if ( $this->max() > $this->zero() && $amount > $this->max() ) {
$_amount = $amount;
$amount = $this->number( $this->max() );
do_action( 'mycred_max_enforced', $user_id, $_amount, $this->max() );
}
// Adjust creds
$current_balance = $this->get_users_balance( $user_id, $type );
$new_balance = $current_balance+$amount;
// Update creds
mycred_update_user_meta( $user_id, $type, '', $new_balance );
// Update total creds
$total = mycred_query_users_total( $user_id, $type );
mycred_update_user_meta( $user_id, $type, '_total', $total );
// Clear caches
mycred_delete_option( 'mycred-cache-total-' . $type );
// Let others play
do_action( 'mycred_update_user_balance', $user_id, $current_balance, $amount, $type );
// Return the new balance
return $this->number( $new_balance );
}
然后它像这样设置用户余额:
/**
* Set users balance
* Changes a users balance to the amount given.
*
* @param $user_id (int), required user id
* @param $new_balance (int|float), amount to add/deduct from users balance. This value must be pre-formated.
* @returns (bool) true on success or false on fail.
* @since 1.7.3
* @version 1.0.1
*/
public function set_users_balance( $user_id = NULL, $new_balance = NULL ) {
// Minimum Requirements: User id and amount can not be null
if ( $user_id === NULL || $new_balance === NULL ) return false;
$type = $this->get_cred_id();
$new_balance = $this->number( $new_balance );
$old_balance = $this->get_users_balance( $user_id, $type );
// Update balance
mycred_update_user_meta( $user_id, $type, '', $new_balance );
// Clear caches
mycred_delete_option( 'mycred-cache-total-' . $type );
// Let others play
do_action( 'mycred_set_user_balance', $user_id, $new_balance, $old_balance, $this );
return true;
}
最后在mycred-balances.php文件中,我们有:
// Add to the balance
if ( $method == 'add' )
$mycred->update_users_balance( $user_id, $balance );
// Change the balance
else
$mycred->set_users_balance( $user_id, $balance );
我花了好几个小时才找到解决方案,我想我找不到解决方案,所以如果你能帮我解决这个问题,我将不胜感激。
编辑 :
我想$amount
用我的 javascript 变量的值来增加 myCred 的值!我想将 Wordpress 插件的变量(我认为是$amount
)分配给 javascript 变量的值,以便将交互式电子学习课程连接到 WordPress myCred 插件,这将是惊人的游戏化体验……!
让我再解释一下:
使用 jQuerypost()
方法,我可以将要处理的数据(例如数字 javascript 变量)提交到服务器中的指定 PHP 文件。让我们看看我的代码以了解我想用它做什么:
这是我的 JavaScript 代码:
var speechResult= 10;
$.ajax({
url:"https://...Example.php",
method: "post",
data: {'speechResult': speechResult},
success: function(res) {
console.log(res)
}
});
这是我的Example.php中的代码。它只是在控制台中显示SpeechResult值(来自我的 javascript 代码的值!):
<?php
print($_POST['speechResult'])
?>
如您所见,我有一个名为SpeechResult的 JavaScript 变量,还有一个名为“Example.php”的PHP文件。
通过使用上面的代码,我可以将SpeechResult值传递给Example.php或服务器上的任何其他PHP文件(但不是 myCred 插件的 PHP 文件,因为我不知道如何使用钩子、过滤器、操作等)。
如果我将 **speechResult值传递给$amount
myCred** 中的变量,那么我将根据电子学习 html5 课程中发生的情况来控制向用户提供的奖励积分...
我想传递speechResult值来分配 myCred$amount
值...以增加或减少使用的总余额点。