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?
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
I feel you should try something like this