{"id":2300,"date":"2011-12-13T08:11:17","date_gmt":"2011-12-13T07:11:17","guid":{"rendered":"http:\/\/wpengineer.com\/?p=2300"},"modified":"2011-12-13T08:11:17","modified_gmt":"2011-12-13T07:11:17","slug":"activate-wordpress-plugins-automatically-via-a-function","status":"publish","type":"post","link":"https:\/\/wpengineer.com\/2300\/activate-wordpress-plugins-automatically-via-a-function\/","title":{"rendered":"Activate WordPress Plugins Automatically via a Function"},"content":{"rendered":"<p>WordPress stores the active Plugins in the database table <code>options<\/code>, field <code>activate_plugins<\/code>, so it is easy to change this value to activate various Plugins by WordPress; either as a Plugin solution after setting up a new installation or because some Plugins need some other Plugins.<br \/>\n<!--more--><br \/>\nI show you as an example a solution. It is important that you don&#8217;t use the Plugin names, but the string of the file, which is also required in various hooks. Below you will also find a simple solution to get to this string in your backend.<\/p>\n<pre>\r\n\/\/ example on admin init, control about register_activation_hook() \r\nadd_action( 'admin_init', 'fb_activate_plugins' );\r\n\/\/ the exmple function\r\nfunction fb_activate_plugins() {\r\n\t\r\n\tif ( ! current_user_can('activate_plugins') )\r\n\t\twp_die(__('You do not have sufficient permissions to activate plugins for this site.'));\r\n\t$plugins = FALSE;\r\n\t$plugins = get_option('active_plugins'); \/\/ get active plugins\r\n\t\r\n\tif ( $plugins ) {\r\n\t\t\/\/ plugins to active\r\n\t\t$pugins_to_active = array(\r\n\t\t\t'hello.php', \/\/ Hello Dolly\r\n\t\t\t'adminimize\/adminimize.php', \/\/ Adminimize\r\n\t\t\t'akismet\/akismet.php' \/\/ Akismet\r\n\t\t);\r\n\t\t\r\n\t\tforeach ( $pugins_to_active as $plugin ) {\r\n\t\t\tif ( ! in_array( $plugin, $plugins ) ) {\r\n\t\t\t\tarray_push( $plugins, $plugin );\r\n\t\t\t\tupdate_option( 'active_plugins', $plugins );\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t} \/\/ end if $plugins\r\n\r\n}\r\n<\/pre>\n<p>The following function and its hook provides a direct output of the string of the Plugin file on the Plugin page in your backend, so please use it only for a quick finding.<\/p>\n<pre>\r\nadd_filter( 'plugin_row_meta', 'fb_get_plugin_string', 10, 4 );\r\nfunction fb_get_plugin_string( $plugin_meta, $plugin_file, $plugin_data, $status ) {\r\n\t\/\/ echo plugin file string\r\n\techo '<code>' . $plugin_file . '<\/code><br>';\r\n\treturn $plugin_meta;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>WordPress stores the active Plugins in the database table options, field activate_plugins, so it is easy to change this value to activate various Plugins by WordPress; either as a Plugin solution after setting up a new installation or because some Plugins need some other Plugins.<\/p>\n","protected":false},"author":321,"featured_media":2362,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[172,33,28,114,17,5,3,20],"class_list":["post-2300","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress-hacks","tag-advent-calendar","tag-backend","tag-code","tag-development","tag-php","tag-plugin","tag-wordpress","tag-wp"],"_links":{"self":[{"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/posts\/2300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/comments?post=2300"}],"version-history":[{"count":0,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/posts\/2300\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/media\/2362"}],"wp:attachment":[{"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/media?parent=2300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/categories?post=2300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpengineer.com\/wp-json\/wp\/v2\/tags?post=2300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}