Skip to content

Instantly share code, notes, and snippets.

@WPArena
Created March 6, 2017 07:44
Show Gist options
  • Save WPArena/8f6be610066fed753e433216643eb2cb to your computer and use it in GitHub Desktop.
Save WPArena/8f6be610066fed753e433216643eb2cb to your computer and use it in GitHub Desktop.
A Detailed Guide on Adding a Reddit Button To Your WordPress Site https://wp.me/p6prCM-4uk
<?php
require_once("inc/redditoauth.php");
class Reddit_Login_Widget extends WP_Widget
{
public function __construct()
{
parent::__construct("Reddit_login_widget", "Reddit Login", array("description" => __("Display a Reddit Login Button")));
}
public function form( $instance )
{
// Check values
if($instance)
{
$title = esc_attr($instance['title']);
$api_key = $instance['api_key'];
$secret_key = $instance['secret_key'];
$username = $instance['username'];
}
else
{
$title = '';
$api_key = '';
$secret_key = '';
$username = '';
}
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php echo "Title"; ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('api_key'); ?>"><?php echo "Client Id"; ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('api_key'); ?>" name="<?php echo $this->get_field_name('api_key'); ?>" value="<?php echo $api_key; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('secret_key'); ?>"><?php echo "Client Secret"; ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('secret_key'); ?>" name="<?php echo $this->get_field_name('secret_key'); ?>" value="<?php echo $secret_key; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('username'); ?>"><?php echo "Reddit Username"; ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('username'); ?>" name="<?php echo $this->get_field_name('username'); ?>" value="<?php echo $username; ?>" />
</p>
<p>
While creating a Reddit app use "<?php echo get_site_url() . '/wp-admin/admin-ajax.php?action=reddit_oauth_callback' ?>" as callback URL.
</p>
<?php
}
public function update( $new_instance, $old_instance )
{
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['api_key'] = strip_tags($new_instance['api_key']);
$instance['secret_key'] = strip_tags($new_instance['secret_key']);
$instance['username'] = strip_tags($new_instance['username']);
update_option("reddit_client_id", $new_instance['api_key']);
update_option("reddit_client_secret", $new_instance['secret_key']);
update_option("reddit_username", $new_instance['username']);
return $instance;
}
public function widget( $args, $instance )
{
extract($args);
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;
if($title)
{
echo $before_title . $title . $after_title ;
}
if(is_user_logged_in())
{
?>
<a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="Logout"><input type="button" value="Logout" /></a>
<?php
}
else
{
?>
<a href="<?php echo site_url() . '/wp-admin/admin-ajax.php?action=reddit_oauth_redirect'; ?>"><input type="button" value="Login Using Reddit" /></a>
<?php
}
echo $after_widget;
}
}
register_widget("Reddit_Login_Widget");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment