Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Daniel Mobbs
Daniel Mobbs
7,634 Points

badge plugin

Hey guys need some help with this. Been banging away for hours, cant work out what is wrong?? Thanks for any help in advance :) Heres the code:

<?php

/* *

  • Plugin Name: Beast Badge
  • PLugin URI: www.mobbzys.co.uk
  • Author: Daniel Mobbs
  • Author URI: nothing here...
  • Description: A plugin to share Treehouse Badges on your website.
  • Version: 1.0
  • License: GPL2
  • Please share this... * */

/*

  • Assign global variables * */

$plugin_url = WP_PLUGIN_URL . '/dsm_badge_beast.php'; $options = array(); $display_json = false;

function dsmbeast_badges() {

add_options_page(
    'Beast Badges Plugin', 
    'Beast Badges', 
    'manage_options',
    'dsm-beast-badges',
    'beastbadges_options_page'
);

} add_action('admin_menu', 'dsmbeast_badges');

function beastbadges_options_page() {

if( !current_user_can( 'manage_options' ) ){
    wp_die( "you can't do that, here!!!" );
}

global $plugin_url;
global $options;
global $display_json;

if( isset( $_POST['dsm_form_submitted'] ) ) {

    $hidden_field = esc_html( $_POST[ 'dsm_form_submitted' ] );
    if( $hidden_field == 'Y' ) {

        $dsm_username = esc_html( $_POST['dsm_username'] );

        $dsm_profile = dsm_badges_get_profile( $dsm_username );

        $options['dsm_username']   = $dsm_username;
        $options['dsm_profile']    = $dsm_profile;
        $options['last_updated']   = time();

        update_option( 'dsm_badge_beast', $options );

    }

}

$options = get_option( 'dsm_badge_beast');

if( $options != '' ) {

    $dsm_username = $options['dsm_username'];
    $dsm_profile = $options['dsm_profile'];
}

require( 'inc/options.php' );

}

class Dsm_Beast_Badges_widget extends WP_Widget {

function dsm_Beast_badges_widget() {
    // Instantiate the parent object
    parent::__construct( false, 'Beast Badge' );
}

function widget( $args, $instance ) {
    // Widget output

    extract( $args );
    $title = apply_filters( 'widget_title', $instance['title'] );
    $num_badges = $instance['num_badges'];
    $display_tooltip = $instance['display_tooltip'];

    $options = get_option( 'dsm_beast_badges' );
    $dsm_profile = $options['dsm_profile'];

    require( 'inc/front-end.php');
}

function update( $new_instance, $old_instance ) {
    // Save widget options

    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['num_badges'] = strip_tags($new_instance['num_badges']);
    $instance['display_tooltip'] = strip_tags($new_instance['display_tooltip']);

    return $instance;
}

function form( $instance ) {
    // Output admin widget options form

    $title = esc_attr( $instance['title'] );
    $num_badges = esc_attr( $instance['num_badges'] );
    $display_tooltip = esc_attr( $instance['display_tooltip'] );

    $options = get_option( 'dsm_beast_badges' );
    $dsm_profile = $options['dsm_profile'];

    require( 'inc/widget_fields.php' );

}

}

function dsm_beast_badges_widget() { register_widget( 'Dsm_Beast_Badges_widget' ); }

add_action( 'widgets_init', 'dsm_beast_badges_widget' );

function dsm_badges_get_profile( $dsm_username ) {

$json_feed_url = 'https://teamtreehouse.com/' . $dsm_username . '.json';
$args = array( 'timeout' => 120 );

$json_feed = wp_remote_get( $json_feed_url, $args );

$dsm_profile = json_decode( $json_feed['body'] );

return $dsm_profile;

}

/*

  • Style * */

function dsm_badge_styles() {

wp_enqueue_style( 'dsm_badge_styles', plugins_url( 'dsm_badge_beast/dsm_badge_beast.css ') );

} add_action( 'admin_head', 'dsm_badge_styles' );

?>

Daniel Mobbs
Daniel Mobbs
7,634 Points

<p> <label>Title</label> <input class="widefat" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /> </p>

<p> <ul class="wptreehouse-badges-and-points">

    <li>Badges: <strong><?php echo count( $dsm_profile->{'badges'} ); ?></strong></li>

</ul>

</p> <p> <label>How many of your most recent badges would you you like to display?</label> <input size="4" name="<?php echo $this->get_field_name('num_badges'); ?>" type="text" value="<?php echo $num_badges; ?>" /> </p> <p> <label>Display tooltips?</label> <input type="checkbox" name="<?php echo $this->get_field_name('display_tooltip'); ?>" value="1" <?php checked( $display_tooltip, 1 ); ?> /> </p>

I DIDNT SAY WHAT IS WRONG... cant get badge count to display on widget area or front end :)