I made a WordPress plugin, so now what I want is, I want to set a logic like if a certain theme is active, then load the plugin. Else, only keep the plugin activated, but don't apply any changes on the site. Any idea how can I achieve that? Sorry if I sound very nerdy :')
2 Answers
Use get_stylesheet()
to get the directory name of the theme being used. If a child theme is being used you can determine the parent with get_template()
.
At the top of the plugin file you can simply return
early if the value is not what you want.
if ( 'my-theme' !== get_stylesheet() ) {
return;
}
You can use the wp_get_theme()
function to get information about the active theme and specify its name.
add_action('init', 'pre_function_test');
function pre_function_test()
{
$var = wp_get_theme();
$theme_name = $var['Name'];
if( $theme_name == 'Hello Elementor'){
wp_die('is theme verify !');
}
}
Here my theme name is Hello Elementor and my condition is executed.
Note: You can see the theme name from the theme directory and style.css file.
The wp_get_theme() function returns the current theme information.
See in this picture: