[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Theme file editor administration panel. 4 * 5 * @package WordPress 6 * @subpackage Administration 7 */ 8 9 /** WordPress Administration Bootstrap */ 10 require_once __DIR__ . '/admin.php'; 11 12 if ( is_multisite() && ! is_network_admin() ) { 13 wp_redirect( network_admin_url( 'theme-editor.php' ) ); 14 exit; 15 } 16 17 if ( ! current_user_can( 'edit_themes' ) ) { 18 wp_die( '<p>' . __( 'Sorry, you are not allowed to edit templates for this site.' ) . '</p>' ); 19 } 20 21 // Used in the HTML title tag. 22 $title = __( 'Edit Themes' ); 23 $parent_file = 'themes.php'; 24 25 get_current_screen()->add_help_tab( 26 array( 27 'id' => 'overview', 28 'title' => __( 'Overview' ), 29 'content' => 30 '<p>' . __( 'You can use the theme file editor to edit the individual CSS and PHP files which make up your theme.' ) . '</p>' . 31 '<p>' . __( 'Begin by choosing a theme to edit from the dropdown menu and clicking the Select button. A list then appears of the theme’s template files. Clicking once on any file name causes the file to appear in the large Editor box.' ) . '</p>' . 32 '<p>' . __( 'For PHP files, you can use the documentation dropdown to select from functions recognized in that file. Look Up takes you to a web page with reference material about that particular function.' ) . '</p>' . 33 '<p id="editor-keyboard-trap-help-1">' . __( 'When using a keyboard to navigate:' ) . '</p>' . 34 '<ul>' . 35 '<li id="editor-keyboard-trap-help-2">' . __( 'In the editing area, the Tab key enters a tab character.' ) . '</li>' . 36 '<li id="editor-keyboard-trap-help-3">' . __( 'To move away from this area, press the Esc key followed by the Tab key.' ) . '</li>' . 37 '<li id="editor-keyboard-trap-help-4">' . __( 'Screen reader users: when in forms mode, you may need to press the Esc key twice.' ) . '</li>' . 38 '</ul>' . 39 '<p>' . __( 'After typing in your edits, click Update File.' ) . '</p>' . 40 '<p>' . __( '<strong>Advice:</strong> Think very carefully about your site crashing if you are live-editing the theme currently in use.' ) . '</p>' . 41 '<p>' . sprintf( 42 /* translators: %s: Link to documentation on child themes. */ 43 __( 'Upgrading to a newer version of the same theme will override changes made here. To avoid this, consider creating a <a href="%s">child theme</a> instead.' ), 44 __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ) 45 ) . '</p>' . 46 ( is_network_admin() ? '<p>' . __( 'Any edits to files from this screen will be reflected on all sites in the network.' ) . '</p>' : '' ), 47 ) 48 ); 49 50 get_current_screen()->set_help_sidebar( 51 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' . 52 '<p>' . __( '<a href="https://developer.wordpress.org/themes/">Documentation on Theme Development</a>' ) . '</p>' . 53 '<p>' . __( '<a href="https://wordpress.org/documentation/article/appearance-theme-file-editor-screen/">Documentation on Editing Themes</a>' ) . '</p>' . 54 '<p>' . __( '<a href="https://developer.wordpress.org/advanced-administration/wordpress/edit-files/">Documentation on Editing Files</a>' ) . '</p>' . 55 '<p>' . __( '<a href="https://developer.wordpress.org/themes/basics/template-tags/">Documentation on Template Tags</a>' ) . '</p>' . 56 '<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>' 57 ); 58 59 $action = ! empty( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : ''; 60 $theme = ! empty( $_REQUEST['theme'] ) ? sanitize_text_field( $_REQUEST['theme'] ) : ''; 61 $file = ! empty( $_REQUEST['file'] ) ? sanitize_text_field( $_REQUEST['file'] ) : ''; 62 $error = ! empty( $_REQUEST['error'] ); 63 64 if ( $theme ) { 65 $stylesheet = $theme; 66 } else { 67 $stylesheet = get_stylesheet(); 68 } 69 70 $theme = wp_get_theme( $stylesheet ); 71 72 if ( ! $theme->exists() ) { 73 wp_die( __( 'The requested theme does not exist.' ) ); 74 } 75 76 if ( $theme->errors() && 'theme_no_stylesheet' === $theme->errors()->get_error_code() ) { 77 wp_die( __( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message() ); 78 } 79 80 $allowed_files = array(); 81 $style_files = array(); 82 83 $file_types = wp_get_theme_file_editable_extensions( $theme ); 84 85 foreach ( $file_types as $type ) { 86 switch ( $type ) { 87 case 'php': 88 $allowed_files += $theme->get_files( 'php', -1 ); 89 break; 90 case 'css': 91 $style_files = $theme->get_files( 'css', -1 ); 92 $allowed_files['style.css'] = $style_files['style.css']; 93 $allowed_files += $style_files; 94 break; 95 default: 96 $allowed_files += $theme->get_files( $type, -1 ); 97 break; 98 } 99 } 100 101 // Move functions.php and style.css to the top. 102 if ( isset( $allowed_files['functions.php'] ) ) { 103 $allowed_files = array( 'functions.php' => $allowed_files['functions.php'] ) + $allowed_files; 104 } 105 if ( isset( $allowed_files['style.css'] ) ) { 106 $allowed_files = array( 'style.css' => $allowed_files['style.css'] ) + $allowed_files; 107 } 108 109 if ( empty( $file ) ) { 110 $relative_file = 'style.css'; 111 $file = $allowed_files['style.css']; 112 } else { 113 $relative_file = wp_unslash( $file ); 114 $file = $theme->get_stylesheet_directory() . '/' . $relative_file; 115 } 116 117 validate_file_to_edit( $file, $allowed_files ); 118 119 // Handle fallback editing of file when JavaScript is not available. 120 $edit_error = null; 121 $posted_content = null; 122 123 if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) { 124 $r = wp_edit_theme_plugin_file( wp_unslash( $_POST ) ); 125 if ( is_wp_error( $r ) ) { 126 $edit_error = $r; 127 if ( check_ajax_referer( 'edit-theme_' . $stylesheet . '_' . $relative_file, 'nonce', false ) && isset( $_POST['newcontent'] ) ) { 128 $posted_content = wp_unslash( $_POST['newcontent'] ); 129 } 130 } else { 131 wp_redirect( 132 add_query_arg( 133 array( 134 'a' => 1, // This means "success" for some reason. 135 'theme' => $stylesheet, 136 'file' => $relative_file, 137 ), 138 admin_url( 'theme-editor.php' ) 139 ) 140 ); 141 exit; 142 } 143 } 144 145 $settings = array( 146 'codeEditor' => wp_enqueue_code_editor( compact( 'file' ) ), 147 ); 148 wp_enqueue_script( 'wp-theme-plugin-editor' ); 149 wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( 'jQuery( function( $ ) { wp.themePluginEditor.init( $( "#template" ), %s ); } )', wp_json_encode( $settings ) ) ); 150 wp_add_inline_script( 'wp-theme-plugin-editor', 'wp.themePluginEditor.themeOrPlugin = "theme";' ); 151 152 require_once ABSPATH . 'wp-admin/admin-header.php'; 153 154 update_recently_edited( $file ); 155 156 if ( ! is_file( $file ) ) { 157 $error = true; 158 } 159 160 $content = ''; 161 if ( ! empty( $posted_content ) ) { 162 $content = $posted_content; 163 } elseif ( ! $error && filesize( $file ) > 0 ) { 164 $f = fopen( $file, 'r' ); 165 $content = fread( $f, filesize( $file ) ); 166 167 if ( str_ends_with( $file, '.php' ) ) { 168 $functions = wp_doc_link_parse( $content ); 169 170 if ( ! empty( $functions ) ) { 171 $docs_select = '<select name="docs-list" id="docs-list">'; 172 $docs_select .= '<option value="">' . esc_html__( 'Function Name…' ) . '</option>'; 173 174 foreach ( $functions as $function ) { 175 $docs_select .= '<option value="' . esc_attr( $function ) . '">' . esc_html( $function ) . '()</option>'; 176 } 177 178 $docs_select .= '</select>'; 179 } 180 } 181 182 $content = esc_textarea( $content ); 183 } 184 185 $file_show = array_search( $file, array_filter( $allowed_files ), true ); 186 ?> 187 <div class="wrap"> 188 <h1><?php echo esc_html( $title ); ?></h1> 189 190 <?php 191 if ( isset( $_GET['a'] ) ) { 192 wp_admin_notice( 193 __( 'File edited successfully.' ), 194 array( 195 'id' => 'message', 196 'dismissible' => true, 197 'additional_classes' => array( 'updated' ), 198 ) 199 ); 200 } elseif ( is_wp_error( $edit_error ) ) { 201 $error_code = esc_html( $edit_error->get_error_message() ? $edit_error->get_error_message() : $edit_error->get_error_code() ); 202 $message = '<p>' . __( 'There was an error while trying to update the file. You may need to fix something and try updating again.' ) . '</p> 203 <pre>' . $error_code . '</pre>'; 204 wp_admin_notice( 205 $message, 206 array( 207 'type' => 'error', 208 'id' => 'message', 209 ) 210 ); 211 } 212 213 if ( preg_match( '/\.css$/', $file ) && ! wp_is_block_theme() && current_user_can( 'customize' ) ) { 214 $message = '<p><strong>' . __( 'Did you know?' ) . '</strong></p><p>' . sprintf( 215 /* translators: %s: Link to Custom CSS section in the Customizer. */ 216 __( 'There is no need to change your CSS here — you can edit and live preview CSS changes in the <a href="%s">built-in CSS editor</a>.' ), 217 esc_url( add_query_arg( 'autofocus[section]', 'custom_css', admin_url( 'customize.php' ) ) ) 218 ) . '</p>'; 219 wp_admin_notice( 220 $message, 221 array( 222 'type' => 'info', 223 'id' => 'message', 224 ) 225 ); 226 } 227 ?> 228 229 <div class="fileedit-sub"> 230 <div class="alignleft"> 231 <h2> 232 <?php 233 if ( wp_get_theme()->get( 'Name' ) === $theme->display( 'Name' ) ) { 234 /* translators: %s: Theme name. */ 235 printf( __( 'Editing %s (active)' ), '<strong>' . $theme->display( 'Name' ) . '</strong>' ); 236 } else { 237 /* translators: %s: Theme name. */ 238 printf( __( 'Editing %s (inactive)' ), '<strong>' . $theme->display( 'Name' ) . '</strong>' ); 239 } 240 ?> 241 </h2> 242 <?php 243 printf( 244 /* translators: %s: File path. */ 245 ' <span><strong>' . __( 'File: %s' ) . '</strong></span>', 246 esc_html( $file_show ) 247 ); 248 ?> 249 </div> 250 <div class="alignright"> 251 <form action="theme-editor.php" method="get"> 252 <label for="theme" id="theme-plugin-editor-selector"><?php _e( 'Select theme to edit:' ); ?> </label> 253 <select name="theme" id="theme"> 254 <?php 255 foreach ( wp_get_themes( array( 'errors' => null ) ) as $a_stylesheet => $a_theme ) { 256 if ( $a_theme->errors() && 'theme_no_stylesheet' === $a_theme->errors()->get_error_code() ) { 257 continue; 258 } 259 260 $selected = ( $a_stylesheet === $stylesheet ) ? ' selected="selected"' : ''; 261 echo "\n\t" . '<option value="' . esc_attr( $a_stylesheet ) . '"' . $selected . '>' . $a_theme->display( 'Name' ) . '</option>'; 262 } 263 ?> 264 </select> 265 <?php submit_button( __( 'Select' ), '', 'Submit', false ); ?> 266 </form> 267 </div> 268 <br class="clear" /> 269 </div> 270 271 <?php 272 if ( $theme->errors() ) { 273 wp_admin_notice( 274 '<strong>' . __( 'This theme is broken.' ) . '</strong> ' . $theme->errors()->get_error_message(), 275 array( 276 'additional_classes' => array( 'error' ), 277 ) 278 ); 279 } 280 ?> 281 282 <div id="templateside"> 283 <h2 id="theme-files-label"><?php _e( 'Theme Files' ); ?></h2> 284 <ul role="tree" aria-labelledby="theme-files-label"> 285 <?php if ( $theme->parent() ) : ?> 286 <li class="howto"> 287 <?php 288 printf( 289 /* translators: %s: Link to edit parent theme. */ 290 __( 'This child theme inherits templates from a parent theme, %s.' ), 291 sprintf( 292 '<a href="%s">%s</a>', 293 self_admin_url( 'theme-editor.php?theme=' . urlencode( $theme->get_template() ) ), 294 $theme->parent()->display( 'Name' ) 295 ) 296 ); 297 ?> 298 </li> 299 <?php endif; ?> 300 <li role="treeitem" tabindex="-1" aria-expanded="true" aria-level="1" aria-posinset="1" aria-setsize="1"> 301 <ul role="group"> 302 <?php wp_print_theme_file_tree( wp_make_theme_file_tree( $allowed_files ) ); ?> 303 </ul> 304 </li> 305 </ul> 306 </div> 307 308 <?php 309 if ( $error ) : 310 wp_admin_notice( 311 __( 'File does not exist! Please double check the name and try again.' ), 312 array( 313 'additional_classes' => array( 'error' ), 314 ) 315 ); 316 else : 317 ?> 318 <form name="template" id="template" action="theme-editor.php" method="post"> 319 <?php wp_nonce_field( 'edit-theme_' . $stylesheet . '_' . $relative_file, 'nonce' ); ?> 320 <div> 321 <label for="newcontent" id="theme-plugin-editor-label"><?php _e( 'Selected file content:' ); ?></label> 322 <textarea cols="70" rows="30" name="newcontent" id="newcontent" aria-describedby="editor-keyboard-trap-help-1 editor-keyboard-trap-help-2 editor-keyboard-trap-help-3 editor-keyboard-trap-help-4"><?php echo $content; ?></textarea> 323 <input type="hidden" name="action" value="update" /> 324 <input type="hidden" name="file" value="<?php echo esc_attr( $relative_file ); ?>" /> 325 <input type="hidden" name="theme" value="<?php echo esc_attr( $theme->get_stylesheet() ); ?>" /> 326 </div> 327 328 <?php if ( ! empty( $functions ) ) : ?> 329 <div id="documentation" class="hide-if-no-js"> 330 <label for="docs-list"><?php _e( 'Documentation:' ); ?></label> 331 <?php echo $docs_select; ?> 332 <input disabled id="docs-lookup" 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'); }" /> 333 </div> 334 <?php endif; ?> 335 336 <div> 337 <div class="editor-notices"> 338 <?php 339 if ( is_child_theme() && $theme->get_stylesheet() === get_template() ) : 340 $message = ( is_writable( $file ) ) ? '<strong>' . __( 'Caution:' ) . '</strong> ' : ''; 341 $message .= __( 'This is a file in your current parent theme.' ); 342 wp_admin_notice( 343 $message, 344 array( 345 'type' => 'warning', 346 'additional_classes' => array( 'inline' ), 347 ) 348 ); 349 endif; 350 ?> 351 </div> 352 <?php 353 if ( is_writable( $file ) ) { 354 ?> 355 <p class="submit"> 356 <?php submit_button( __( 'Update File' ), 'primary', 'submit', false ); ?> 357 <span class="spinner"></span> 358 </p> 359 <?php 360 } else { 361 ?> 362 <p> 363 <?php 364 printf( 365 /* translators: %s: Documentation URL. */ 366 __( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ), 367 __( 'https://developer.wordpress.org/advanced-administration/server/file-permissions/' ) 368 ); 369 ?> 370 </p> 371 <?php 372 } 373 ?> 374 </div> 375 376 <?php wp_print_file_editor_templates(); ?> 377 </form> 378 <?php 379 endif; // End if $error. 380 ?> 381 <br class="clear" /> 382 </div> 383 <?php 384 $dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ); 385 if ( ! in_array( 'theme_editor_notice', $dismissed_pointers, true ) ) { 386 // Get a back URL. 387 $referer = wp_get_referer(); 388 389 $excluded_referer_basenames = array( 'theme-editor.php', 'wp-login.php' ); 390 391 $return_url = admin_url( '/' ); 392 if ( $referer ) { 393 $referer_path = parse_url( $referer, PHP_URL_PATH ); 394 if ( is_string( $referer_path ) && ! in_array( basename( $referer_path ), $excluded_referer_basenames, true ) ) { 395 $return_url = $referer; 396 } 397 } 398 ?> 399 <div id="file-editor-warning" class="notification-dialog-wrap file-editor-warning hide-if-no-js hidden"> 400 <div class="notification-dialog-background"></div> 401 <div class="notification-dialog"> 402 <div class="file-editor-warning-content"> 403 <div class="file-editor-warning-message"> 404 <h1><?php _e( 'Heads up!' ); ?></h1> 405 <p> 406 <?php 407 _e( 'You appear to be making direct edits to your theme in the WordPress dashboard. It is not recommended! Editing your theme directly could break your site and your changes may be lost in future updates.' ); 408 ?> 409 </p> 410 <?php 411 if ( ! $theme->parent() ) { 412 echo '<p>'; 413 printf( 414 /* translators: %s: Link to documentation on child themes. */ 415 __( 'If you need to tweak more than your theme’s CSS, you might want to try <a href="%s">making a child theme</a>.' ), 416 esc_url( __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ) ) 417 ); 418 echo '</p>'; 419 } 420 ?> 421 <p><?php _e( 'If you decide to go ahead with direct edits anyway, use a file manager to create a copy with a new name and hang on to the original. That way, you can re-enable a functional version if something goes wrong.' ); ?></p> 422 </div> 423 <p> 424 <a class="button file-editor-warning-go-back" href="<?php echo esc_url( $return_url ); ?>"><?php _e( 'Go back' ); ?></a> 425 <button type="button" class="file-editor-warning-dismiss button button-primary"><?php _e( 'I understand' ); ?></button> 426 </p> 427 </div> 428 </div> 429 </div> 430 <?php 431 } // Editor warning notice. 432 433 require_once ABSPATH . 'wp-admin/admin-footer.php';
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Apr 26 08:20:01 2025 | Cross-referenced by PHPXref |