wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_random_content_flush_rewrite_rules_for_post › WordPress Function

Since7.2.0
Deprecatedn/a
› wp_random_content_flush_rewrite_rules_for_post ( $post_id, $post = null, $update = false, $post_before = null )
Parameters: (4)
  • (int) $post_id Post ID.
    Required: Yes
  • (WP_Post|null) $post Optional. Post object. Looked up from `$post_id` when null. Default null.
    Required: No
    Default: null
  • (bool) $update Optional. Whether this is an existing post being updated. Default false.
    Required: No
    Default: false
  • (WP_Post|null) $post_before Optional. Post object before the update, null for new posts. Default null.
    Required: No
    Default: null
See:
  • WP_Rewrite::random_rewrite_rules()
Defined at:
Codex:

Flushes the rewrite rules when a page or post at the random content base changes.

A published page at the random base, for example example.com/random/, takes precedence over random content redirects. So does a published post when the permalink structure places posts at the same URL, for example /%postname%/, see WP_Rewrite::post_permalinks_share_random_base. As the rewrite rules account for the page or post, they need to be regenerated when it is published, unpublished, renamed, moved or deleted. Runs on the {@see 'wp_after_insert_post'} and {@see 'after_delete_post'} actions. Third-party code may fire these actions with unexpected values, so the parameters are not type declared. When $post is omitted it is looked up from $post_id. Passing anything other than a WP_Post as $post, or a post that cannot be looked up, is incorrect usage and nothing is flushed. $post_before is only considered on wp_after_insert_post, where passing anything other than a WP_Post or null is incorrect usage and the value is ignored.


Source

function wp_random_content_flush_rewrite_rules_for_post( $post_id, $post = null, $update = false, $post_before = null ): void {
	global $wp_rewrite;

	// Only look up a real ID: get_post() falls back to the global post for an empty value.
	if ( null === $post && (int) $post_id > 0 ) {
		$post = get_post( (int) $post_id );
	}

	if ( ! $post instanceof WP_Post ) {
		_doing_it_wrong(
			__FUNCTION__,
			sprintf(
				/* translators: 1: $post, 2: WP_Post */
				__( 'The %1$s argument must be a %2$s object.' ),
				'<code>$post</code>',
				'<code>WP_Post</code>'
			),
			'7.2.0'
		);
		return;
	}

	$posts = array( $post );

	// Only wp_after_insert_post passes the previous post object, which matters when it was renamed or moved.
	if ( 'wp_after_insert_post' === current_action() ) {
		if ( $post_before instanceof WP_Post ) {
			$posts[] = $post_before;
		} elseif ( null !== $post_before ) {
			_doing_it_wrong(
				__FUNCTION__,
				sprintf(
					/* translators: 1: $post_before, 2: WP_Post */
					__( 'The %1$s argument must be a %2$s object or null.' ),
					'<code>$post_before</code>',
					'<code>WP_Post</code>'
				),
				'7.2.0'
			);
		}
	}

	if ( ! $wp_rewrite->using_permalinks() || ! wp_is_random_content_redirect_enabled() ) {
		return;
	}

	foreach ( $posts as $page ) {
		if (
			$wp_rewrite->get_random_base() === $page->post_name
			&& (
				( 'page' === $page->post_type && 0 === (int) $page->post_parent )
				|| ( 'post' === $page->post_type && $wp_rewrite->post_permalinks_share_random_base() )
			)
		) {
			flush_rewrite_rules( false );
			return;
		}
	}
}