WordPress.org

Forums

[resolved] Site ignoring 'after_setup_theme' hook (6 posts)

  1. Jack Reichert
    Member
    Posted 3 years ago #

    I'm using WordPress 3.4-beta4-20778

    Here is the code.

    add_action('after_setup_theme', 'theme_activation');
    function theme_activation(){
    	wp_schedule_event(time(), 'twicedaily', 'hourly_uploads_cleanup');
    	flush_rewrite_rules();
    }

    I haven't used this hook before but I understand it is supposed to only fire when the theme is activated. But each time I refresh my site I see more cron jobs added..

    (using Cron GUI)

    May 15, 2012 @ 6:59 (1337065164)	 Twice Daily	hourly_uploads_cleanup
    May 15, 2012 @ 6:59 (1337065181)	 Twice Daily	hourly_uploads_cleanup
    May 15, 2012 @ 6:59 (1337065185)	 Twice Daily	hourly_uploads_cleanup
    May 15, 2012 @ 6:59 (1337065186)	 Twice Daily	hourly_uploads_cleanup
    May 15, 2012 @ 6:59 (1337065193)	 Twice Daily	hourly_uploads_cleanup
    May 15, 2012 @ 7:02 (1337065330)	 Twice Daily	hourly_uploads_cleanup

    Is my code wrong or is this a bug?

    Thank you.

  2. Try using switch_theme instead of after_setup_theme - the setup hook isn't for activation, but for events that need to fire before init on any given load (most commonly post thumbnail support).

    (or better yet, put it in a plugin, where you have register_activation_hook; "hourly uploads cleanup" doesn't sound like something that ought to be handled in a theme)

  3. Jack Reichert
    Member
    Posted 3 years ago #

    Thanks

    In the codex the page for after_setup_theme says:

    This hook is called during a themes initialization. Is generally used to perform basic setup, registration, and init actions for a theme.

    It sounds like it should work the way I set it up to work.

  4. esmi
    Forum Moderator
    Posted 3 years ago #

    "theme initialization" in this context refers to loading the theme's functions during an average page display - not when the theme is first activated.

  5. Jack Reichert
    Member
    Posted 3 years ago #

    I checked out the action switch_theme and the "Usage" section had the usage for the function, so I fixed that in the wiki.

    Thanks for your help.

  6. Jack Reichert
    Member
    Posted 3 years ago #

    Thanks @esmi.

    A bit confusing but now what was happening makes sense.

Topic Closed

This topic has been closed to new replies.

About this Topic