avoid_blog_page_permalink_collision() WordPress Function

When creating a new blog page in WordPress, it's important to avoid any potential permalink collisions. The avoid_blog_page_permalink_collision() function helps to ensure that your new blog page won't conflict with any existing pages or posts on your site. This function takes a single parameter, the desired permalink for your new blog page. It then checks to see if that permalink is already in use on your site. If it is, the function generates a new permalink for your blog page that won't conflict with any other pages or posts.

avoid_blog_page_permalink_collision( array $data, array $postarr ) #

Avoids a collision between a site slug and a permalink slug.


Description

In a subdirectory installation this will make sure that a site and a post do not use the same subdirectory by checking for a site with the same name as a new post.


Top ↑

Parameters

$data

(array)(Required)An array of post data.

$postarr

(array)(Required)An array of posts. Not currently used.


Top ↑

Return

(array) The new array of post data after checking for collisions.


Top ↑

Source

File: wp-admin/includes/ms.php

function avoid_blog_page_permalink_collision( $data, $postarr ) {
	if ( is_subdomain_install() ) {
		return $data;
	}
	if ( 'page' !== $data['post_type'] ) {
		return $data;
	}
	if ( ! isset( $data['post_name'] ) || '' === $data['post_name'] ) {
		return $data;
	}
	if ( ! is_main_site() ) {
		return $data;
	}
	if ( isset( $data['post_parent'] ) && $data['post_parent'] ) {
		return $data;
	}

	$post_name = $data['post_name'];
	$c         = 0;

	while ( $c < 10 && get_id_from_blogname( $post_name ) ) {
		$post_name .= mt_rand( 1, 10 );
		$c++;
	}

	if ( $post_name !== $data['post_name'] ) {
		$data['post_name'] = $post_name;
	}

	return $data;
}


Top ↑

Changelog

Changelog
VersionDescription
3.0.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.