wp_unregister_widget_control

Definition:
function wp_unregister_widget_control($id) {}

Remove control callback for widget.

Parameters

  • int|string $id: Widget ID.

Source code

function wp_unregister_widget_control($id) {

	return wp_register_widget_control($id, '', '');

}

4199

wp_unregister_sidebar_widget

Definition:
function wp_unregister_sidebar_widget($id) {}

Remove widget from sidebar.

Parameters

  • int|string $id: Widget ID.

Defined actions

  • wp_unregister_sidebar_widget
    do_action( 'wp_unregister_sidebar_widget', $id );

Source code

function wp_unregister_sidebar_widget($id) {

	do_action( 'wp_unregister_sidebar_widget', $id );



	wp_register_sidebar_widget($id, '', '');

	wp_unregister_widget_control($id);

}

4197

wp_unique_term_slug

Definition:
function wp_unique_term_slug($slug, $term) {}

Will make slug unique, if it isn’t already.
The $slug has to be unique global to every taxonomy, meaning that one taxonomy term can’t have a matching slug with another taxonomy term. Each slug has to be globally unique for every taxonomy.

Parameters

  • string $slug: The string that will be tried for a unique slug
  • object $term: The term object that the $slug will belong too

Return values

returns:Will return a true unique slug.

Source code

function wp_unique_term_slug($slug, $term) {

	global $wpdb;



	if ( ! term_exists( $slug ) )

		return $slug;



	// If the taxonomy supports hierarchy and the term has a parent, make the slug unique

	// by incorporating parent slugs.

	if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) {

		$the_parent = $term->parent;

		while ( ! empty($the_parent) ) {

			$parent_term = get_term($the_parent, $term->taxonomy);

			if ( is_wp_error($parent_term) || empty($parent_term) )

				break;

			$slug .= '-' . $parent_term->slug;

			if ( ! term_exists( $slug ) )

				return $slug;



			if ( empty($parent_term->parent) )

				break;

			$the_parent = $parent_term->parent;

		}

	}



	// If we didn't get a unique slug, try appending a number to make it unique.

	if ( !empty($args['term_id']) )

		$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $args['term_id'] );

	else

		$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );



	if ( $wpdb->get_var( $query ) ) {

		$num = 2;

		do {

			$alt_slug = $slug . "-$num";

			$num++;

			$slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );

		} while ( $slug_check );

		$slug = $alt_slug;

	}



	return $slug;

}

4195

wp_unique_post_slug

Definition:
function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) {}

Computes a unique slug for the post, when given the desired slug and some post details.

Parameters

  • string $slug: the desired slug (post_name)
  • integer $post_ID
  • string $post_status: no uniqueness checks are made if the post is still draft or pending
  • string $post_type
  • integer $post_parent

Return values

returns:unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)

Defined filters

  • wp_unique_post_slug_is_bad_attachment_slug
    apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug )
  • wp_unique_post_slug_is_bad_hierarchical_slug
    apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent )
  • wp_unique_post_slug_is_bad_flat_slug
    apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type )
  • wp_unique_post_slug
    apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent )

Source code

function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) {

	if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )

		return $slug;



	global $wpdb, $wp_rewrite;



	$feeds = $wp_rewrite->feeds;

	if ( ! is_array( $feeds ) )

		$feeds = array();



	$hierarchical_post_types = get_post_types( array('hierarchical' => true) );

	if ( 'attachment' == $post_type ) {

		// Attachment slugs must be unique across all types.

		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";

		$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );



		if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {

			$suffix = 2;

			do {

				$alt_post_name = substr ($slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";

				$post_name_check = $wpdb->get_var( $wpdb->prepare($check_sql, $alt_post_name, $post_ID ) );

				$suffix++;

			} while ( $post_name_check );

			$slug = $alt_post_name;

		}

	} elseif ( in_array( $post_type, $hierarchical_post_types ) ) {

		// Page slugs must be unique within their own trees. Pages are in a separate

		// namespace than posts so page slugs are allowed to overlap post slugs.

		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '" . implode( "', '", esc_sql( $hierarchical_post_types ) ) . "' ) AND ID != %d AND post_parent = %d LIMIT 1";

		$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) );



		if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug )  || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) {

			$suffix = 2;

			do {

				$alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";

				$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID, $post_parent ) );

				$suffix++;

			} while ( $post_name_check );

			$slug = $alt_post_name;

		}

	} else {

		// Post slugs must be unique across all posts.

		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";

		$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );



		if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {

			$suffix = 2;

			do {

				$alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";

				$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) );

				$suffix++;

			} while ( $post_name_check );

			$slug = $alt_post_name;

		}

	}



	return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent );

}

4193

wp_unique_filename

Definition:
function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) {}

Get a filename that is sanitized and unique for the given directory.
If the filename is not unique, then a number will be added to the filename before the extension, and will continue adding numbers until the filename is unique.

Parameters

  • string $dir
  • string $filename
  • mixed $unique_filename_callback: Callback.

Return values

returns:New filename, if given wasn’t unique.

Source code

function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) {

	// sanitize the file name before we begin processing

	$filename = sanitize_file_name($filename);



	// separate the filename into a name and extension

	$info = pathinfo($filename);

	$ext = !empty($info['extension']) ? '.' . $info['extension'] : '';

	$name = basename($filename, $ext);



	// edge case: if file is named '.ext', treat as an empty name

	if ( $name === $ext )

		$name = '';



	// Increment the file number until we have a unique file to save in $dir. Use callback if supplied.

	if ( $unique_filename_callback && is_callable( $unique_filename_callback ) ) {

		$filename = call_user_func( $unique_filename_callback, $dir, $name, $ext );

	} else {

		$number = '';



		// change '.ext' to lower case

		if ( $ext && strtolower($ext) != $ext ) {

			$ext2 = strtolower($ext);

			$filename2 = preg_replace( '|' . preg_quote($ext) . '$|', $ext2, $filename );



			// check for both lower and upper case extension or image sub-sizes may be overwritten

			while ( file_exists($dir . "/$filename") || file_exists($dir . "/$filename2") ) {

				$new_number = $number + 1;

				$filename = str_replace( "$number$ext", "$new_number$ext", $filename );

				$filename2 = str_replace( "$number$ext2", "$new_number$ext2", $filename2 );

				$number = $new_number;

			}

			return $filename2;

		}



		while ( file_exists( $dir . "/$filename" ) ) {

			if ( '' == "$number$ext" )

				$filename = $filename . ++$number . $ext;

			else

				$filename = str_replace( "$number$ext", ++$number . $ext, $filename );

		}

	}



	return $filename;

}

4191