wp_set_unique_slug_on_create_template_part() WordPress Function

The wp_set_unique_slug_on_create_template_part() function in WordPress allows you to set a unique slug for a template part when it is created. This is useful for preventing collisions between template parts with the same name.

wp_set_unique_slug_on_create_template_part( int $post_id ) #

Sets a custom slug when creating auto-draft template parts.


Description

This is only needed for auto-drafts created by the regular WP editor. If this page is to be removed, this will not be necessary.


Top ↑

Parameters

$post_id

(int)(Required)Post ID.


Top ↑

Source

File: wp-includes/theme-templates.php

function wp_set_unique_slug_on_create_template_part( $post_id ) {
	$post = get_post( $post_id );
	if ( 'auto-draft' !== $post->post_status ) {
		return;
	}

	if ( ! $post->post_name ) {
		wp_update_post(
			array(
				'ID'        => $post_id,
				'post_name' => 'custom_slug_' . uniqid(),
			)
		);
	}

	$terms = get_the_terms( $post_id, 'wp_theme' );
	if ( ! is_array( $terms ) || ! count( $terms ) ) {
		wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' );
	}
}


Top ↑

Changelog

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