[wp-trac] Re: [WordPress Trac] #7795: Activate and Deactivate Theme
hooks
WordPress Trac
wp-trac at lists.automattic.com
Thu Dec 18 12:58:11 GMT 2008
#7795: Activate and Deactivate Theme hooks
-------------------------------------------------+--------------------------
Reporter: jacobsantos | Owner: anonymous
Type: enhancement | Status: new
Priority: normal | Milestone: 2.8
Component: Template | Version: 2.7
Severity: normal | Resolution:
Keywords: theme API, has-patch, needs-testing |
-------------------------------------------------+--------------------------
Changes (by DD32):
* keywords: theme API, needs-patch => theme API, has-patch, needs-
testing
* owner: jacobsantos => anonymous
* status: assigned => new
Comment:
> 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)
--
Ticket URL: <http://trac.wordpress.org/ticket/7795#comment:8>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list