wp_set_password

Definition:
function wp_set_password( $password, $user_id ) {}

Updates the user’s password with a new encrypted one.
For integration with other applications, this function can be overwritten to instead use the other package password checking algorithm.

Parameters

  • string $password: The plaintext new user password
  • int $user_id: User ID

Source code

function wp_set_password( $password, $user_id ) {

	global $wpdb;



	$hash = wp_hash_password($password);

	$wpdb->update($wpdb->users, array('user_pass' => $hash, 'user_activation_key' => ''), array('ID' => $user_id) );



	wp_cache_delete($user_id, 'users');

}

4109

wp_set_object_terms

Definition:
function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) {}

Create Term and Taxonomy Relationships.
Relates an object (post, link etc) to a term and taxonomy type. Creates the term and taxonomy relationship if it doesn’t already exist. Creates a term if it doesn’t exist (using the slug).

Parameters

  • int $object_id: The object to relate to.
  • array|int|string $terms: The slug or id of the term, will replace all existing related terms in this taxonomy.
  • array|string $taxonomy: The context in which to relate the term to the object.
  • bool $append: If false will delete difference of terms.

Return values

returns:Affected Term IDs

Defined actions

  • add_term_relationship
    do_action( 'add_term_relationship', $object_id, $tt_id );
  • added_term_relationship
    do_action( 'added_term_relationship', $object_id, $tt_id );
  • delete_term_relationships
    do_action( 'delete_term_relationships', $object_id, $delete_terms );
  • deleted_term_relationships
    do_action( 'deleted_term_relationships', $object_id, $delete_terms );
  • set_object_terms
    do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids);

Source code

function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) {

	global $wpdb;



	$object_id = (int) $object_id;



	if ( ! taxonomy_exists($taxonomy) )

		return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));



	if ( !is_array($terms) )

		$terms = array($terms);



	if ( ! $append )

		$old_tt_ids =  wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'orderby' => 'none'));

	else

		$old_tt_ids = array();



	$tt_ids = array();

	$term_ids = array();

	$new_tt_ids = array();



	foreach ( (array) $terms as $term) {

		if ( !strlen(trim($term)) )

			continue;



		if ( !$term_info = term_exists($term, $taxonomy) ) {

			// Skip if a non-existent term ID is passed.

			if ( is_int($term) )

				continue;

			$term_info = wp_insert_term($term, $taxonomy);

		}

		if ( is_wp_error($term_info) )

			return $term_info;

		$term_ids[] = $term_info['term_id'];

		$tt_id = $term_info['term_taxonomy_id'];

		$tt_ids[] = $tt_id;



		if ( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id ) ) )

			continue;

		do_action( 'add_term_relationship', $object_id, $tt_id );

		$wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $tt_id ) );

		do_action( 'added_term_relationship', $object_id, $tt_id );

		$new_tt_ids[] = $tt_id;

	}



	if ( $new_tt_ids )

		wp_update_term_count( $new_tt_ids, $taxonomy );



	if ( ! $append ) {

		$delete_terms = array_diff($old_tt_ids, $tt_ids);

		if ( $delete_terms ) {

			$in_delete_terms = "'" . implode("', '", $delete_terms) . "'";

			do_action( 'delete_term_relationships', $object_id, $delete_terms );

			$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_delete_terms)", $object_id) );

			do_action( 'deleted_term_relationships', $object_id, $delete_terms );

			wp_update_term_count($delete_terms, $taxonomy);

		}

	}



	$t = get_taxonomy($taxonomy);

	if ( ! $append && isset($t->sort) && $t->sort ) {

		$values = array();

		$term_order = 0;

		$final_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids'));

		foreach ( $tt_ids as $tt_id )

			if ( in_array($tt_id, $final_tt_ids) )

				$values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tt_id, ++$term_order);

		if ( $values )

			$wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)");

	}



	do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids);

	return $tt_ids;

}

4107

wp_set_link_cats

Definition:
function wp_set_link_cats( $link_id = 0, $link_categories = array() {}

Update link with the specified link categories.

Parameters

  • int $link_id: ID of link to update
  • array $link_categories: Array of categories to

Source code

function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {

	// If $link_categories isn't already an array, make it one:

	if ( !is_array( $link_categories ) || 0 == count( $link_categories ) )

		$link_categories = array( get_option( 'default_link_category' ) );



	$link_categories = array_map( 'intval', $link_categories );

	$link_categories = array_unique( $link_categories );



	wp_set_object_terms( $link_id, $link_categories, 'link_category' );



	clean_bookmark_cache( $link_id );

}

4105

wp_set_current_user

Definition:
function wp_set_current_user($id, $name = '') {}

Changes the current user by ID or name.
Set $id to null and specify a name if you do not know a user’s ID.

Parameters

  • int $id: User ID
  • string $name: User’s username

Return values

returns:Current user User object

Defined actions

  • set_current_user
    do_action('set_current_user');

Source code

function wp_set_current_user($id, $name = '') {

	global $current_user;



	if ( isset($current_user) && ($id == $current_user->ID) )

		return $current_user;



	$current_user = new WP_User($id, $name);



	setup_userdata($current_user->ID);



	do_action('set_current_user');



	return $current_user;

}

4103

wp_set_comment_status

Definition:
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) {}

Sets the status of a comment.
The ‘wp_set_comment_status’ action is called after the comment is handled and will only be called, if the comment status is either ‘hold’, ‘approve’, or ‘spam’. If the comment status is not in the list, then false is returned and if the status is ‘delete’, then the comment is deleted without calling the action.

Parameters

  • int $comment_id: Comment ID.
  • string $comment_status: New comment status, either ‘hold’, ‘approve’, ‘spam’, or ‘delete’.
  • bool $wp_error: Whether to return a WP_Error object if there is a failure. Default is false.

Return values

returns:False on failure or deletion and true on success.

Defined actions

  • wp_set_comment_status
    do_action('wp_set_comment_status', $comment_id, $comment_status);

Source code

function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) {

	global $wpdb;



	$status = '0';

	switch ( $comment_status ) {

		case 'hold':

		case '0':

			$status = '0';

			break;

		case 'approve':

		case '1':

			$status = '1';

			if ( get_option('comments_notify') ) {

				$comment = get_comment($comment_id);

				wp_notify_postauthor($comment_id, $comment->comment_type);

			}

			break;

		case 'spam':

			$status = 'spam';

			break;

		case 'trash':

			$status = 'trash';

			break;

		default:

			return false;

	}



	$comment_old = clone get_comment($comment_id);



	if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id) ) ) {

		if ( $wp_error )

			return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);

		else

			return false;

	}



	clean_comment_cache($comment_id);



	$comment = get_comment($comment_id);



	do_action('wp_set_comment_status', $comment_id, $comment_status);

	wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);



	wp_update_comment_count($comment->comment_post_ID);



	return true;

}

4101