[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Tue Oct 20 08:20:01 2020 Cross-referenced by PHPXref