Ticket #7795 (new enhancement)

Opened 3 months ago

Last modified 3 weeks ago

Activate and Deactivate Theme hooks

Reported by: jacobsantos Assigned to: anonymous
Priority: normal Milestone: 2.8
Component: Template Version: 2.7
Severity: normal Keywords: theme API, has-patch, needs-testing
Cc: westi

Description

Currently, there is no standard way of checking whether of theme is activated, deactivated and uninstalled. Plugins have this capability and themes should also have the same to ensure that both share similar functionality.

Attachments

7795.diff (3.9 kB) - added by DD32 on 12/18/08 12:50:03.

Change History

09/26/08 20:41:00 changed by jacobsantos

  • status changed from new to assigned.

09/28/08 20:47:16 changed by westi

  • cc set to westi.

10/13/08 10:13:29 changed by DD32

Marked #7556 as duplicate of this.

10/13/08 10:14:40 changed by DD32

12/12/08 13:04:03 changed by ionfish

  • keywords changed from theme API to theme API, needs-patch.
  • version set to 2.7.

12/18/08 10:46:42 changed by ionfish

See also #8652.

12/18/08 11:55:25 changed by DD32

Hm.. Just started implementing this.

How to handle theme activation.. It would be possible to include both themes at once, that would be the simplest way, but could cause issues if themes duplicate functions.

could run the activation hook on the page reload, so that only 1 theme is included at once.. Thoughts?

12/18/08 12:50:03 changed by DD32

  • attachment 7795.diff added.

12/18/08 12:58:11 changed by DD32

  • keywords changed from theme API, needs-patch to theme API, has-patch, needs-testing.
  • owner changed from jacobsantos to anonymous.
  • status changed from assigned to new.

attachment 7795.diff added.

  • Downside: Theme activation hooks are only run if the theme is activated via the theme panel, Other custom methods will need to call the run_theme_activation_hook() function manually after including the theme.
  • uses register_theme_activation_hook(__FILE__, 'function') and register_theme_deactivation_hook(__FILE__, 'function') syntax, Must be called from a file within the themes directory, but NOT in a subdirectory.
  • Activation hook is run on a page refresh, So activation has 2 redirects, It would be possible to remove the wp_redirect() and exit lines from the theme_activate if block, but the URL would be messy, and any output from the themes activation function would be shown.. Seemed best to redirect again

Example use: (in a themes functions.php)

add_action('generate_rewrite_rules', 'theme_add_rewrite_rules', 100);
function theme_add_rewrite_rules( $wp_rewrite ) {
	$new_rules = array( '^redirect-me/?$' => 'index.php?pagename=to-something-else' );
	$wp_rewrite->rules = $new_rules + (array)$wp_rewrite->rules;
}

register_theme_activation_hook(__FILE__, 'theme_activate');
function theme_activate() {
	global $wp_rewrite;
	$wp_rewrite->flush_rules();
}

register_theme_deactivation_hook(__FILE__, 'theme_deactivate');
function theme_deactivate() {
	global $wp_rewrite;
	remove_action('generate_rewrite_rules', 'theme_add_rewrite_rules', 100);
	$wp_rewrite->flush_rules();
}

(Hope you dont mind me clearing the assigned status jacobsantos)

12/18/08 14:02:30 changed by jacobsantos

I don't mind you replacing me as the assigned, since you've done the most work anyway. I just like being able to track tickets without having to do a search for 'cc' or reporter. I always seem to forget which on I've used.

I don't see why the functions need to be in the themes directory and not a subdirectory. It appears to work just like the plugins activation and deactivation hook. You are hooking into the file name of the main theme, in which case it will always be THEME_NAME/functions.php, which does make it easier. It means that they'll just have to include the file that has those functions in their main file.

This weekend I think I'm going to test it and see how it goes. Hopefully more people will test it.

12/18/08 22:58:03 changed by DD32

You are hooking into the file name of the main theme, in which case it will always be THEME_NAME/functions.php

Errr... 'doh! Here was I thinking i needed to check which theme it was coming from -- Err, Actually, You do need to. In the case of a child and parent theme, you'd want both activation hooks to run, and as such, you'd need to be able to tell which one is calling.

The reason for the "must be in theme root" was because of the __FILE__ -> theme name, __FILE__ returns c:\...\..\ on windows, and the Theme dir constant is C:\...\..../wp-content/..... so a simple str_replace and all that would need to be done, And i was thinking of optimization at the time, its code thats potentially going to run on every page load (I guess a !is_admin() line could short circuit it?

The other option would've been to do:

$theme = preg_replace('|^.*/themes/|', '', dirname($file));
$theme = preg_replace('|[/\\\].*$|', '', $theme);

OR
$theme = preg_replace('|^.*/themes/|', '', dirname($file));
if ( $pos = strpos($theme, '/') )
   $theme = substr($theme, 0, $pos);

(Since /themes/ is hard-coded)

I'm really open to suggestions on a better way there..