Is there a way to programmatically enable a plugin?

Yes, there’s

  • activate_plugin() defined in wp-admin/includes/plugin.php (which isn’t normally loaded from non-admin code, and note that obviously a PHP plugin file can’t activate itself)
  • wp-cli wp plugin activate if you meant activate from the command line without using the admin site
  • (and at a pinch you could edit the active_plugins value in wp_options in the database, although note that it’s a PHP serialized value and probably safest edited by unserializing the string in PHP, modifying and reserializing)

You’ll probably need to add a plugin header comment to your file to use one of these.

However the simplest way to get a script running is to drop it into wp-content/mu-plugins, the must use plugins folder, which you may have to create yourself first. (This is fairly powerful, so if you do have to create this it might be a good idea to not leave it writeable by the web server user.)

error code: 523