[wp-trac] [WordPress Trac] #51805: Clarify the prerequisites for updating the .htaccess file in flush_rewrite_rules()

WordPress Trac noreply at wordpress.org
Fri Mar 4 20:32:41 UTC 2022


#51805: Clarify the prerequisites for updating the .htaccess file in
flush_rewrite_rules()
----------------------------+-----------------------------
 Reporter:  SergeyBiryukov  |       Owner:  (none)
     Type:  defect (bug)    |      Status:  new
 Priority:  normal          |   Milestone:  Future Release
Component:  Rewrite Rules   |     Version:
 Severity:  normal          |  Resolution:
 Keywords:                  |     Focuses:  docs
----------------------------+-----------------------------

Comment (by bwmarkle):

 +1

 Usually it's Stack Overflow that answers all my questions, but today it
 was this trac ticket!

 I'm creating a REST call to allow changing of a siteurl, and
 flush_rewrite_rules() wasn't working as expected. I ended up writing a
 wrapper function.

 {{{#!php
 <?php
 /**
  * A wrapper for WordPress' flush_rewrite_rules.
  *
  * Wrapper function is necessary because rewriting the .htaccess only
 works if the
  * save_mod_rewrite_rules() function exists, which only does in admin.
 This method ensures it's
  * there.
  *
  * @link https://core.trac.wordpress.org/ticket/51805
  *
  * @param bool $hard Whether to update .htaccess (hard flush) or just
 update rewrite_rules option
  *                   (soft flush).
  */
 public static function flush_rewrite_rules( $hard = true ) {
         // A requirement for rewriting the .htaccess file.
         if ( $hard && ! function_exists( 'save_mod_rewrite_rules' ) ) {
                 require_once ABSPATH . 'wp-admin/includes/misc.php';
         }

         flush_rewrite_rules( $hard );
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51805#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list