Never actually adding the action? Or do I have to call the action?

I have the following class that is instantiated in the function.php file. In the constructor I am setting up a activation and a deactivation hook for the theme. How ever neither seem to be called when I actually switch between this theme and say twenty fourteen and then back. As the echo statements are not called:

namespace Core;

class Activation {

    protected $themeOptions = null;

    public function __construct(){
        if ($this->themeOptions === null) {
            $this->themeOptions = \Freya\Factory\Pattern::create('Freya\Templates\Options');
        }

        if(is_admin()) {
            echo 'Sample';
            // On Activation ...
            add_action('after_theme_switch', array($this, 'themeActivation'));

            // On Deactivation ...
            add_action('switch_theme', array($this, 'themeDeactivation'));
        }
    }

    public function themeActivation(){
        echo 'activated';
        $this->setUpPluginOptions();
        $this->installPlugins();
    }

    public function themeDeactivation(){
        echo 'deactivated';
        $this->themeOptions->deleteOptions(array (
            'theme_options',
            'plugin_management'
        ));
    }

    public function plugin_install_error_message() {
        $this->themeOptions->renderView('plugin_install_error_message');
    }

    protected function installPlugins() {
        if (!get_option('plugin_installed') && !get_option('plugin_install_error') && is_admin()) {
            $pluginManagement = new \Core\Plugins\InstallPlugins();
            $pluginManagement->installPlugins();

            if (get_option('plugin_install_error')) {
                add_action('admin_notices', array ($this, 'plugin_install_error_message'));
            }
        }
    }

    protected function setUpPluginOptions() {
        $this->themeOptions->createOptions(
            array (
                'plugin_management' => array (
                    'plugin_installed',
                    'plugin_install_error',
                    'plugin_install_success',
                )
            )
        );
    }
}

The echo Sample; is called when the theme is activated. so I know its at least getting here. But the other echos are not called.

Can some one tell me why the add_action is not doing what I want it to? do I have to call do_action? is there something I am missing?

Solutions

You are using after_theme_switch action but the correct name is after_switch_theme.

Also, you won't see the echo statements on after_theme_switch and switch_theme. To debug things in that hooks you can use, for example, error_log() function and look for messages in PHP error log file (you need to have errors "On" and/or WP_DEBUG enabled).

I've tested this and it works:

new Activation;

class Activation {

    public function __construct(){

            add_action('after_switch_theme', array($this, 'themeActivation'));
            add_action('switch_theme', array($this, 'themeDeactivation'));

    }

    public function themeActivation(){
        error_log( 'activated' );
    }

    public function themeDeactivation(){
        error_log( 'deactivated' );
    }

}

Similar questions

action wp_set_comment_status never gets fired when i change the comment status
I want to run some code on comment status change so using the action wp_set_comment_status but it seem to never fired when comment status gets changed. I am using wordpress 3.3.1. Here is a simple code which doesn't work for me: There was a same bug on the wp version 2.8.2 and it was fixed later on. Not sure if i am doing something wrong or not. ht...
Ajax comes back as success but the action is "never" called
I am helping with a plugin build on Wordpress and am a bit stuck on what should be a pretty basic ajax call. I am trying to take the object and run it through the method 'grab_object_from_ajax_call' however the method never runs and I am stumped on why it is not happening. This is the javascript portion of my code: (am I calling the wrong action?) ...
Why does preg_replace_callback never fire in this function?
In the function below, the save_content function runs, but the doReplace does not (echo "This is the doReplace" never shows. Any ideas why?
Wordpress menu: How to make www.link.com/#name never contain current_page_item class?
I am using the wordpress menus on a website, but the problem is that one of the links on that menu is www.homepage.com/#order So, when I am on the homepage, both "home" and "order" links on the menu contain the class current_page_item, which with my css it makes them appear both as the active button. How can I make it so the #order link never conta...
thickbox never gets called (weird behavior)
I have the exact same files (except for wp-config) running on a local server and a test server, and in my functions.php I have this: On my localhost, everything works fine and all files are happily working. But on the test server, somehow, thickbox never gets called, i don't get any errors on the console and Firebug Network tab simply doesn't know ...
Which WordPress pages/URLs should never be cached?
I have a multisite blog that will soon have an Akamai caching service sitting in front of it, serving cached content to visitors (15 minute TTL). I want to configure Akamai to exclude any important URLs that should never be cached (i.e., don't want to serve cached login/admin pages). Is there a compilation, or general recommendation, of all importa...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.