• Resolved kcurlsjr

    (@kcurlsjr)


    We’ve been tinkering with trying to grant admin-awarded badges in bulk through the WP Users screen

    Started by repurposing a function for adding users to BuddyPress groups in bulk; that code can be found here.

    Here is what we came up with:

    function add_achievements_to_users() {
            if( isset( $_GET['action'] ) && isset( $_GET['achievement_id'] ) && isset( $_GET['users'] ) ) {
                $achievement_id = $_GET['achievement_id'];
                $users = $_GET['users'];
    
                foreach ( $users as $user_id ) {
    				badgeos_award_achievement_to_user($achievement_id, $user_id);
                }
            }
            //form submission
            add_action( 'admin_footer', function() { ?>
                <script type="text/javascript" charset="utf-8">
                    jQuery("select[name='action']").append(jQuery('<option value="awardachievement">Award Achievement</option>'));
                    jQuery("#doaction").click(function(e){
                        if(jQuery("select[name='action'] :selected").val()=="awardachievement") { e.preventDefault();
                            achievementid=prompt("Enter an Achievement ID","1");
                            jQuery(".wrap form").append('<input type="hidden" name="achievement_id" value="'+achievementid+'" />').submit();
                        }
                    });
                </script>
            <?php
            });
    }
    add_action ( 'load-users.php', 'add_achievements_to_users' );

    The problem is that the badge is awarded twice (we think, but aren’t sure because all our badges can only be earned once). However, 2 activity feed items are created.

    Anyone have any thoughts on what’s going on?

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

Viewing 4 replies - 1 through 4 (of 4 total)
  • Michael Beckwith

    (@tw2113)

    The BenchPresser

    Interesting solution you have there, and at least from the looks of it, would work for what it’s worth.

    I do have to wonder if the achievements you’re awarding have steps that are similarly or same named as the achievements they’re associated with, and what you’re actually seeing is the step + the achievement.

    You should be able to click to the editor for each, and determine there if one is a step or not.

    @kcurlsjr
    If you are interested by Bulk Awarding Achievements, you may take a look at my BadgeOS Bulk Actions Add-On (premium)

    Hope it can help you !

    kcurlsjr, great suggestion. This is exactly what I was looking for, I am testing it out and so far it looks promising. Thanks to you and Michael Beckwith.

    So I ran into the same problem as kcurlsjr, my awards were being added to the user’s profile twice. I found a fix, it might be a bit hacky so proceed with caution, I am just getting started with this plugin and need to be able to do a few things before I choose it as our solution. So I am only working with test data. I am not worried about ruining my test data, you should be cautious with your data.

    I found a function called badgeos_process_user_data in the fie user.php, it called the function badgeos_award_achievement_to_user and then it redirected afterwards.

    // Award the achievement
    badgeos_award_achievement_to_user( absint( $_GET['achievement_id'] ), absint( $_GET['user_id'] ) );
    // Redirect back to the user editor
    wp_redirect( add_query_arg( 'user_id', absint( $_GET['user_id'] ), admin_url( 'user-edit.php' ) ) );
    exit();

    So I tried the same thing and redirected back to the user list in wp-admin and it seems to be working. Here is the full function I added to my theme. I am curious to know why this works?

    function add_achievements_to_users() {
            if( isset( $_GET['action'] ) && isset( $_GET['achievement_id'] ) && isset( $_GET['users'] ) ) {
                $achievement_id = $_GET['achievement_id'];
                $users = $_GET['users'];
                foreach ( $users as $user_id ) {
    		badgeos_award_achievement_to_user(absint( $achievement_id ), absint ($user_id ));
                }
    	wp_redirect( home_url('/wp-admin/users.php') );
    	exit();
            }
            //form submission
            add_action( 'admin_footer', function() { ?>
                <script type="text/javascript" charset="utf-8">
                    jQuery("select[name='action']").append(jQuery('<option value="awardachievement">Award Achievement</option>'));
                    jQuery("#doaction").click(function(e){
                        if(jQuery("select[name='action'] :selected").val()=="awardachievement") { e.preventDefault();
                            achievementid=prompt("Enter an Achievement ID","1");
                            jQuery(".wrap form").append('<input type="hidden" name="achievement_id" value="'+achievementid+'" />').submit();
                        }
                    });
                </script>
            <?php
            });
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Awarding Badges in Bulk’ is closed to new replies.