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 );
我花了数小时的时间来寻找解决方案,我想找不到解决方案,因此,如果您能帮助我解决这个问题,我将不胜感激。
编辑:
我想用我的javascript变量的值增加myCred的
$amount
值!我想将WordPress插件的变量(我认为是$amount
)分配给javascript变量的值,以便将交互式电子学习课程连接到WordPress myCred插件,这将是惊人的游戏体验……!让我再解释一下:
使用jQuery
post()
方法,我可以提交要处理到服务器中指定PHP文件的数据(例如,数字javascript变量)。让我们看一下我的代码,以了解我要怎么做:这是我的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值传递给myCred **中的
$amount
变量,那么我将根据电子倾斜html5课程中发生的情况来控制向用户回传点的操作...我想传递SpeechResult值来分配myCred
$amount
值...,以便增加或减少使用的总平衡点。 最佳答案
我不确定要实现什么目标。也许每个余额增加10?
您可以在代码中的任何地方使用add_filter
。因此,例如:
add_filter('mycred_get_users_cred', function($balance, $this, $user_id, $type) {
return $balance + 10;
});
不确定,是否有帮助...