Skip to content

Instantly share code, notes, and snippets.

@codyogden
Last active August 8, 2021 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codyogden/4133482e9ccbbab5b3dfe0072bf668c0 to your computer and use it in GitHub Desktop.
Save codyogden/4133482e9ccbbab5b3dfe0072bf668c0 to your computer and use it in GitHub Desktop.
How to specify the Auth0 SSO connection in WordPress with PHP and skip the Auth0 Login Page.
<?php
/**
* Get a valid list of Auth0 Connection slugs.
* use the `wpa0_sso_connection` filter to add or remove slugs.
*
* @return Array Array of Auth0 Connection slugs.
*/
function wpa0_sso_connections() {
$connections = array();
return apply_filters( 'wpa0_sso_connections', $connections );
}
/**
* Filters to detect a specific connection that will
* redirect the user to immediately (instead of hitting
* the Auth0 page)
*
* @param String $connection The Auth0 Connection slug.
*
* @return String Auth0 Connection slug.
*/
add_filter( 'auth0_get_auto_login_connection', 'wpa0_auto_login_filter' );
function wpa0_auto_login_filter($connection) {
if (isset($_GET['connection']) && !empty($_GET['connection']) && in_array($_GET['connection'], array_keys(wpa0_sso_connections()))) {
return $_GET['connection'];
}
return $connection;
}
/**
* Returns `wp_login_url` with the provided, vqlid Auth0 Connection slug.
* For use in templating or other filters.
* @link https://developer.wordpress.org/reference/functions/wp_login_url/
*
* @param String $redirect (Optional) Path to redirect to on log in. Default value: ''
* @param Bool $force_reauth (Optional) Whether to force reauthorization, even if a cookie is present. Default value: false
* @param String $connection (Optional) The Auth0 Connection slug (e.g. google-oauth2, twitter )
*
* @return String The login URL.
*/
function wpa0_sso_login_url( $connection = '', $redirect = '', $force_reauth = false ) {
$login_url = wp_login_url( $redirect, $force_reauth );
return apply_filters( 'wpa0_sso_login_url', (!empty($connection)) ? add_query_arg('connection', $connection, $login_url) : $login_url );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment