如何将wordpress插件的变量赋值给我自己的变量值? [英] How to assign variable of wordpress plugin to value of my own variable?

查看:98
本文介绍了如何将wordpress插件的变量赋值给我自己的变量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了好几个小时才找到一个解决方案,没有留下任何头发但是仍然... ...



我知道应该有办法实现这一点,但不幸的是,我是PHP的新手,我真的需要你的帮助。



有一些有用的问题在StackOverflow(下面的链接)上,这是一个良好的开端,但我认为还不够。



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 是一个插件到WordPress,允许您轻松创建徽章和,并设置用户获得它们的步骤和成就。



以下是他们的开源插件的链接:



Wordpress: https://wordpress.org/plugins/badgeos/



Github: https://github.com/opencredit/badgeos



我想增加 BadgeOS用户积分值与我的自定义变量的值!让我再解释一下:



使用jQuery post()方法我能够提交数据(例如,一个数字javascript变量)要处理到服务器中指定的 PHP文件。让我们看看我的代码,了解我想用它做什么:



这是我的JavaScript代码:



  var  speechResult =  10 ; 
$ .ajax({
url: https://...Example.php
方法: post
data: {' speechResult':speechResult},
成功: function (res){
console .log(res)
}
});





这是我的 Example.php 中的代码。它只是在控制台中显示 speechResult 值(来自我的javascript代码的值!):



 <?php  
print($ _ POST [' speechResult'])
?>





你可以看到我有一个名为 speechResult 的JavaScript变量,我有一个名为 Example.php的 PHP 文件 .By使用上面的代码,我可以将 speechResult 值传递给 Example.php 或服务器上的任何其他PHP文件(例如 BadgeOS插件的任何PHP文件 ,当然在纸上:))



我想通过 speechResult BadgeOS用户的分数 >变量,但在此之前让我们变得更深:



BadgeOS PHP文件中有变量(你可以在GitHub上找到它所有的PHP包含的文件[这里] [1 ])th处理用户点,例如: $ user_points $ total_points $ current_points



例如,我们在 points-functions.php 文件中有一段代码:



  function  badgeos_get_users_points($ user_id =  0 ){

// 如果没有指定,则使用当前用户的ID
if (! $ user_id)
$ user_id = wp_get_current_user() - > ID;

// 将用户的点作为整数返回(如果为空,则明显回落为0)
return absint(get_user_meta($ user_id,' _ badgeos_points' true ));
}

/ * *
*发布日志条目时用户获得积分
*
* @since 1.0.0
* @param整数$ user_id给定用户的ID
* @param整数$ new_points用户的新积分奖励
* @param整数$ admin_id如果由管理员授予,管理员的用户ID
* @param整数$ achievement_id产生积分的成就,如果适用
* @return整数用户的更新点总计
* /

function badgeos_update_users_points($ user_id = 0 ,$ new_points = 0 ,$ admin_id = 0 ,$ achievement_id = null ){

// 使用c如果没有指定,则为用户的ID
如果(! $ user_id)
$ user_id = get_current_user_id();

// 抓住用户的当前积分
$ current_points = badgeos_get_users_points($ user_id);

// 如果我们获得管理员ID,$ new_points实际上是最终总数,减去当前点数
如果($ admin_id){
$ new_points = $ new_points - $ current_points;
}

// 更新用户总数
$ total_points = max($ current_points + $ new_points, 0 );
update_user_meta($ user_id,' _ badgeos_points',$ total_points);

// 触发其他流程的可用操作
do_action( ' badgeos_update_users_points',$ user_id,$ new_points,$ total_points,$ admin_id,$ achievement_id);

// 也许奖励一些基于积分的徽章
foreach (badgeos_get_points_based_achievements()as $ achievement){
badgeos_maybe_award_achievement_to_user($ achievement-> ID,$ user_id);
}

return $ total_points;
}

/ * *
*记录用户的更新点
*
* @since 1.2.0
* @param integer $ user_id用户ID
* @param整数$ new_points添加到用户总数
* @的点数param integer $ total_points用户的更新总分
* @param整数$ admin_id管理员ID(如果管理员授予)
* @param整数$ achievement_id相关的成就ID
* /

function badgeos_log_users_points($ user_id,$ new_points,$ total_points,$ admin_id,$ achievement_id){

// 设置我们的用户对象
$ user = get_userdata($ user_id);
$ admin = get_userdata($ admin_id);

// 如果这是管理员操作,请更改我们的日志消息
if ($ admin_id)
$ log_message = sprintf(__(' %1 $ s奖励%2 $ s%3 $ s积分为新的总计%4 $ s积分' badgeos'),$ admin-> user_login,$ user-> user_login,number_format($ new_points),number_format($ total_points));
else
$ log_message = sprintf(__(' %1 $ s赢得%2 $ s分为新的总计%3 $ s分'' badgeos'),$ user-> user_login,number_format($ new_points),number_format($ total_points));

// 创建日志条目
$ log_entry_id = badgeos_post_log_entry ($ achievement_id,$ user_id,' points',$ log_message);

// 将相关元添加到我们的日志条目
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 );

/ * *
*根据记录的活动向用户奖励新积分并获得徽章
*
* @since 1.0.0
* @param integer $ user_id给定用户的ID
* @param整数$ achievement_id给定成就的帖子ID
* @return整数用户的更新点总计
* /

function badgeos_award_user_points($ user_id = 0 ,$ achievement_id = 0 ){

// 从提供的帖子中获取我们的积分
$ 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 );





我希望增加 BadgeOS用户积分的值< b> speechReslt 变量(我想使用我自己的外部javascript变量来增加BadgeOS变量)。



任何任何建议或想法都非常感谢... < br $> b $ b

[1]:https://github.com/opencredit/badgeos/tree/master/includes



我尝试了什么:



我用POST方法分配变量,如所描述的那样没有运气......

解决方案

.ajax({
url: https://...Example.php
方法: post
data:{' speechResult':speechResult},
成功:功能(res) {
console .log(res)
}
});





这是我的 Example.php 中的代码。它只是在控制台中显示 speechResult 值(来自我的javascript代码的值!):



 <?php  
print(


_POST [' speechResult'])
?>





你可以看到我有一个名为 speechResult 的JavaScript变量,我有一个名为 PHP 文件> Example.php 。通过使用上面的代码,我可以将 speechResult 值传递给 Example.php 或服务器上的任何其他PHP文件(就像任何PHP文件一样) BadgeOS插件,当然在纸上:) :)



我想增加 BadgeOS用户的分数 speechResult 变量的值,但在此之前让我们更深入:



BadgeOS PHP文件中有变量(你可以找到所有的变量) PHP包括在内处理用户点的GitHub [Here] [1]上的文件:


user_points

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...

解决方案

.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


这篇关于如何将wordpress插件的变量赋值给我自己的变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆