myCred 是 WordPress 的插件,可让您轻松创建积分余额并设置用户赚取积分的步骤。< / p>
以下是其开源插件的链接:
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
值...,以便增加或减少使用的总平衡点。
答案 0 :(得分:1)
我不确定要实现什么目标。也许每个余额增加10?
您可以在代码中的任何地方add_filter
中使用apply_filters
。因此,例如:
add_filter('mycred_get_users_cred', function($balance, $this, $user_id, $type) {
return $balance + 10;
});
不确定,是否有帮助...