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


Generated: Wed Aug 4 01:00:05 2021 Cross-referenced by PHPXref 0.7.1