Skip to content

Instantly share code, notes, and snippets.

@WPArena
Created July 19, 2018 00:26
Show Gist options
  • Save WPArena/e33afad0c372fa16b922c28816d73543 to your computer and use it in GitHub Desktop.
Save WPArena/e33afad0c372fa16b922c28816d73543 to your computer and use it in GitHub Desktop.
How to Login into WordPress using Github - https://wparena.com/login-wordpress-using-github/
class GitHub_Login_Widget extends WP_Widget
{
public function __construct()
{
parent::__construct( "Github_login_widget", "GitHub Login", array( "description" => __("Display a GitHub Login Button" ) ) );
}
public function form( $instance ) {
if ( $instance ) {
$title = esc_attr( $instance['title'] );
$api_key = $instance['api_key'];
$secret_key = $instance['secret_key'];
$github_app_name = $instance['github_app_name'];
} else {
$title = '';
$api_key = '';
$secret_key = '';
$github_app_name = '';
}
?>
<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 "Secret Key"; ?></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( 'github_app_name' ); ?>"><?php echo "App Name"; ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'github_app_name' ); ?>" name="<?php echo $this->get_field_name( 'github_app_name' ); ?>" value="<?php echo $github_app_name; ?>" />
</p>
<p>
While creating a Github app use "<?php echo get_site_url() . '/wp-admin/admin-ajax.php?action=github_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['github_app_name'] = strip_tags( $new_instance['github_app_name'] );
update_option( "github_key", $new_instance['api_key'] );
update_option( "github_secret", $new_instance['secret_key'] );
update_option( "github_app_name", $new_instance['github_app_name'] );
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=github_oauth_redirect'; ?>"><input type="button" value="Login Using Github" /></a>
<?php
}
echo $after_widget;
}
}
register_widget( "GitHub_Login_Widget" );
require_once( 'inc/githuboauth.php' );
if( is_user_logged_in() )
{
//GET Request
$response = GithubApiRequest( "https://api-url/", false, array("HTTP Header" => "HTTP Header Value", "HTTP Header" => "HTTP Header Value") );
//POST Request
$response = GithubApiRequest( "https://api-url/", array("Field Name" => "Field Value", "Field Name" => "Field Value"), array("HTTP Header" => "HTTP Header Value", "HTTP Header" => "HTTP Header Value") );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment