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

Read More

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?

Related posts

Leave a Reply

2 comments

  1. 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' );
    }