1<?php
2/**
3 * Permalink Settings Administration Screen.
4 *
5 * @package WordPress
6 * @subpackage Administration
7 */
8
9/** WordPress Administration Bootstrap */
10require_once __DIR__ . '/admin.php';
11
12if ( ! current_user_can( 'manage_options' ) ) {
13 wp_die( __( 'Sorry, you are not allowed to manage options for this site.' ) );
14}
15
16// Used in the HTML title tag.
17$title = __( 'Permalink Settings' );
18$parent_file = 'options-general.php';
19
20get_current_screen()->add_help_tab(
21 array(
22 'id' => 'overview',
23 'title' => __( 'Overview' ),
24 'content' => '<p>' . __( 'Permalinks are the permanent URLs to your individual pages and blog posts, as well as your category and tag archives. A permalink is the web address used to link to your content. The URL to each post should be permanent, and never change — hence the name permalink.' ) . '</p>' .
25 '<p>' . __( 'This screen allows you to choose your permalink structure. You can choose from common settings or create custom URL structures.' ) . '</p>' .
26 '<p>' . __( 'You must click the Save Changes button at the bottom of the screen for new settings to take effect.' ) . '</p>',
27 )
28);
29
30get_current_screen()->add_help_tab(
31 array(
32 'id' => 'permalink-settings',
33 'title' => __( 'Permalink Settings' ),
34 'content' => '<p>' . __( 'Permalinks can contain useful information, such as the post date, title, or other elements. You can choose from any of the suggested permalink formats, or you can craft your own if you select Custom Structure.' ) . '</p>' .
35 '<p>' . sprintf(
36 /* translators: %s: Percent sign (%). */
37 __( 'If you pick an option other than Plain, your general URL path with structure tags (terms surrounded by %s) will also appear in the custom structure field and your path can be further modified there.' ),
38 '<code>%</code>'
39 ) . '</p>' .
40 '<p>' . sprintf(
41 /* translators: 1: %category%, 2: %tag% */
42 __( 'When you assign multiple categories or tags to a post, only one can show up in the permalink: the lowest numbered category. This applies if your custom structure includes %1$s or %2$s.' ),
43 '<code>%category%</code>',
44 '<code>%tag%</code>'
45 ) . '</p>' .
46 '<p>' . __( 'You must click the Save Changes button at the bottom of the screen for new settings to take effect.' ) . '</p>',
47 )
48);
49
50get_current_screen()->add_help_tab(
51 array(
52 'id' => 'custom-structures',
53 'title' => __( 'Custom Structures' ),
54 'content' => '<p>' . __( 'The Optional fields let you customize the “category” and “tag” base names that will appear in archive URLs. For example, the page listing all posts in the “Uncategorized” category could be <code>/topics/uncategorized</code> instead of <code>/category/uncategorized</code>.' ) . '</p>' .
55 '<p>' . __( 'You must click the Save Changes button at the bottom of the screen for new settings to take effect.' ) . '</p>',
56 )
57);
58
59$help_sidebar_content = '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
60 '<p>' . __( '<a href="https://wordpress.org/documentation/article/settings-permalinks-screen/">Documentation on Permalinks Settings</a>' ) . '</p>' .
61 '<p>' . __( '<a href="https://wordpress.org/documentation/article/customize-permalinks/">Documentation on Using Permalinks</a>' ) . '</p>';
62
63if ( $is_nginx ) {
64 $help_sidebar_content .= '<p>' . __( '<a href="https://developer.wordpress.org/advanced-administration/server/web-server/nginx/">Documentation on Nginx configuration</a>.' ) . '</p>';
65}
66
67$help_sidebar_content .= '<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>';
68
69get_current_screen()->set_help_sidebar( $help_sidebar_content );
70unset( $help_sidebar_content );
71
72$home_path = get_home_path();
73$iis7_permalinks = iis7_supports_permalinks();
74$permalink_structure = get_option( 'permalink_structure' );
75
76$index_php_prefix = '';
77$blog_prefix = '';
78
79if ( ! got_url_rewrite() ) {
80 $index_php_prefix = '/index.php';
81}
82
83/*
84 * In a subdirectory configuration of multisite, the `/blog` prefix is used by
85 * default on the main site to avoid collisions with other sites created on that
86 * network. If the `permalink_structure` option has been changed to remove this
87 * base prefix, WordPress core can no longer account for the possible collision.
88 */
89if ( is_multisite() && ! is_subdomain_install() && is_main_site()
90 && str_starts_with( $permalink_structure, '/blog/' )
91) {
92 $blog_prefix = '/blog';
93}
94
95$category_base = get_option( 'category_base' );
96$tag_base = get_option( 'tag_base' );
97
98$structure_updated = false;
99$htaccess_update_required = false;
100
101if ( isset( $_POST['permalink_structure'] ) || isset( $_POST['category_base'] ) ) {
102 check_admin_referer( 'update-permalink' );
103
104 if ( isset( $_POST['permalink_structure'] ) ) {
105 if ( isset( $_POST['selection'] ) && 'custom' !== $_POST['selection'] ) {
106 $permalink_structure = $_POST['selection'];
107 } else {
108 $permalink_structure = $_POST['permalink_structure'];
109 }
110
111 if ( ! empty( $permalink_structure ) ) {
112 $permalink_structure = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $permalink_structure ) );
113
114 if ( $index_php_prefix && $blog_prefix ) {
115 $permalink_structure = $index_php_prefix . preg_replace( '#^/?index\.php#', '', $permalink_structure );
116 } else {
117 $permalink_structure = $blog_prefix . $permalink_structure;
118 }
119 }
120
121 $permalink_structure = sanitize_option( 'permalink_structure', $permalink_structure );
122
123 $wp_rewrite->set_permalink_structure( $permalink_structure );
124
125 $structure_updated = true;
126 }
127
128 if ( isset( $_POST['category_base'] ) ) {
129 $category_base = $_POST['category_base'];
130
131 if ( ! empty( $category_base ) ) {
132 $category_base = $blog_prefix . preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $category_base ) );
133 }
134
135 $wp_rewrite->set_category_base( $category_base );
136 }
137
138 if ( isset( $_POST['tag_base'] ) ) {
139 $tag_base = $_POST['tag_base'];
140
141 if ( ! empty( $tag_base ) ) {
142 $tag_base = $blog_prefix . preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $tag_base ) );
143 }
144
145 $wp_rewrite->set_tag_base( $tag_base );
146 }
147}
148
149if ( $iis7_permalinks ) {
150 if ( ( ! file_exists( $home_path . 'web.config' )
151 && win_is_writable( $home_path ) ) || win_is_writable( $home_path . 'web.config' )
152 ) {
153 $writable = true;
154 } else {
155 $writable = false;
156 }
157} elseif ( $is_nginx || $is_caddy ) {
158 $writable = false;
159} else {
160 if ( ( ! file_exists( $home_path . '.htaccess' )
161 && is_writable( $home_path ) ) || is_writable( $home_path . '.htaccess' )
162 ) {
163 $writable = true;
164 } else {
165 $writable = false;
166 $existing_rules = array_filter( extract_from_markers( $home_path . '.htaccess', 'WordPress' ) );
167 $new_rules = array_filter( explode( "\n", $wp_rewrite->mod_rewrite_rules() ) );
168
169 $htaccess_update_required = ( $new_rules !== $existing_rules );
170 }
171}
172
173$using_index_permalinks = $wp_rewrite->using_index_permalinks();
174
175if ( $structure_updated ) {
176 $message = __( 'Permalink structure updated.' );
177
178 if ( ! is_multisite() && $permalink_structure && ! $using_index_permalinks ) {
179 if ( $iis7_permalinks ) {
180 if ( ! $writable ) {
181 $message = sprintf(
182 /* translators: %s: web.config */
183 __( 'You should update your %s file now.' ),
184 '<code>web.config</code>'
185 );
186 } else {
187 $message = sprintf(
188 /* translators: %s: web.config */
189 __( 'Permalink structure updated. Remove write access on %s file now!' ),
190 '<code>web.config</code>'
191 );
192 }
193 } elseif ( ! $is_nginx && ! $is_caddy && $htaccess_update_required && ! $writable ) {
194 $message = sprintf(
195 /* translators: %s: .htaccess */
196 __( 'You should update your %s file now.' ),
197 '<code>.htaccess</code>'
198 );
199 }
200 }
201
202 if ( ! get_settings_errors() ) {
203 add_settings_error( 'general', 'settings_updated', $message, 'success' );
204 }
205
206 set_transient( 'settings_errors', get_settings_errors(), 30 ); // 30 seconds.
207
208 wp_redirect( admin_url( 'options-permalink.php?settings-updated=true' ) );
209 exit;
210}
211
212flush_rewrite_rules();
213
214require_once ABSPATH . 'wp-admin/admin-header.php';
215?>
216<div class="wrap">
217<h1><?php echo esc_html( $title ); ?></h1>
218
219<form name="form" action="options-permalink.php" method="post">
220<?php wp_nonce_field( 'update-permalink' ); ?>
221
222<p>
223<?php
224printf(
225 /* translators: %s: Documentation URL. */
226 __( 'WordPress offers you the ability to create a custom URL structure for your permalinks and archives. Custom URL structures can improve the aesthetics, usability, and forward-compatibility of your links. A <a href="%s">number of tags are available</a>, and here are some examples to get you started.' ),
227 __( 'https://wordpress.org/documentation/article/customize-permalinks/' )
228);
229?>
230</p>
231
232<?php
233if ( is_multisite() && ! is_subdomain_install() && is_main_site()
234 && str_starts_with( $permalink_structure, '/blog/' )
235) {
236 $permalink_structure = preg_replace( '|^/?blog|', '', $permalink_structure );
237 $category_base = preg_replace( '|^/?blog|', '', $category_base );
238 $tag_base = preg_replace( '|^/?blog|', '', $tag_base );
239}
240
241$url_base = home_url( $blog_prefix . $index_php_prefix );
242
243$default_structures = array(
244 array(
245 'id' => 'plain',
246 'label' => __( 'Plain' ),
247 'value' => '',
248 'example' => home_url( '/?p=123' ),
249 ),
250 array(
251 'id' => 'day-name',
252 'label' => __( 'Day and name' ),
253 'value' => $index_php_prefix . '/%year%/%monthnum%/%day%/%postname%/',
254 'example' => $url_base . '/' . gmdate( 'Y/m/d' ) . '/' . _x( 'sample-post', 'sample permalink structure' ) . '/',
255 ),
256 array(
257 'id' => 'month-name',
258 'label' => __( 'Month and name' ),
259 'value' => $index_php_prefix . '/%year%/%monthnum%/%postname%/',
260 'example' => $url_base . '/' . gmdate( 'Y/m' ) . '/' . _x( 'sample-post', 'sample permalink structure' ) . '/',
261 ),
262 array(
263 'id' => 'numeric',
264 'label' => __( 'Numeric' ),
265 'value' => $index_php_prefix . '/' . _x( 'archives', 'sample permalink base' ) . '/%post_id%',
266 'example' => $url_base . '/' . _x( 'archives', 'sample permalink base' ) . '/123',
267 ),
268 array(
269 'id' => 'post-name',
270 'label' => __( 'Post name' ),
271 'value' => $index_php_prefix . '/%postname%/',
272 'example' => $url_base . '/' . _x( 'sample-post', 'sample permalink structure' ) . '/',
273 ),
274);
275
276$default_structure_values = wp_list_pluck( $default_structures, 'value' );
277
278$available_tags = array(
279 /* translators: %s: Permalink structure tag. */
280 'year' => __( '%s (The year of the post, four digits, for example 2004.)' ),
281 /* translators: %s: Permalink structure tag. */
282 'monthnum' => __( '%s (Month of the year, for example 05.)' ),
283 /* translators: %s: Permalink structure tag. */
284 'day' => __( '%s (Day of the month, for example 28.)' ),
285 /* translators: %s: Permalink structure tag. */
286 'hour' => __( '%s (Hour of the day, for example 15.)' ),
287 /* translators: %s: Permalink structure tag. */
288 'minute' => __( '%s (Minute of the hour, for example 43.)' ),
289 /* translators: %s: Permalink structure tag. */
290 'second' => __( '%s (Second of the minute, for example 33.)' ),
291 /* translators: %s: Permalink structure tag. */
292 'post_id' => __( '%s (The unique ID of the post, for example 423.)' ),
293 /* translators: %s: Permalink structure tag. */
294 'postname' => __( '%s (The sanitized post title (slug).)' ),
295 /* translators: %s: Permalink structure tag. */
296 'category' => __( '%s (Category slug. Nested sub-categories appear as nested directories in the URL.)' ),
297 /* translators: %s: Permalink structure tag. */
298 'author' => __( '%s (A sanitized version of the author name.)' ),
299);
300
301/**
302 * Filters the list of available permalink structure tags on the Permalinks settings page.
303 *
304 * @since 4.9.0
305 *
306 * @param string[] $available_tags An array of key => value pairs of available permalink structure tags.
307 */
308$available_tags = apply_filters( 'available_permalink_structure_tags', $available_tags );
309
310/* translators: %s: Permalink structure tag. */
311$tag_added = __( '%s added to permalink structure' );
312/* translators: %s: Permalink structure tag. */
313$tag_removed = __( '%s removed from permalink structure' );
314/* translators: %s: Permalink structure tag. */
315$tag_already_used = __( '%s (already used in permalink structure)' );
316?>
317<h2 class="title"><?php _e( 'Common Settings' ); ?></h2>
318<p>
319<?php
320printf(
321 /* translators: %s: %postname% */
322 __( 'Select the permalink structure for your website. Including the %s tag makes links easy to understand, and can help your posts rank higher in search engines.' ),
323 '<code>%postname%</code>'
324);
325?>
326</p>
327<table class="form-table permalink-structure" role="presentation">
328<tbody>
329<?php $permalink_structure_title = __( 'Permalink structure' ); ?>
330<tr>
331 <th scope="row"><?php echo $permalink_structure_title; ?></th>
332 <td>
333 <fieldset class="structure-selection">
334 <legend class="screen-reader-text"><?php echo $permalink_structure_title; ?></legend>
335 <?php foreach ( $default_structures as $input ) : ?>
336 <div class="row">
337 <input id="permalink-input-<?php echo esc_attr( $input['id'] ); ?>"
338 name="selection" aria-describedby="permalink-<?php echo esc_attr( $input['id'] ); ?>"
339 type="radio" value="<?php echo esc_attr( $input['value'] ); ?>"
340 <?php checked( $input['value'], $permalink_structure ); ?>
341 />
342 <div>
343 <label for="permalink-input-<?php echo esc_attr( $input['id'] ); ?>">
344 <?php echo esc_html( $input['label'] ); ?>
345 </label>
346 <p>
347 <code id="permalink-<?php echo esc_attr( $input['id'] ); ?>">
348 <?php echo esc_html( $input['example'] ); ?>
349 </code>
350 </p>
351 </div>
352 </div><!-- .row -->
353 <?php endforeach; ?>
354
355 <div class="row">
356 <input id="custom_selection"
357 name="selection" type="radio" value="custom"
358 <?php checked( ! in_array( $permalink_structure, $default_structure_values, true ) ); ?>
359 />
360 <div>
361 <label for="custom_selection"><?php _e( 'Custom Structure' ); ?></label>
362 <p>
363 <label for="permalink_structure" class="screen-reader-text">
364 <?php
365 /* translators: Hidden accessibility text. */
366 _e( 'Customize permalink structure by selecting available tags' );
367 ?>
368 </label>
369 <span class="code">
370 <code id="permalink-custom"><?php echo esc_url( $url_base ); ?></code>
371 <input name="permalink_structure" id="permalink_structure"
372 type="text" value="<?php echo esc_attr( $permalink_structure ); ?>"
373 aria-describedby="permalink-custom" class="regular-text code"
374 />
375 </span>
376 </p>
377
378 <div class="available-structure-tags hide-if-no-js">
379 <div id="custom_selection_updated" aria-live="assertive" class="screen-reader-text"></div>
380 <?php if ( ! empty( $available_tags ) ) : ?>
381 <fieldset>
382 <legend><?php _e( 'Available tags:' ); ?></legend>
383 <ul role="list">
384 <?php foreach ( $available_tags as $tag => $explanation ) : ?>
385 <li>
386 <button type="button"
387 class="button button-secondary"
388 aria-label="<?php echo esc_attr( sprintf( $explanation, $tag ) ); ?>"
389 data-added="<?php echo esc_attr( sprintf( $tag_added, $tag ) ); ?>"
390 data-removed="<?php echo esc_attr( sprintf( $tag_removed, $tag ) ); ?>"
391 data-used="<?php echo esc_attr( sprintf( $tag_already_used, $tag ) ); ?>">
392 <?php echo '%' . esc_html( $tag ) . '%'; ?>
393 </button>
394 </li>
395 <?php endforeach; ?>
396 </ul>
397 </fieldset>
398 <?php endif; ?>
399 </div><!-- .available-structure-tags -->
400 </div>
401 </div><!-- .row -->
402 </fieldset><!-- .structure-selection -->
403 </td>
404</tr>
405</tbody>
406</table>
407
408<h2 class="title"><?php _e( 'Optional' ); ?></h2>
409<p>
410<?php
411printf(
412 /* translators: %s: Placeholder that must come at the start of the URL. */
413 __( 'If you like, you may enter custom structures for your category and tag URLs here. For example, using <code>topics</code> as your category base would make your category links like <code>%s/topics/uncategorized/</code>. If you leave these blank the defaults will be used.' ),
414 $url_base
415);
416?>
417</p>
418
419<table class="form-table" role="presentation">
420 <tr>
421 <th>
422 <label for="category_base">
423 <?php /* translators: Prefix for category permalinks. */ _e( 'Category base' ); ?>
424 </label>
425 </th>
426 <td>
427 <?php echo $blog_prefix; ?>
428 <input name="category_base" id="category_base" type="text"
429 value="<?php echo esc_attr( $category_base ); ?>" class="regular-text code"
430 />
431 </td>
432 </tr>
433 <tr>
434 <th>
435 <label for="tag_base"><?php _e( 'Tag base' ); ?></label>
436 </th>
437 <td>
438 <?php echo $blog_prefix; ?>
439 <input name="tag_base" id="tag_base" type="text"
440 value="<?php echo esc_attr( $tag_base ); ?>" class="regular-text code"
441 />
442 </td>
443 </tr>
444 <?php do_settings_fields( 'permalink', 'optional' ); ?>
445</table>
446
447<?php do_settings_sections( 'permalink' ); ?>
448
449<?php submit_button(); ?>
450</form>
451
452<?php if ( ! is_multisite() ) : ?>
453 <?php
454 if ( $iis7_permalinks ) :
455 if ( isset( $_POST['submit'] ) && $permalink_structure && ! $using_index_permalinks && ! $writable ) :
456 if ( file_exists( $home_path . 'web.config' ) ) :
457 ?>
458 <p id="iis-description-a">
459 <?php
460 printf(
461 /* translators: 1: web.config, 2: Documentation URL, 3: Ctrl + A, 4: ⌘ + A, 5: Element code. */
462 __( '<strong>Error:</strong> Your %1$s file is not <a href="%2$s">writable</a>, so updating it automatically was not possible. This is the URL rewrite rule you should have in your %1$s file. Click in the field and press %3$s (or %4$s on Mac) to select all. Then insert this rule inside of the %5$s element in %1$s file.' ),
463 '<code>web.config</code>',
464 __( 'https://developer.wordpress.org/advanced-administration/server/file-permissions/' ),
465 '<kbd>Ctrl + A</kbd>',
466 '<kbd>⌘ + A</kbd>',
467 '<code>/<configuration>/<system.webServer>/<rewrite>/<rules></code>'
468 );
469 ?>
470 </p>
471 <form action="options-permalink.php" method="post">
472 <?php wp_nonce_field( 'update-permalink' ); ?>
473 <p>
474 <label for="rules"><?php _e( 'Rewrite rules:' ); ?></label><br />
475 <textarea rows="9" class="large-text readonly"
476 name="rules" id="rules" readonly="readonly"
477 aria-describedby="iis-description-a"
478 ><?php echo esc_textarea( $wp_rewrite->iis7_url_rewrite_rules() ); ?></textarea>
479 </p>
480 </form>
481 <p>
482 <?php
483 printf(
484 /* translators: %s: web.config */
485 __( 'If you temporarily make your %s file writable to generate rewrite rules automatically, do not forget to revert the permissions after the rule has been saved.' ),
486 '<code>web.config</code>'
487 );
488 ?>
489 </p>
490 <?php else : ?>
491 <p id="iis-description-b">
492 <?php
493 printf(
494 /* translators: 1: Documentation URL, 2: web.config, 3: Ctrl + A, 4: ⌘ + A */
495 __( '<strong>Error:</strong> The root directory of your site is not <a href="%1$s">writable</a>, so creating a file automatically was not possible. This is the URL rewrite rule you should have in your %2$s file. Create a new file called %2$s in the root directory of your site. Click in the field and press %3$s (or %4$s on Mac) to select all. Then insert this code into the %2$s file.' ),
496 __( 'https://developer.wordpress.org/advanced-administration/server/file-permissions/' ),
497 '<code>web.config</code>',
498 '<kbd>Ctrl + A</kbd>',
499 '<kbd>⌘ + A</kbd>'
500 );
501 ?>
502 </p>
503 <form action="options-permalink.php" method="post">
504 <?php wp_nonce_field( 'update-permalink' ); ?>
505 <p>
506 <label for="rules"><?php _e( 'Rewrite rules:' ); ?></label><br />
507 <textarea rows="18" class="large-text readonly"
508 name="rules" id="rules" readonly="readonly"
509 aria-describedby="iis-description-b"
510 ><?php echo esc_textarea( $wp_rewrite->iis7_url_rewrite_rules( true ) ); ?></textarea>
511 </p>
512 </form>
513 <p>
514 <?php
515 printf(
516 /* translators: %s: web.config */
517 __( 'If you temporarily make your site’s root directory writable to generate the %s file automatically, do not forget to revert the permissions after the file has been created.' ),
518 '<code>web.config</code>'
519 );
520 ?>
521 </p>
522 <?php endif; // End if 'web.config' exists. ?>
523 <?php endif; // End if $_POST['submit'] && ! $writable. ?>
524 <?php else : ?>
525 <?php if ( $permalink_structure && ! $using_index_permalinks && ! $writable && $htaccess_update_required ) : ?>
526 <p id="htaccess-description">
527 <?php
528 printf(
529 /* translators: 1: .htaccess, 2: Documentation URL, 3: Ctrl + A, 4: ⌘ + A */
530 __( '<strong>Error:</strong> Your %1$s file is not <a href="%2$s">writable</a>, so updating it automatically was not possible. These are the mod_rewrite rules you should have in your %1$s file. Click in the field and press %3$s (or %4$s on Mac) to select all.' ),
531 '<code>.htaccess</code>',
532 __( 'https://developer.wordpress.org/advanced-administration/server/file-permissions/' ),
533 '<kbd>Ctrl + A</kbd>',
534 '<kbd>⌘ + A</kbd>'
535 );
536 ?>
537 </p>
538 <form action="options-permalink.php" method="post">
539 <?php wp_nonce_field( 'update-permalink' ); ?>
540 <p>
541 <label for="rules"><?php _e( 'Rewrite rules:' ); ?></label><br />
542 <textarea rows="8" class="large-text readonly"
543 name="rules" id="rules" readonly="readonly"
544 aria-describedby="htaccess-description"
545 ><?php echo esc_textarea( $wp_rewrite->mod_rewrite_rules() ); ?></textarea>
546 </p>
547 </form>
548 <?php endif; // End if ! $writable && $htaccess_update_required. ?>
549 <?php endif; // End if $iis7_permalinks. ?>
550<?php endif; // End if ! is_multisite(). ?>
551
552</div><!-- .wrap -->
553
554<?php require_once ABSPATH . 'wp-admin/admin-footer.php'; ?>
555