Skip to content

Instantly share code, notes, and snippets.

@stefanomarra
Last active April 2, 2020 18:16
Show Gist options
  • Save stefanomarra/d8ed4cd8e44b43821986946f73c289e5 to your computer and use it in GitHub Desktop.
Save stefanomarra/d8ed4cd8e44b43821986946f73c289e5 to your computer and use it in GitHub Desktop.
This PHP function will auto initialize your Emojics wordpress plugin based on the host. Emojics WP Plugin is needed: https://wordpress.org/plugins/emojics-wp/
/**
* This snippet needs to be added in the functions.php file of your theme
* This function will auto initialize emojics based on the known hosts array defined in the function
* Update $known_hosts array by adding the key => value pair host => api key as shown in the commented example
*/
function emojics_auto_init() {
/**
* Store key value pairs of known hosts as key and api key as value
*/
$known_hosts = array(
// 'development.example.local' => '[YOUR_DEVELOPMENT_API_KEY]',
// 'production.example.com' => '[YOUR_PRODUCTION_API_KEY]',
// 'staging.example.com' => '[YOUR_STAGING_API_KEY]'
);
/**
* Get the current host
*/
$host = apply_filters( 'emojics_get_host', isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:false );
/**
* Do nothing if needed class/function doesn't exists or server host is not defined
*/
if ( !class_exists('Emojics_Admin') || !function_exists('emojics_set_option') || !$host ) return;
/**
* Do nothing if the host is not in the known hosts array
*/
if ( !isset($known_hosts[$host]) ) return;
/**
* Do nothing if the host api key is empty
*/
if ( !$known_hosts[$host] ) return;
/**
* Do nothing if the host is already being initialized
*/
if ( $host == get_option('_emojics_initialized_host', false) ) return;
/**
* Authenticate the host with api key
*/
$e = new Emojics_Admin('emojics-wp', '1.5.4');
$r = $e->authenticate_api_key($known_hosts[$host]);
if ( $e->is_authentication_valid($r) ) {
emojics_set_option( 'js_code', $r['js_code_snippet'] );
}
/**
* Store the initialized host to avoid re-authentication
*/
update_option( '_emojics_initialized_host', $host );
}
add_action( 'init', 'emojics_auto_init');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment