I would like to write a function to email me the URL of the website when my theme is activated.

What is the hook initiated when the theme is activated?

  • 5
    Using a Theme activation hook for this purpose is absolutely wrong: "The freedom to run the program means the freedom... to use it... for any kind of... purpose, without being required to communicate about it with the developer or any other specific entity. In this freedom, it is the user's purpose that matters, not the developer's purpose; you as a user are free to run the program for your purposes, and if you distribute it to someone else... you are not entitled to impose your purposes on her." Jul 19 '11 at 20:23
  • 1
    This is a bad idea. As a naive plugin developer early on, I implemented something like this without thinking of the consequences for me or my users. 1. This violates a user's privacy. 2. If your theme is widely distributed, you will receive more email than you can possibly handle. 3. If #2 is true, depending on where you host your email, your account could be construed as in violation of terms of usage. My email account was shut down for a while because of this. Aug 7 '13 at 19:41
  • @BrianFegter Absolutely makes sense. I was only on my initial learning stage when I was trying this out. Thanks for sharing concerns. The greatest fact about StackOverflow and StackExchange is that when you look at your questions in past year, you realize how much you have developed from time to time :-)
    – Atif
    Aug 8 '13 at 20:52

I have that code here just name the file theme_activation_hook.php like on the website and copy this.

 * Provides activation/deactivation hook for wordpress theme.
 * @author Krishna Kant Sharma (http://www.krishnakantsharma.com)
 * Usage:
 * ----------------------------------------------
 * Include this file in your theme code.
 * ----------------------------------------------
 * function my_theme_activate() {
 *    // code to execute on theme activation
 * }
 * wp_register_theme_activation_hook('mytheme', 'my_theme_activate');
 * function my_theme_deactivate() {
 *    // code to execute on theme deactivation
 * }
 * wp_register_theme_deactivation_hook('mytheme', 'my_theme_deactivate');
 * ----------------------------------------------

 * @desc registers a theme activation hook
 * @param string $code : Code of the theme. This can be the base folder of your theme. Eg if your theme is in folder 'mytheme' then code will be 'mytheme'
 * @param callback $function : Function to call when theme gets activated.
function wp_register_theme_activation_hook($code, $function) {
    $optionKey="theme_is_activated_" . $code;
    if(!get_option($optionKey)) {
        update_option($optionKey , 1);

 * @desc registers deactivation hook
 * @param string $code : Code of the theme. This must match the value you provided in wp_register_theme_activation_hook function as $code
 * @param callback $function : Function to call when theme gets deactivated.
function wp_register_theme_deactivation_hook($code, $function) {
    // store function in code specific global
    $GLOBALS["wp_register_theme_deactivation_hook_function" . $code]=$function;

    // create a runtime function which will delete the option set while activation of this theme and will call deactivation function provided in $function
    $fn=create_function('$theme', ' call_user_func($GLOBALS["wp_register_theme_deactivation_hook_function' . $code . '"]); delete_option("theme_is_activated_' . $code. '");');

    // add above created function to switch_theme action hook. This hook gets called when admin changes the theme.
    // Due to wordpress core implementation this hook can only be received by currently active theme (which is going to be deactivated as admin has chosen another one.
    // Your theme can perceive this hook as a deactivation hook.
    add_action("switch_theme", $fn);
  • 1
    The author of this code (Krishna Kant Sharma) also left an answer with a link to its source. Maybe by the time Benny answered this question, he was not savvy enough to simply edit Krishna's answer and add the code to it, hence my downvote...
    – brasofilo
    Jun 21 '12 at 13:54

I have written a code which provides a reliable activation/deactivation theme hooks. Please check it out and let me know what you guys think!


  • @Krisha Kant Sharma: That code looks promising, but could you copy it into your answer? Then it will still exist if your blog ever changes location, or for some reason goes offline.
    – Jan Fabry
    Jan 25 '11 at 8:58
  • 1
    Krishna's code is the one in Benny's answer
    – brasofilo
    Jun 21 '12 at 13:44

There is no specialized hook for this. I have seen couple of approaches:

I want to note that emailing yourself any information without user's consent (and running anything on activation has no opportunity to request such) can be viewed as inappropriate.

  • Is it? just the URL so that I may know where it is installed?
    – Atif
    Jan 3 '11 at 12:07

Wordpress now provides this hook as after_switch_theme. You can use it like so:

add_action('after_switch_theme', 'my_theme_activation');

function my_theme_activation () {

You can use the switch_theme hook to run code on theme deactivation as well.

Source: http://codex.wordpress.org/Plugin_API/Action_Reference/after_switch_theme


Place this code at the top in your functions.php

<?php if ( is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" ) {
// do your stuff
$url = get_site_url();
// The message
$message = "a new wordpress theme is activated on $url ";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");

// Send
wp_mail('mail@yourdomain.com', 'theme geactiveerd', $message);


replace mail@yourdomain.com with your own email address.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.