3 * Edit plugin editor administration panel.
6 * @subpackage Administration
9 /** WordPress Administration Bootstrap */
10 require_once( dirname( __FILE__ ) . '/admin.php' );
12 if ( is_multisite() && ! is_network_admin() ) {
13 wp_redirect( network_admin_url( 'plugin-editor.php' ) );
17 if ( !current_user_can('edit_plugins') )
18 wp_die( __('Sorry, you are not allowed to edit plugins for this site.') );
20 $title = __("Edit Plugins");
21 $parent_file = 'plugins.php';
23 $plugins = get_plugins();
25 if ( empty( $plugins ) ) {
26 include( ABSPATH . 'wp-admin/admin-header.php' );
29 <h1><?php echo esc_html( $title ); ?></h1>
30 <div id="message" class="error"><p><?php _e( 'You do not appear to have any plugins available at this time.' ); ?></p></div>
33 include( ABSPATH . 'wp-admin/admin-footer.php' );
39 if ( isset( $_REQUEST['file'] ) ) {
40 $file = sanitize_text_field( $_REQUEST['file'] );
43 if ( isset( $_REQUEST['plugin'] ) ) {
44 $plugin = sanitize_text_field( $_REQUEST['plugin'] );
47 if ( empty( $plugin ) ) {
51 $plugin = array_keys( $plugins );
56 $plugin_files = get_plugin_files($plugin);
59 $file = $plugin_files[0];
61 $file = validate_file_to_edit($file, $plugin_files);
62 $real_file = WP_PLUGIN_DIR . '/' . $file;
63 $scrollto = isset($_REQUEST['scrollto']) ? (int) $_REQUEST['scrollto'] : 0;
65 if ( isset( $_REQUEST['action'] ) && 'update' === $_REQUEST['action'] ) {
67 check_admin_referer('edit-plugin_' . $file);
69 $newcontent = wp_unslash( $_POST['newcontent'] );
70 if ( is_writeable($real_file) ) {
71 $f = fopen($real_file, 'w+');
72 fwrite($f, $newcontent);
75 $network_wide = is_plugin_active_for_network( $file );
77 // Deactivate so we can test it.
78 if ( is_plugin_active( $plugin ) || isset( $_POST['phperror'] ) ) {
79 if ( is_plugin_active( $plugin ) ) {
80 deactivate_plugins( $plugin, true );
83 if ( ! is_network_admin() ) {
84 update_option( 'recently_activated', array( $file => time() ) + (array) get_option( 'recently_activated' ) );
86 update_site_option( 'recently_activated', array( $file => time() ) + (array) get_site_option( 'recently_activated' ) );
89 wp_redirect( add_query_arg( '_wpnonce', wp_create_nonce( 'edit-plugin-test_' . $file ), "plugin-editor.php?file=$file&plugin=$plugin&liveupdate=1&scrollto=$scrollto&networkwide=" . $network_wide ) );
92 wp_redirect( self_admin_url( "plugin-editor.php?file=$file&plugin=$plugin&a=te&scrollto=$scrollto" ) );
94 wp_redirect( self_admin_url( "plugin-editor.php?file=$file&plugin=$plugin&scrollto=$scrollto" ) );
100 if ( isset($_GET['liveupdate']) ) {
101 check_admin_referer('edit-plugin-test_' . $file);
103 $error = validate_plugin( $plugin );
105 if ( is_wp_error( $error ) ) {
109 if ( ( ! empty( $_GET['networkwide'] ) && ! is_plugin_active_for_network( $file ) ) || ! is_plugin_active( $file ) ) {
110 activate_plugin( $plugin, "plugin-editor.php?file=$file&phperror=1", ! empty( $_GET['networkwide'] ) );
111 } // we'll override this later if the plugin can be included without fatal error
113 wp_redirect( self_admin_url("plugin-editor.php?file=$file&plugin=$plugin&a=te&scrollto=$scrollto") );
117 // List of allowable extensions
118 $editable_extensions = array('php', 'txt', 'text', 'js', 'css', 'html', 'htm', 'xml', 'inc', 'include');
121 * Filters file type extensions editable in the plugin editor.
125 * @param array $editable_extensions An array of editable plugin file extensions.
127 $editable_extensions = (array) apply_filters( 'editable_extensions', $editable_extensions );
129 if ( ! is_file($real_file) ) {
130 wp_die(sprintf('<p>%s</p>', __('No such file exists! Double check the name and try again.')));
132 // Get the extension of the file
133 if ( preg_match('/\.([^.]+)$/', $real_file, $matches) ) {
134 $ext = strtolower($matches[1]);
135 // If extension is not in the acceptable list, skip it
136 if ( !in_array( $ext, $editable_extensions) )
137 wp_die(sprintf('<p>%s</p>', __('Files of this type are not editable.')));
141 get_current_screen()->add_help_tab( array(
143 'title' => __('Overview'),
145 '<p>' . __('You can use the editor to make changes to any of your plugins’ individual PHP files. Be aware that if you make changes, plugins updates will overwrite your customizations.') . '</p>' .
146 '<p>' . __('Choose a plugin to edit from the dropdown menu and click the Select button. Click once on any file name to load it in the editor, and make your changes. Don’t forget to save your changes (Update File) when you’re finished.') . '</p>' .
147 '<p>' . __('The Documentation menu below the editor lists the PHP functions recognized in the plugin file. Clicking Look Up takes you to a web page about that particular function.') . '</p>' .
148 '<p id="newcontent-description">' . __( 'In the editing area the Tab key enters a tab character. To move below this area by pressing Tab, press the Esc key followed by the Tab key. In some cases the Esc key will need to be pressed twice before the Tab key will allow you to continue.' ) . '</p>' .
149 '<p>' . __('If you want to make changes but don’t want them to be overwritten when the plugin is updated, you may be ready to think about writing your own plugin. For information on how to edit plugins, write your own from scratch, or just better understand their anatomy, check out the links below.') . '</p>' .
150 ( is_network_admin() ? '<p>' . __('Any edits to files from this screen will be reflected on all sites in the network.') . '</p>' : '' )
153 get_current_screen()->set_help_sidebar(
154 '<p><strong>' . __('For more information:') . '</strong></p>' .
155 '<p>' . __('<a href="https://codex.wordpress.org/Plugins_Editor_Screen">Documentation on Editing Plugins</a>') . '</p>' .
156 '<p>' . __('<a href="https://codex.wordpress.org/Writing_a_Plugin">Documentation on Writing Plugins</a>') . '</p>' .
157 '<p>' . __('<a href="https://wordpress.org/support/">Support Forums</a>') . '</p>'
160 require_once(ABSPATH . 'wp-admin/admin-header.php');
162 update_recently_edited(WP_PLUGIN_DIR . '/' . $file);
164 $content = file_get_contents( $real_file );
166 if ( '.php' == substr( $real_file, strrpos( $real_file, '.' ) ) ) {
167 $functions = wp_doc_link_parse( $content );
169 if ( !empty($functions) ) {
170 $docs_select = '<select name="docs-list" id="docs-list">';
171 $docs_select .= '<option value="">' . __( 'Function Name…' ) . '</option>';
172 foreach ( $functions as $function) {
173 $docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>';
175 $docs_select .= '</select>';
179 $content = esc_textarea( $content );
181 <?php if (isset($_GET['a'])) : ?>
182 <div id="message" class="updated notice is-dismissible"><p><?php _e('File edited successfully.') ?></p></div>
183 <?php elseif (isset($_GET['phperror'])) : ?>
184 <div id="message" class="updated"><p><?php _e('This plugin has been deactivated because your changes resulted in a <strong>fatal error</strong>.') ?></p>
186 if ( wp_verify_nonce( $_GET['_error_nonce'], 'plugin-activation-error_' . $file ) ) {
187 $iframe_url = add_query_arg( array(
188 'action' => 'error_scrape',
189 'plugin' => urlencode( $file ),
190 '_wpnonce' => urlencode( $_GET['_error_nonce'] ),
191 ), admin_url( 'plugins.php' ) );
193 <iframe style="border:0" width="100%" height="70px" src="<?php echo esc_url( $iframe_url ); ?>"></iframe>
198 <h1><?php echo esc_html( $title ); ?></h1>
200 <div class="fileedit-sub">
201 <div class="alignleft">
203 if ( is_plugin_active( $plugin ) ) {
204 if ( is_writeable( $real_file ) ) {
205 /* translators: %s: plugin file name */
206 echo sprintf( __( 'Editing %s (active)' ), '<strong>' . $file . '</strong>' );
208 /* translators: %s: plugin file name */
209 echo sprintf( __( 'Browsing %s (active)' ), '<strong>' . $file . '</strong>' );
212 if ( is_writeable( $real_file ) ) {
213 /* translators: %s: plugin file name */
214 echo sprintf( __( 'Editing %s (inactive)' ), '<strong>' . $file . '</strong>' );
216 /* translators: %s: plugin file name */
217 echo sprintf( __( 'Browsing %s (inactive)' ), '<strong>' . $file . '</strong>' );
222 <div class="alignright">
223 <form action="plugin-editor.php" method="post">
224 <strong><label for="plugin"><?php _e('Select plugin to edit:'); ?> </label></strong>
225 <select name="plugin" id="plugin">
227 foreach ( $plugins as $plugin_key => $a_plugin ) {
228 $plugin_name = $a_plugin['Name'];
229 if ( $plugin_key == $plugin )
230 $selected = " selected='selected'";
233 $plugin_name = esc_attr($plugin_name);
234 $plugin_key = esc_attr($plugin_key);
235 echo "\n\t<option value=\"$plugin_key\" $selected>$plugin_name</option>";
239 <?php submit_button( __( 'Select' ), '', 'Submit', false ); ?>
245 <div id="templateside">
246 <h2><?php _e( 'Plugin Files' ); ?></h2>
250 foreach ( $plugin_files as $plugin_file ) :
251 // Get the extension of the file
252 if ( preg_match('/\.([^.]+)$/', $plugin_file, $matches) ) {
253 $ext = strtolower($matches[1]);
254 // If extension is not in the acceptable list, skip it
255 if ( !in_array( $ext, $editable_extensions ) )
258 // No extension found
262 <li<?php echo $file == $plugin_file ? ' class="highlight"' : ''; ?>><a href="plugin-editor.php?file=<?php echo urlencode( $plugin_file ) ?>&plugin=<?php echo urlencode( $plugin ) ?>"><?php echo $plugin_file ?></a></li>
266 <form name="template" id="template" action="plugin-editor.php" method="post">
267 <?php wp_nonce_field('edit-plugin_' . $file) ?>
268 <div><textarea cols="70" rows="25" name="newcontent" id="newcontent" aria-describedby="newcontent-description"><?php echo $content; ?></textarea>
269 <input type="hidden" name="action" value="update" />
270 <input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
271 <input type="hidden" name="plugin" value="<?php echo esc_attr($plugin) ?>" />
272 <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
274 <?php if ( !empty( $docs_select ) ) : ?>
275 <div id="documentation" class="hide-if-no-js"><label for="docs-list"><?php _e('Documentation:') ?></label> <?php echo $docs_select ?> <input type="button" class="button" value="<?php esc_attr_e( 'Look Up' ) ?> " onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'https://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&locale=<?php echo urlencode( get_user_locale() ) ?>&version=<?php echo urlencode( get_bloginfo( 'version' ) ) ?>&redirect=true'); }" /></div>
277 <?php if ( is_writeable($real_file) ) : ?>
278 <?php if ( in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) ) { ?>
279 <p><?php _e('<strong>Warning:</strong> Making changes to active plugins is not recommended. If your changes cause a fatal error, the plugin will be automatically deactivated.'); ?></p>
283 if ( isset($_GET['phperror']) ) {
284 echo "<input type='hidden' name='phperror' value='1' />";
285 submit_button( __( 'Update File and Attempt to Reactivate' ), 'primary', 'submit', false );
287 submit_button( __( 'Update File' ), 'primary', 'submit', false );
292 <p><em><?php _e('You need to make this file writable before you can save your changes. See <a href="https://codex.wordpress.org/Changing_File_Permissions">the Codex</a> for more information.'); ?></em></p>
297 <script type="text/javascript">
298 jQuery(document).ready(function($){
299 $('#template').submit(function(){ $('#scrollto').val( $('#newcontent').scrollTop() ); });
300 $('#newcontent').scrollTop( $('#scrollto').val() );
306 include(ABSPATH . "wp-admin/admin-footer.php");