[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/ -> options-permalink.php (source)

   1  <?php
   2  /**
   3   * Permalink Settings Administration Screen.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /** WordPress Administration Bootstrap */
  10  require_once  __DIR__ . '/admin.php';
  11  
  12  if ( ! 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  
  20  get_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 &#8212; 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  
  30  get_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  
  50  get_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 &#8220;category&#8221; and &#8220;tag&#8221; base names that will appear in archive URLs. For example, the page listing all posts in the &#8220;Uncategorized&#8221; 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/support/article/settings-permalinks-screen/">Documentation on Permalinks Settings</a>' ) . '</p>' .
  61                          '<p>' . __( '<a href="https://wordpress.org/support/article/using-permalinks/">Documentation on Using Permalinks</a>' ) . '</p>';
  62  
  63  if ( $is_nginx ) {
  64      $help_sidebar_content .= '<p>' . __( '<a href="https://wordpress.org/support/article/nginx/">Documentation on Nginx configuration</a>.' ) . '</p>';
  65  }
  66  
  67  $help_sidebar_content .= '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>';
  68  
  69  get_current_screen()->set_help_sidebar( $help_sidebar_content );
  70  unset( $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  $prefix      = '';
  77  $blog_prefix = '';
  78  if ( ! got_url_rewrite() ) {
  79      $prefix = '/index.php';
  80  }
  81  
  82  /*
  83   * In a subdirectory configuration of multisite, the `/blog` prefix is used by
  84   * default on the main site to avoid collisions with other sites created on that
  85   * network. If the `permalink_structure` option has been changed to remove this
  86   * base prefix, WordPress core can no longer account for the possible collision.
  87   */
  88  if ( is_multisite() && ! is_subdomain_install() && is_main_site() && 0 === strpos( $permalink_structure, '/blog/' ) ) {
  89      $blog_prefix = '/blog';
  90  }
  91  
  92  $category_base = get_option( 'category_base' );
  93  $tag_base      = get_option( 'tag_base' );
  94  
  95  $structure_updated        = false;
  96  $htaccess_update_required = false;
  97  
  98  if ( isset( $_POST['permalink_structure'] ) || isset( $_POST['category_base'] ) ) {
  99      check_admin_referer( 'update-permalink' );
 100  
 101      if ( isset( $_POST['permalink_structure'] ) ) {
 102          if ( isset( $_POST['selection'] ) && 'custom' !== $_POST['selection'] ) {
 103              $permalink_structure = $_POST['selection'];
 104          } else {
 105              $permalink_structure = $_POST['permalink_structure'];
 106          }
 107  
 108          if ( ! empty( $permalink_structure ) ) {
 109              $permalink_structure = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $permalink_structure ) );
 110              if ( $prefix && $blog_prefix ) {
 111                  $permalink_structure = $prefix . preg_replace( '#^/?index\.php#', '', $permalink_structure );
 112              } else {
 113                  $permalink_structure = $blog_prefix . $permalink_structure;
 114              }
 115          }
 116  
 117          $permalink_structure = sanitize_option( 'permalink_structure', $permalink_structure );
 118  
 119          $wp_rewrite->set_permalink_structure( $permalink_structure );
 120  
 121          $structure_updated = true;
 122      }
 123  
 124      if ( isset( $_POST['category_base'] ) ) {
 125          $category_base = $_POST['category_base'];
 126  
 127          if ( ! empty( $category_base ) ) {
 128              $category_base = $blog_prefix . preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $category_base ) );
 129          }
 130  
 131          $wp_rewrite->set_category_base( $category_base );
 132      }
 133  
 134      if ( isset( $_POST['tag_base'] ) ) {
 135          $tag_base = $_POST['tag_base'];
 136  
 137          if ( ! empty( $tag_base ) ) {
 138              $tag_base = $blog_prefix . preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $tag_base ) );
 139          }
 140  
 141          $wp_rewrite->set_tag_base( $tag_base );
 142      }
 143  }
 144  
 145  if ( $iis7_permalinks ) {
 146      if ( ( ! file_exists( $home_path . 'web.config' ) && win_is_writable( $home_path ) ) || win_is_writable( $home_path . 'web.config' ) ) {
 147          $writable = true;
 148      } else {
 149          $writable = false;
 150      }
 151  } elseif ( $is_nginx ) {
 152      $writable = false;
 153  } else {
 154      if ( ( ! file_exists( $home_path . '.htaccess' ) && is_writable( $home_path ) ) || is_writable( $home_path . '.htaccess' ) ) {
 155          $writable = true;
 156      } else {
 157          $writable       = false;
 158          $existing_rules = array_filter( extract_from_markers( $home_path . '.htaccess', 'WordPress' ) );
 159          $new_rules      = array_filter( explode( "\n", $wp_rewrite->mod_rewrite_rules() ) );
 160  
 161          $htaccess_update_required = ( $new_rules !== $existing_rules );
 162      }
 163  }
 164  
 165  $using_index_permalinks = $wp_rewrite->using_index_permalinks();
 166  
 167  if ( $structure_updated ) {
 168      $message = __( 'Permalink structure updated.' );
 169  
 170      if ( ! is_multisite() && $permalink_structure && ! $using_index_permalinks ) {
 171          if ( $iis7_permalinks ) {
 172              if ( ! $writable ) {
 173                  $message = sprintf(
 174                      /* translators: %s: web.config */
 175                      __( 'You should update your %s file now.' ),
 176                      '<code>web.config</code>'
 177                  );
 178              } else {
 179                  $message = sprintf(
 180                      /* translators: %s: web.config */
 181                      __( 'Permalink structure updated. Remove write access on %s file now!' ),
 182                      '<code>web.config</code>'
 183                  );
 184              }
 185          } elseif ( ! $is_nginx && $htaccess_update_required && ! $writable ) {
 186              $message = sprintf(
 187                  /* translators: %s: .htaccess */
 188                  __( 'You should update your %s file now.' ),
 189                  '<code>.htaccess</code>'
 190              );
 191          }
 192      }
 193  
 194      if ( ! get_settings_errors() ) {
 195          add_settings_error( 'general', 'settings_updated', $message, 'success' );
 196      }
 197  
 198      set_transient( 'settings_errors', get_settings_errors(), 30 );
 199  
 200      wp_redirect( admin_url( 'options-permalink.php?settings-updated=true' ) );
 201      exit;
 202  }
 203  
 204  flush_rewrite_rules();
 205  
 206  require_once ABSPATH . 'wp-admin/admin-header.php';
 207  ?>
 208  <div class="wrap">
 209  <h1><?php echo esc_html( $title ); ?></h1>
 210  
 211  <form name="form" action="options-permalink.php" method="post">
 212  <?php wp_nonce_field( 'update-permalink' ); ?>
 213  
 214      <p>
 215      <?php
 216          printf(
 217              /* translators: %s: Documentation URL. */
 218              __( '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.' ),
 219              __( 'https://wordpress.org/support/article/using-permalinks/' )
 220          );
 221          ?>
 222      </p>
 223  
 224  <?php
 225  if ( is_multisite() && ! is_subdomain_install() && is_main_site() && 0 === strpos( $permalink_structure, '/blog/' ) ) {
 226      $permalink_structure = preg_replace( '|^/?blog|', '', $permalink_structure );
 227      $category_base       = preg_replace( '|^/?blog|', '', $category_base );
 228      $tag_base            = preg_replace( '|^/?blog|', '', $tag_base );
 229  }
 230  
 231  $structures = array(
 232      0 => '',
 233      1 => $prefix . '/%year%/%monthnum%/%day%/%postname%/',
 234      2 => $prefix . '/%year%/%monthnum%/%postname%/',
 235      3 => $prefix . '/' . _x( 'archives', 'sample permalink base' ) . '/%post_id%',
 236      4 => $prefix . '/%postname%/',
 237  );
 238  ?>
 239  <h2 class="title"><?php _e( 'Common Settings' ); ?></h2>
 240  <table class="form-table permalink-structure">
 241      <tr>
 242          <th scope="row"><label><input name="selection" type="radio" value="" <?php checked( '', $permalink_structure ); ?> /> <?php _e( 'Plain' ); ?></label></th>
 243          <td><code><?php echo get_option( 'home' ); ?>/?p=123</code></td>
 244      </tr>
 245      <tr>
 246          <th scope="row"><label><input name="selection" type="radio" value="<?php echo esc_attr( $structures[1] ); ?>" <?php checked( $structures[1], $permalink_structure ); ?> /> <?php _e( 'Day and name' ); ?></label></th>
 247          <td><code><?php echo get_option( 'home' ) . $blog_prefix . $prefix . '/' . gmdate( 'Y' ) . '/' . gmdate( 'm' ) . '/' . gmdate( 'd' ) . '/' . _x( 'sample-post', 'sample permalink structure' ) . '/'; ?></code></td>
 248      </tr>
 249      <tr>
 250          <th scope="row"><label><input name="selection" type="radio" value="<?php echo esc_attr( $structures[2] ); ?>" <?php checked( $structures[2], $permalink_structure ); ?> /> <?php _e( 'Month and name' ); ?></label></th>
 251          <td><code><?php echo get_option( 'home' ) . $blog_prefix . $prefix . '/' . gmdate( 'Y' ) . '/' . gmdate( 'm' ) . '/' . _x( 'sample-post', 'sample permalink structure' ) . '/'; ?></code></td>
 252      </tr>
 253      <tr>
 254          <th scope="row"><label><input name="selection" type="radio" value="<?php echo esc_attr( $structures[3] ); ?>" <?php checked( $structures[3], $permalink_structure ); ?> /> <?php _e( 'Numeric' ); ?></label></th>
 255          <td><code><?php echo get_option( 'home' ) . $blog_prefix . $prefix . '/' . _x( 'archives', 'sample permalink base' ) . '/123'; ?></code></td>
 256      </tr>
 257      <tr>
 258          <th scope="row"><label><input name="selection" type="radio" value="<?php echo esc_attr( $structures[4] ); ?>" <?php checked( $structures[4], $permalink_structure ); ?> /> <?php _e( 'Post name' ); ?></label></th>
 259          <td><code><?php echo get_option( 'home' ) . $blog_prefix . $prefix . '/' . _x( 'sample-post', 'sample permalink structure' ) . '/'; ?></code></td>
 260      </tr>
 261      <tr>
 262          <th scope="row">
 263              <label><input name="selection" id="custom_selection" type="radio" value="custom" <?php checked( ! in_array( $permalink_structure, $structures, true ) ); ?> />
 264              <?php _e( 'Custom Structure' ); ?>
 265              </label>
 266          </th>
 267          <td>
 268              <code><?php echo get_option( 'home' ) . $blog_prefix; ?></code>
 269              <input name="permalink_structure" id="permalink_structure" type="text" value="<?php echo esc_attr( $permalink_structure ); ?>" class="regular-text code" />
 270              <div class="available-structure-tags hide-if-no-js">
 271                  <div id="custom_selection_updated" aria-live="assertive" class="screen-reader-text"></div>
 272                  <?php
 273                  $available_tags = array(
 274                      /* translators: %s: Permalink structure tag. */
 275                      'year'     => __( '%s (The year of the post, four digits, for example 2004.)' ),
 276                      /* translators: %s: Permalink structure tag. */
 277                      'monthnum' => __( '%s (Month of the year, for example 05.)' ),
 278                      /* translators: %s: Permalink structure tag. */
 279                      'day'      => __( '%s (Day of the month, for example 28.)' ),
 280                      /* translators: %s: Permalink structure tag. */
 281                      'hour'     => __( '%s (Hour of the day, for example 15.)' ),
 282                      /* translators: %s: Permalink structure tag. */
 283                      'minute'   => __( '%s (Minute of the hour, for example 43.)' ),
 284                      /* translators: %s: Permalink structure tag. */
 285                      'second'   => __( '%s (Second of the minute, for example 33.)' ),
 286                      /* translators: %s: Permalink structure tag. */
 287                      'post_id'  => __( '%s (The unique ID of the post, for example 423.)' ),
 288                      /* translators: %s: Permalink structure tag. */
 289                      'postname' => __( '%s (The sanitized post title (slug).)' ),
 290                      /* translators: %s: Permalink structure tag. */
 291                      'category' => __( '%s (Category slug. Nested sub-categories appear as nested directories in the URL.)' ),
 292                      /* translators: %s: Permalink structure tag. */
 293                      'author'   => __( '%s (A sanitized version of the author name.)' ),
 294                  );
 295  
 296                  /**
 297                   * Filters the list of available permalink structure tags on the Permalinks settings page.
 298                   *
 299                   * @since 4.9.0
 300                   *
 301                   * @param string[] $available_tags An array of key => value pairs of available permalink structure tags.
 302                   */
 303                  $available_tags = apply_filters( 'available_permalink_structure_tags', $available_tags );
 304  
 305                  /* translators: %s: Permalink structure tag. */
 306                  $structure_tag_added = __( '%s added to permalink structure' );
 307  
 308                  /* translators: %s: Permalink structure tag. */
 309                  $structure_tag_already_used = __( '%s (already used in permalink structure)' );
 310  
 311                  if ( ! empty( $available_tags ) ) :
 312                      ?>
 313                      <p><?php _e( 'Available tags:' ); ?></p>
 314                      <ul role="list">
 315                          <?php
 316                          foreach ( $available_tags as $tag => $explanation ) {
 317                              ?>
 318                              <li>
 319                                  <button type="button"
 320                                          class="button button-secondary"
 321                                          aria-label="<?php echo esc_attr( sprintf( $explanation, $tag ) ); ?>"
 322                                          data-added="<?php echo esc_attr( sprintf( $structure_tag_added, $tag ) ); ?>"
 323                                          data-used="<?php echo esc_attr( sprintf( $structure_tag_already_used, $tag ) ); ?>">
 324                                      <?php echo '%' . $tag . '%'; ?>
 325                                  </button>
 326                              </li>
 327                              <?php
 328                          }
 329                          ?>
 330                      </ul>
 331                  <?php endif; ?>
 332              </div>
 333          </td>
 334      </tr>
 335  </table>
 336  
 337  <h2 class="title"><?php _e( 'Optional' ); ?></h2>
 338  <p>
 339  <?php
 340  /* translators: %s: Placeholder that must come at the start of the URL. */
 341  printf( __( '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.' ), get_option( 'home' ) . $blog_prefix . $prefix );
 342  ?>
 343  </p>
 344  
 345  <table class="form-table" role="presentation">
 346      <tr>
 347          <th><label for="category_base"><?php /* translators: Prefix for category permalinks. */ _e( 'Category base' ); ?></label></th>
 348          <td><?php echo $blog_prefix; ?> <input name="category_base" id="category_base" type="text" value="<?php echo esc_attr( $category_base ); ?>" class="regular-text code" /></td>
 349      </tr>
 350      <tr>
 351          <th><label for="tag_base"><?php _e( 'Tag base' ); ?></label></th>
 352          <td><?php echo $blog_prefix; ?> <input name="tag_base" id="tag_base" type="text" value="<?php echo esc_attr( $tag_base ); ?>" class="regular-text code" /></td>
 353      </tr>
 354      <?php do_settings_fields( 'permalink', 'optional' ); ?>
 355  </table>
 356  
 357  <?php do_settings_sections( 'permalink' ); ?>
 358  
 359  <?php submit_button(); ?>
 360  </form>
 361  <?php if ( ! is_multisite() ) { ?>
 362      <?php
 363      if ( $iis7_permalinks ) :
 364          if ( isset( $_POST['submit'] ) && $permalink_structure && ! $using_index_permalinks && ! $writable ) :
 365              if ( file_exists( $home_path . 'web.config' ) ) :
 366                  ?>
 367  <p id="iis-description-a">
 368                  <?php
 369                  printf(
 370                      /* translators: 1: web.config, 2: Documentation URL, 3: CTRL + a, 4: Element code. */
 371                      __( 'If your %1$s file was <a href="%2$s">writable</a>, we could do this automatically, but it isn&#8217;t so this is the url rewrite rule you should have in your %1$s file. Click in the field and press %3$s to select all. Then insert this rule inside of the %4$s element in %1$s file.' ),
 372                      '<code>web.config</code>',
 373                      __( 'https://wordpress.org/support/article/changing-file-permissions/' ),
 374                      '<kbd>CTRL + a</kbd>',
 375                      '<code>/&lt;configuration&gt;/&lt;system.webServer&gt;/&lt;rewrite&gt;/&lt;rules&gt;</code>'
 376                  );
 377                  ?>
 378  </p>
 379  <form action="options-permalink.php" method="post">
 380                  <?php wp_nonce_field( 'update-permalink' ); ?>
 381      <p><label for="rules"><?php _e( 'Rewrite rules:' ); ?></label><br /><textarea rows="9" class="large-text readonly" name="rules" id="rules" readonly="readonly" aria-describedby="iis-description-a"><?php echo esc_textarea( $wp_rewrite->iis7_url_rewrite_rules() ); ?></textarea></p>
 382  </form>
 383  <p>
 384                  <?php
 385                  printf(
 386                      /* translators: %s: web.config */
 387                      __( 'If you temporarily make your %s file writable for us to generate rewrite rules automatically, do not forget to revert the permissions after rule has been saved.' ),
 388                      '<code>web.config</code>'
 389                  );
 390                  ?>
 391  </p>
 392          <?php else : ?>
 393  <p id="iis-description-b">
 394              <?php
 395              printf(
 396                  /* translators: 1: Documentation URL, 2: web.config, 3: CTRL + a */
 397                  __( 'If the root directory of your site was <a href="%1$s">writable</a>, we could do this automatically, but it isn&#8217;t so 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 to select all. Then insert this code into the %2$s file.' ),
 398                  __( 'https://wordpress.org/support/article/changing-file-permissions/' ),
 399                  '<code>web.config</code>',
 400                  '<kbd>CTRL + a</kbd>'
 401              );
 402              ?>
 403  </p>
 404  <form action="options-permalink.php" method="post">
 405              <?php wp_nonce_field( 'update-permalink' ); ?>
 406      <p><label for="rules"><?php _e( 'Rewrite rules:' ); ?></label><br /><textarea rows="18" class="large-text readonly" name="rules" id="rules" readonly="readonly" aria-describedby="iis-description-b"><?php echo esc_textarea( $wp_rewrite->iis7_url_rewrite_rules( true ) ); ?></textarea></p>
 407  </form>
 408  <p>
 409              <?php
 410              printf(
 411                  /* translators: %s: web.config */
 412                  __( 'If you temporarily make your site&#8217;s root directory writable for us to generate the %s file automatically, do not forget to revert the permissions after the file has been created.' ),
 413                  '<code>web.config</code>'
 414              );
 415              ?>
 416  </p>
 417          <?php endif; ?>
 418      <?php endif; ?>
 419          <?php
 420  else :
 421      if ( $permalink_structure && ! $using_index_permalinks && ! $writable && $htaccess_update_required ) :
 422          ?>
 423  <p id="htaccess-description">
 424          <?php
 425          printf(
 426              /* translators: 1: .htaccess, 2: Documentation URL, 3: CTRL + a */
 427              __( 'If your %1$s file was <a href="%2$s">writable</a>, we could do this automatically, but it isn&#8217;t so these are the mod_rewrite rules you should have in your %1$s file. Click in the field and press %3$s to select all.' ),
 428              '<code>.htaccess</code>',
 429              __( 'https://wordpress.org/support/article/changing-file-permissions/' ),
 430              '<kbd>CTRL + a</kbd>'
 431          );
 432          ?>
 433  </p>
 434  <form action="options-permalink.php" method="post">
 435          <?php wp_nonce_field( 'update-permalink' ); ?>
 436      <p><label for="rules"><?php _e( 'Rewrite rules:' ); ?></label><br /><textarea rows="8" class="large-text readonly" name="rules" id="rules" readonly="readonly" aria-describedby="htaccess-description"><?php echo esc_textarea( $wp_rewrite->mod_rewrite_rules() ); ?></textarea></p>
 437  </form>
 438      <?php endif; ?>
 439  <?php endif; ?>
 440  <?php } // End if ! is_multisite(). ?>
 441  
 442  </div>
 443  
 444  <?php require_once ABSPATH . 'wp-admin/admin-footer.php'; ?>


Generated: Mon Oct 25 01:00:03 2021 Cross-referenced by PHPXref 0.7.1