: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
$this->date_structure = '';
// The date permalink must have year, month, and day separated by slashes.
$endians = array( '%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%' );
$this->date_structure = '';
foreach ( $endians as $endian ) {
if ( str_contains( $this->permalink_structure, $endian ) ) {
if ( empty( $date_endian ) ) {
$date_endian = '%year%/%monthnum%/%day%';
* Do not allow the date tags and %post_id% to overlap in the permalink
* structure. If they do, move the date tags to $front/date/.
preg_match_all( '/%.+?%/', $this->permalink_structure, $tokens );
foreach ( (array) $tokens[0] as $token ) {
if ( '%post_id%' === $token && ( $tok_index <= 3 ) ) {
$front = $front . 'date/';
$this->date_structure = $front . $date_endian;
return $this->date_structure;
* Retrieves the year permalink structure without month and day.
* Gets the date permalink structure and strips out the month and day
* @return string|false Year permalink structure on success, false on failure.
public function get_year_permastruct() {
$structure = $this->get_date_permastruct();
if ( empty( $structure ) ) {
$structure = str_replace( '%monthnum%', '', $structure );
$structure = str_replace( '%day%', '', $structure );
$structure = preg_replace( '#/+#', '/', $structure );
* Retrieves the month permalink structure without day and with year.
* Gets the date permalink structure and strips out the day permalink
* structures. Keeps the year permalink structure.
* @return string|false Year/Month permalink structure on success, false on failure.
public function get_month_permastruct() {
$structure = $this->get_date_permastruct();
if ( empty( $structure ) ) {
$structure = str_replace( '%day%', '', $structure );
$structure = preg_replace( '#/+#', '/', $structure );
* Retrieves the day permalink structure with month and year.
* Keeps date permalink structure with all year, month, and day.
* @return string|false Year/Month/Day permalink structure on success, false on failure.
public function get_day_permastruct() {
return $this->get_date_permastruct();
* Retrieves the permalink structure for categories.
* If the category_base property has no value, then the category structure
* will have the front property value, followed by 'category', and finally
* '%category%'. If it does, then the root property will be used, along with
* the category_base property value.
* @return string|false Category permalink structure on success, false on failure.
public function get_category_permastruct() {
return $this->get_extra_permastruct( 'category' );
* Retrieves the permalink structure for tags.
* If the tag_base property has no value, then the tag structure will have
* the front property value, followed by 'tag', and finally '%tag%'. If it
* does, then the root property will be used, along with the tag_base
* @return string|false Tag permalink structure on success, false on failure.
public function get_tag_permastruct() {
return $this->get_extra_permastruct( 'post_tag' );
* Retrieves an extra permalink structure by name.
* @param string $name Permalink structure name.
* @return string|false Permalink structure string on success, false on failure.
public function get_extra_permastruct( $name ) {
if ( empty( $this->permalink_structure ) ) {
if ( isset( $this->extra_permastructs[ $name ] ) ) {
return $this->extra_permastructs[ $name ]['struct'];
* Retrieves the author permalink structure.
* The permalink structure is front property, author base, and finally
* '/%author%'. Will set the author_structure property and then return it
* without attempting to set the value again.
* @return string|false Author permalink structure on success, false on failure.
public function get_author_permastruct() {
if ( isset( $this->author_structure ) ) {
return $this->author_structure;
if ( empty( $this->permalink_structure ) ) {
$this->author_structure = '';
$this->author_structure = $this->front . $this->author_base . '/%author%';
return $this->author_structure;
* Retrieves the search permalink structure.
* The permalink structure is root property, search base, and finally
* '/%search%'. Will set the search_structure property and then return it
* without attempting to set the value again.
* @return string|false Search permalink structure on success, false on failure.
public function get_search_permastruct() {
if ( isset( $this->search_structure ) ) {
return $this->search_structure;
if ( empty( $this->permalink_structure ) ) {
$this->search_structure = '';
$this->search_structure = $this->root . $this->search_base . '/%search%';
return $this->search_structure;
* Retrieves the page permalink structure.
* The permalink structure is root property, and '%pagename%'. Will set the
* page_structure property and then return it without attempting to set the
* @return string|false Page permalink structure on success, false on failure.
public function get_page_permastruct() {
if ( isset( $this->page_structure ) ) {
return $this->page_structure;
if ( empty( $this->permalink_structure ) ) {
$this->page_structure = '';
$this->page_structure = $this->root . '%pagename%';
return $this->page_structure;
* Retrieves the feed permalink structure.
* The permalink structure is root property, feed base, and finally
* '/%feed%'. Will set the feed_structure property and then return it
* without attempting to set the value again.
* @return string|false Feed permalink structure on success, false on failure.
public function get_feed_permastruct() {
if ( isset( $this->feed_structure ) ) {
return $this->feed_structure;
if ( empty( $this->permalink_structure ) ) {
$this->feed_structure = '';
$this->feed_structure = $this->root . $this->feed_base . '/%feed%';
return $this->feed_structure;
* Retrieves the comment feed permalink structure.
* The permalink structure is root property, comment base property, feed
* base and finally '/%feed%'. Will set the comment_feed_structure property
* and then return it without attempting to set the value again.
* @return string|false Comment feed permalink structure on success, false on failure.
public function get_comment_feed_permastruct() {
if ( isset( $this->comment_feed_structure ) ) {
return $this->comment_feed_structure;
if ( empty( $this->permalink_structure ) ) {
$this->comment_feed_structure = '';
$this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%';
return $this->comment_feed_structure;
* Adds or updates existing rewrite tags (e.g. %postname%).
* If the tag already exists, replace the existing pattern and query for
* that tag, otherwise add the new tag.
* @see WP_Rewrite::$rewritecode
* @see WP_Rewrite::$rewritereplace
* @see WP_Rewrite::$queryreplace
* @param string $tag Name of the rewrite tag to add or update.
* @param string $regex Regular expression to substitute the tag for in rewrite rules.
* @param string $query String to append to the rewritten query. Must end in '='.
public function add_rewrite_tag( $tag, $regex, $query ) {
$position = array_search( $tag, $this->rewritecode, true );
if ( false !== $position && null !== $position ) {
$this->rewritereplace[ $position ] = $regex;
$this->queryreplace[ $position ] = $query;
$this->rewritecode[] = $tag;
$this->rewritereplace[] = $regex;
$this->queryreplace[] = $query;
* Removes an existing rewrite tag.
* @see WP_Rewrite::$rewritecode
* @see WP_Rewrite::$rewritereplace
* @see WP_Rewrite::$queryreplace
* @param string $tag Name of the rewrite tag to remove.
public function remove_rewrite_tag( $tag ) {
$position = array_search( $tag, $this->rewritecode, true );
if ( false !== $position && null !== $position ) {
unset( $this->rewritecode[ $position ] );
unset( $this->rewritereplace[ $position ] );
unset( $this->queryreplace[ $position ] );
* Generates rewrite rules from a permalink structure.
* The main WP_Rewrite function for building the rewrite rule list. The
* contents of the function is a mix of black magic and regular expressions,
* so best just ignore the contents and move to the parameters.
* @param string $permalink_structure The permalink structure.
* @param int $ep_mask Optional. Endpoint mask defining what endpoints are added to the structure.
* @param bool $paged Optional. Whether archive pagination rules should be added for the structure.
* @param bool $feed Optional. Whether feed rewrite rules should be added for the structure.
* @param bool $forcomments Optional. Whether the feed rules should be a query for a comments feed.
* @param bool $walk_dirs Optional. Whether the 'directories' making up the structure should be walked
* over and rewrite rules built for each in-turn. Default true.
* @param bool $endpoints Optional. Whether endpoints should be applied to the generated rewrite rules.
* @return string[] Array of rewrite rules keyed by their regex pattern.
public function generate_rewrite_rules( $permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true ) {
// Build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?
foreach ( (array) $this->feeds as $feed_name ) {
$feedregex2 .= $feed_name . '|';
$feedregex2 = '(' . trim( $feedregex2, '|' ) . ')/?$';
* $feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom
* and <permalink>/atom are both possible
$feedregex = $this->feed_base . '/' . $feedregex2;
// Build a regex to match the trackback and page/xx parts of URLs.
$trackbackregex = 'trackback/?$';
$pageregex = $this->pagination_base . '/?([0-9]{1,})/?$';
$commentregex = $this->comments_pagination_base . '-([0-9]{1,})/?$';
$embedregex = 'embed/?$';
// Build up an array of endpoint regexes to append => queries to append.
$ep_query_append = array();
foreach ( (array) $this->endpoints as $endpoint ) {
// Match everything after the endpoint name, but allow for nothing to appear there.
$epmatch = $endpoint[1] . '(/(.*))?/?$';
// This will be appended on to the rest of the query for each dir.
$epquery = '&' . $endpoint[2] . '=';
$ep_query_append[ $epmatch ] = array( $endpoint[0], $epquery );
// Get everything up to the first rewrite tag.
$front = substr( $permalink_structure, 0, strpos( $permalink_structure, '%' ) );
// Build an array of the tags (note that said array ends up being in $tokens[0]).
preg_match_all( '/%.+?%/', $permalink_structure, $tokens );
$num_tokens = count( $tokens[0] );
$index = $this->index; // Probably 'index.php'.
$trackbackindex = $index;
* Build a list from the rewritecode and queryreplace arrays, that will look something
* like tagname=$matches[i] where i is the current $i.
for ( $i = 0; $i < $num_tokens; ++$i ) {
$queries[ $i ] = $queries[ $i - 1 ] . '&';
$query_token = str_replace( $this->rewritecode, $this->queryreplace, $tokens[0][ $i ] ) . $this->preg_index( $i + 1 );
$queries[ $i ] .= $query_token;
// Get the structure, minus any cruft (stuff that isn't tags) at the front.
$structure = $permalink_structure;
$structure = str_replace( $front, '', $structure );
* Create a list of dirs to walk over, making rewrite rules for each level
* so for example, a $structure of /%year%/%monthnum%/%postname% would create
* rewrite rules for /%year%/, /%year%/%monthnum%/ and /%year%/%monthnum%/%postname%
$structure = trim( $structure, '/' );
$dirs = $walk_dirs ? explode( '/', $structure ) : array( $structure );
$num_dirs = count( $dirs );
// Strip slashes from the front of $front.
$front = preg_replace( '|^/+|', '', $front );
// The main workhorse loop.
for ( $j = 0; $j < $num_dirs; ++$j ) {
// Get the struct for this dir, and trim slashes off the front.
$struct .= $dirs[ $j ] . '/'; // Accumulate. see comment near explode('/', $structure) above.
$struct = ltrim( $struct, '/' );
// Replace tags with regexes.
$match = str_replace( $this->rewritecode, $this->rewritereplace, $struct );
// Make a list of tags, and store how many there are in $num_toks.
$num_toks = preg_match_all( '/%.+?%/', $struct, $toks );
// Get the 'tagname=$matches[i]'.
$query = ( ! empty( $num_toks ) && isset( $queries[ $num_toks - 1 ] ) ) ? $queries[ $num_toks - 1 ] : '';
// Set up $ep_mask_specific which is used to match more specific URL types.
$ep_mask_specific = EP_YEAR;
$ep_mask_specific = EP_MONTH;
$ep_mask_specific = EP_DAY;
$ep_mask_specific = EP_NONE;
// Create query for /page/xx.
$pagematch = $match . $pageregex;
$pagequery = $index . '?' . $query . '&paged=' . $this->preg_index( $num_toks + 1 );
// Create query for /comment-page-xx.
$commentmatch = $match . $commentregex;
$commentquery = $index . '?' . $query . '&cpage=' . $this->preg_index( $num_toks + 1 );
if ( get_option( 'page_on_front' ) ) {
// Create query for Root /comment-page-xx.
$rootcommentmatch = $match . $commentregex;
$rootcommentquery = $index . '?' . $query . '&page_id=' . get_option( 'page_on_front' ) . '&cpage=' . $this->preg_index( $num_toks + 1 );
// Create query for /feed/(feed|atom|rss|rss2|rdf).