Skip to content

Instantly share code, notes, and snippets.

@pdewouters
Last active December 14, 2015 00:39
Show Gist options
  • Select an option

  • Save pdewouters/5000490 to your computer and use it in GitHub Desktop.

Select an option

Save pdewouters/5000490 to your computer and use it in GitHub Desktop.
Require Genesis for plugin activation
/**
* Method that runs only when the plugin is activated.
* @since 0.1.0
* @return void
*/
public function activation( $network_wide ) {
if ( $this->is_genesis_active() && $this->is_genesis_version_compatible() ) {
// do activation stuff
}
else {
$this->trigger_error( 'This plugin requires Genesis to be installed and activated', E_USER_ERROR );
}
}
/**
* Triggers a PHP error given a message and an error level
* @since 0.1.0
* @return void
* @param $message
* @param $errno
*/
public function trigger_error( $message, $errno ) {
if ( isset( $_GET['action'] ) && $_GET['action'] == 'error_scrape' ) {
echo '<strong>' . $message . '</strong>';
exit;
}
else {
trigger_error( $message, $errno );
}
}
/**
* Tests if the Genesis Theme is the active theme
* @since 0.1.0
* @return bool
*/
public function is_genesis_active() {
$current_theme = wp_get_theme();
$required_theme = wp_get_theme( 'genesis' );
// If Genesis is installed and current theme is a Genesis child theme or Genesis is active theme
if ( $required_theme->exists() && ( ( $this->is_genesis_child() ) || ( 'genesis' == $current_theme ) ) ) {
return true;
}
return false;
}
/**
* Tests if current theme is a child theme of Genesis
* @since 0.1.0
* @return bool
*/
public function is_genesis_child() {
$current_theme = wp_get_theme();
if ( 'genesis' !== $current_theme->get_template() ) {
return false;
}
return true;
}
/**
* Tests if required version of Genesis is available
* @since 0.1.0
* @return bool
*/
public function is_genesis_version_compatible() {
$version_required = '1.9.1';
$theme = wp_get_theme( 'genesis' );
$version_installed = $theme->Version;
if ( version_compare( $version_installed, $version_required, '<' ) ) {
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment