WP_Rewrite::get_date_permastruct() WordPress Method

The WP_Rewrite::get_date_permastruct() method is used to retrieve the date permalink structure for a Wordpress site. This structure is used to generate permalinks for Wordpress posts and pages that are dated.

WP_Rewrite::get_date_permastruct() #

Retrieves date permalink structure, with year, month, and day.


Description

The permalink structure for the date, if not set already depends on the permalink structure. It can be one of three formats. The first is year, month, day; the second is day, month, year; and the last format is month, day, year. These are matched against the permalink structure for which one is used. If none matches, then the default will be used, which is year, month, day.

Prevents post ID and date permalinks from overlapping. In the case of post_id, the date permalink will be prepended with front permalink with ‘date/’ before the actual permalink to form the complete date permalink structure.


Top ↑

Return

(string|false) Date permalink structure on success, false on failure.


Top ↑

Source

File: wp-includes/class-wp-rewrite.php

	public function get_date_permastruct() {
		if ( isset( $this->date_structure ) ) {
			return $this->date_structure;
		}

		if ( empty( $this->permalink_structure ) ) {
			$this->date_structure = '';
			return false;
		}

		// 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 = '';
		$date_endian          = '';

		foreach ( $endians as $endian ) {
			if ( false !== strpos( $this->permalink_structure, $endian ) ) {
				$date_endian = $endian;
				break;
			}
		}

		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/.
		 */
		$front = $this->front;
		preg_match_all( '/%.+?%/', $this->permalink_structure, $tokens );
		$tok_index = 1;
		foreach ( (array) $tokens[0] as $token ) {
			if ( '%post_id%' === $token && ( $tok_index <= 3 ) ) {
				$front = $front . 'date/';
				break;
			}
			$tok_index++;
		}

		$this->date_structure = $front . $date_endian;

		return $this->date_structure;
	}


Top ↑

Changelog

Changelog
VersionDescription
1.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.