henrywright
henrywright

Reputation: 10240

How to run a function on theme activation only in WordPress?

I want to add a capability to one of the default roles in WordPress. The add_cap article advises people to do this sort of thing on theme activation because the setting is saved to the database:

NB: This setting is saved to the database, so it might be better to run this on theme/plugin activation

The function I intend to use is:

function add_theme_caps() {
    $role = get_role( 'author' );
    $role->add_cap( 'edit_others_posts' ); 
}
add_action( 'admin_init', 'add_theme_caps');

As you can see, I'm currently hooking to admin_init, which causes the function to be run each time the admin area is accessed. How can I run the function on theme activation only?

Upvotes: 3

Views: 897

Answers (2)

Ram Sharma
Ram Sharma

Reputation: 8819

I feel you should try something like this

if ( is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" ) {
    $role = get_role( 'author' );
    $role->add_cap( 'edit_others_posts' );
}

Upvotes: 0

Kaloyan
Kaloyan

Reputation: 7352

You can use after_switch_theme . It will affect only your theme, of course.

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

Upvotes: 3

Related Questions