diff --git src/wp-admin/includes/plugin.php src/wp-admin/includes/plugin.php
index 7d804e1c66..c38dd832d3 100644
--- src/wp-admin/includes/plugin.php
+++ src/wp-admin/includes/plugin.php
@@ -557,7 +557,17 @@ function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silen
 		ob_start();
 		wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
 		$_wp_plugin_file = $plugin;
+
+		if ( ! defined( 'WP_ADMIN' ) ) {
+			define( 'WP_ADMIN', true );
+		}
+
 		include_once( WP_PLUGIN_DIR . '/' . $plugin );
+
+		// Trigger key actions that are done on the plugin editor to cause the relevant plugin hooks to fire and potentially cause errors.
+		do_action( 'init' );
+		do_action( 'admin_init' );
+
 		$plugin = $_wp_plugin_file; // Avoid stomping of the $plugin variable in a plugin.
 
 		if ( ! $silent ) {
diff --git src/wp-admin/plugin-editor.php src/wp-admin/plugin-editor.php
index 14d2b65039..c986824e6c 100644
--- src/wp-admin/plugin-editor.php
+++ src/wp-admin/plugin-editor.php
@@ -46,7 +46,20 @@ if ( isset( $_REQUEST['plugin'] ) ) {
 
 if ( empty( $plugin ) ) {
 	if ( $file ) {
-		$plugin = $file;
+
+		// Locate the plugin for a given plugin file being edited.
+		$file_dirname = dirname( $file );
+		foreach ( array_keys( $plugins ) as $plugin_candidate ) {
+			if ( $plugin_candidate === $file || ( '.' !== $file_dirname && dirname( $plugin_candidate ) === $file_dirname ) ) {
+				$plugin = $plugin_candidate;
+				break;
+			}
+		}
+
+		// Fallback to the file as the plugins.
+		if ( empty( $plugin ) ) {
+			$plugin = $file;
+		}
 	} else {
 		$plugin = array_keys( $plugins );
 		$plugin = $plugin[0];
@@ -72,6 +85,10 @@ if ( isset( $_REQUEST['action'] ) && 'update' === $_REQUEST['action'] ) {
 		fwrite($f, $newcontent);
 		fclose($f);
 
+		if ( function_exists( 'opcache_invalidate' ) ) {
+			opcache_invalidate( $real_file, true );
+		}
+
 		$network_wide = is_plugin_active_for_network( $file );
 
 		// Deactivate so we can test it.
@@ -220,7 +237,7 @@ if ( isset( $_REQUEST['action'] ) && 'update' === $_REQUEST['action'] ) {
 <?php if (isset($_GET['a'])) : ?>
  <div id="message" class="updated notice is-dismissible"><p><?php _e('File edited successfully.') ?></p></div>
 <?php elseif (isset($_GET['phperror'])) : ?>
- <div id="message" class="updated"><p><?php _e('This plugin has been deactivated because your changes resulted in a <strong>fatal error</strong>.') ?></p>
+ <div id="message" class="notice notice-error"><p><?php _e('This plugin has been deactivated because your changes resulted in a <strong>fatal error</strong>.') ?></p>
 	<?php
 		if ( wp_verify_nonce( $_GET['_error_nonce'], 'plugin-activation-error_' . $file ) ) {
 			$iframe_url = add_query_arg( array(
