Javascript 如何修改wordpress插件变量值
myCred是WordPress的一个插件,允许您轻松创建积分余额,并设置用户获取积分的步骤 以下是他们的开源插件的链接: Github: myCred网站: Wordpress: 准备好了!这里是:我找不到一个解决方案如何将我自己的$amount添加到$current_余额中以获得$new_余额 我已经尝试过类似的方法,但没有成功:Javascript 如何修改wordpress插件变量值,javascript,php,wordpress,wordpress-hook,Javascript,Php,Wordpress,Wordpress Hook,myCred是WordPress的一个插件,允许您轻松创建积分余额,并设置用户获取积分的步骤 以下是他们的开源插件的链接: Github: myCred网站: Wordpress: 准备好了!这里是:我找不到一个解决方案如何将我自己的$amount添加到$current_余额中以获得$new_余额 我已经尝试过类似的方法,但没有成功: add_filter('filter_update_users_balance', 'add_my_own_amount'); function add_my_
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 );
}
之后,它会更新用户余额,如下所示:
/**
* 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;
}
这里有一行代码$new\u balance=$current\u balance+$amount;这向我们展示了插件如何返回用户的新余额。在代码的最底层,它确实返回$this->number$new\u 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插件分配一个变量,我认为它是一个javascript变量的$amount值,以便将交互式电子学习课程连接到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值传递给myCred**中的$amount变量,那么我将根据学习电子技术的html5课程中发生的情况控制对用户的重写点
我想传递speechResult值以分配myCred$amount值。。。为了增加或减少使用的总平衡点 我不完全确定你想要实现什么。也许每个余额加10 您可以在代码中找到apply_筛选器的任何位置使用add_筛选器。举个例子:
add_filter('mycred_get_users_cred', function($balance, $this, $user_id, $type) {
return $balance + 10;
});
不确定,如果这有帮助…它位于“获取用户平衡”功能中