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


Generated: Wed Jul 17 01:00:03 2019 Cross-referenced by PHPXref 0.7.1