wp_install

Definition:
function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '' ) {}

Installs the blog

Parameters

  • string $blog_title: Blog title.
  • string $user_name: User’s username.
  • string $user_email: User’s email.
  • bool $public: Whether blog is public.
  • null $deprecated: Optional. Not used.
  • string $user_password: Optional. User’s chosen password. Will default to a random password.

Return values

returns:Array keys ‘url’, ‘user_id’, ‘password’, ‘password_message’.

Source code

function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '' ) {

	global $wp_rewrite;



	if ( !empty( $deprecated ) )

		_deprecated_argument( __FUNCTION__, '2.6' );



	wp_check_mysql_version();

	wp_cache_flush();

	make_db_current_silent();

	populate_options();

	populate_roles();



	update_option('blogname', $blog_title);

	update_option('admin_email', $user_email);

	update_option('blog_public', $public);



	$guessurl = wp_guess_url();



	update_option('siteurl', $guessurl);



	// If not a public blog, don't ping.

	if ( ! $public )

		update_option('default_pingback_flag', 0);



	// Create default user.  If the user already exists, the user tables are

	// being shared among blogs.  Just set the role in that case.

	$user_id = username_exists($user_name);

	$user_password = trim($user_password);

	$email_password = false;

	if ( !$user_id && empty($user_password) ) {

		$user_password = wp_generate_password( 12, false );

		$message = __('<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.');

		$user_id = wp_create_user($user_name, $user_password, $user_email);

		update_user_option($user_id, 'default_password_nag', true, true);

		$email_password = true;

	} else if ( !$user_id ) {

		// Password has been provided

		$message = '<em>'.__('Your chosen password.').'</em>';

		$user_id = wp_create_user($user_name, $user_password, $user_email);

	} else {

		$message =  __('User already exists. Password inherited.');

	}



	$user = new WP_User($user_id);

	$user->set_role('administrator');



	wp_install_defaults($user_id);



	$wp_rewrite->flush_rules();



	wp_new_blog_notification($blog_title, $guessurl, $user_id, ($email_password ? $user_password : __('The password you chose during the install.') ) );



	wp_cache_flush();



	return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $user_password, 'password_message' => $message);

}

3799

wp_insert_term

Definition:
function wp_insert_term( $term, $taxonomy, $args = array() {}

Adds a new term to the database. Optionally marks it as an alias of an existing term.
Error handling is assigned for the nonexistence of the $taxonomy and $term parameters before inserting. If both the term id and taxonomy exist previously, then an array will be returned that contains the term id and the contents of what is returned. The keys of the array are ‘term_id’ and ‘term_taxonomy_id’ containing numeric values.

Parameters

  • string $term: The term to add or update.
  • string $taxonomy: The taxonomy to which to add the term
  • array|string $args: Change the values of the inserted term

Return values

returns:The Term ID and Term Taxonomy ID

Defined filters

  • pre_insert_term
    apply_filters( 'pre_insert_term', $term, $taxonomy )
  • term_id_filter
    apply_filters('term_id_filter', $term_id, $tt_id)

Defined actions

  • edit_terms
    do_action( 'edit_terms', $alias->term_id );
  • edited_terms
    do_action( 'edited_terms', $alias->term_id );
  • edit_terms
    do_action( 'edit_terms', $term_id );
  • edited_terms
    do_action( 'edited_terms', $term_id );
  • create_term
    do_action("create_term", $term_id, $tt_id, $taxonomy);
  • create_$taxonomy
    do_action("create_$taxonomy", $term_id, $tt_id);
  • created_term
    do_action("created_term", $term_id, $tt_id, $taxonomy);
  • created_$taxonomy
    do_action("created_$taxonomy", $term_id, $tt_id);

Source code

function wp_insert_term( $term, $taxonomy, $args = array() ) {

	global $wpdb;



	if ( ! taxonomy_exists($taxonomy) )

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



	$term = apply_filters( 'pre_insert_term', $term, $taxonomy );

		if ( is_wp_error( $term ) )

			return $term;



	if ( is_int($term) && 0 == $term )

		return new WP_Error('invalid_term_id', __('Invalid term ID'));



	if ( '' == trim($term) )

		return new WP_Error('empty_term_name', __('A name is required for this term'));



	$defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');

	$args = wp_parse_args($args, $defaults);

	$args['name'] = $term;

	$args['taxonomy'] = $taxonomy;

	$args = sanitize_term($args, $taxonomy, 'db');

	extract($args, EXTR_SKIP);



	// expected_slashed ($name)

	$name = stripslashes($name);

	$description = stripslashes($description);



	if ( empty($slug) )

		$slug = sanitize_title($name);



	$term_group = 0;

	if ( $alias_of ) {

		$alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) );

		if ( $alias->term_group ) {

			// The alias we want is already in a group, so let's use that one.

			$term_group = $alias->term_group;

		} else {

			// The alias isn't in a group, so let's create a new one and firstly add the alias term to it.

			$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;

			do_action( 'edit_terms', $alias->term_id );

			$wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) );

			do_action( 'edited_terms', $alias->term_id );

		}

	}



	if ( $term_id = term_exists($slug) ) {

		$existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id), ARRAY_A );

		// We've got an existing term in the same taxonomy, which matches the name of the new term:

		if ( is_taxonomy_hierarchical($taxonomy) && $existing_term['name'] == $name && $exists = term_exists( (int) $term_id, $taxonomy ) ) {

			// Hierarchical, and it matches an existing term, Do not allow same "name" in the same level.

			$siblings = get_terms($taxonomy, array('fields' => 'names', 'get' => 'all', 'parent' => (int)$parent) );

			if ( in_array($name, $siblings) ) {

				return new WP_Error('term_exists', __('A term with the name provided already exists with this parent.'), $exists['term_id']);

			} else {

				$slug = wp_unique_term_slug($slug, (object) $args);

				if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )

					return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);

				$term_id = (int) $wpdb->insert_id;

			}

		} elseif ( $existing_term['name'] != $name ) {

			// We've got an existing term, with a different name, Create the new term.

			$slug = wp_unique_term_slug($slug, (object) $args);

			if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )

				return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);

			$term_id = (int) $wpdb->insert_id;

		} elseif ( $exists = term_exists( (int) $term_id, $taxonomy ) )  {

			// Same name, same slug.

			return new WP_Error('term_exists', __('A term with the name provided already exists.'), $exists['term_id']);

		}

	} else {

		// This term does not exist at all in the database, Create it.

		$slug = wp_unique_term_slug($slug, (object) $args);

		if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )

			return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);

		$term_id = (int) $wpdb->insert_id;

	}



	// Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string.

	if ( empty($slug) ) {

		$slug = sanitize_title($slug, $term_id);

		do_action( 'edit_terms', $term_id );

		$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );

		do_action( 'edited_terms', $term_id );

	}



	$tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id ) );



	if ( !empty($tt_id) )

		return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);



	$wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );

	$tt_id = (int) $wpdb->insert_id;



	do_action("create_term", $term_id, $tt_id, $taxonomy);

	do_action("create_$taxonomy", $term_id, $tt_id);



	$term_id = apply_filters('term_id_filter', $term_id, $tt_id);



	clean_term_cache($term_id, $taxonomy);



	do_action("created_term", $term_id, $tt_id, $taxonomy);

	do_action("created_$taxonomy", $term_id, $tt_id);



	return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);

}

3795

wp_insert_post

Definition:
function wp_insert_post($postarr, $wp_error = false) {}

Insert a post.
If the $postarr parameter has ‘ID’ set to a value, then post will be updated.

Parameters

  • array $postarr: Elements that make up post to insert.
  • bool $wp_error: Optional. Allow return of WP_Error on failure.

Return values

returns:The value 0 or WP_Error on failure. The post ID on success.

Defined filters

  • wp_insert_post_empty_content
    apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr )
  • wp_insert_post_parent
    apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr )
  • wp_insert_post_data
    apply_filters('wp_insert_post_data', $data, $postarr)

Defined actions

  • pre_post_update
    do_action( 'pre_post_update', $post_ID );
  • edit_post
    do_action('edit_post', $post_ID, $post);
  • post_updated
    do_action( 'post_updated', $post_ID, $post_after, $post_before);
  • save_post
    do_action('save_post', $post_ID, $post);
  • wp_insert_post
    do_action('wp_insert_post', $post_ID, $post);

Source code

function wp_insert_post($postarr, $wp_error = false) {

	global $wpdb, $wp_rewrite, $user_ID;



	$defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,

		'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,

		'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',

		'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,

		'post_content' => '', 'post_title' => '');



	$postarr = wp_parse_args($postarr, $defaults);



	unset( $postarr[ 'filter' ] );



	$postarr = sanitize_post($postarr, 'db');



	// export array as variables

	extract($postarr, EXTR_SKIP);



	// Are we updating or creating?

	$update = false;

	if ( !empty($ID) ) {

		$update = true;

		$previous_status = get_post_field('post_status', $ID);

	} else {

		$previous_status = 'new';

	}



	$maybe_empty = ! $post_content && ! $post_title && ! $post_excerpt && post_type_supports( $post_type, 'editor' )

		&& post_type_supports( $post_type, 'title' ) && post_type_supports( $post_type, 'excerpt' );

	if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) {

		if ( $wp_error )

			return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) );

		else

			return 0;

	}



	if ( empty($post_type) )

		$post_type = 'post';



	if ( empty($post_status) )

		$post_status = 'draft';



	if ( !empty($post_category) )

		$post_category = array_filter($post_category); // Filter out empty terms



	// Make sure we set a valid category.

	if ( empty($post_category) || 0 == count($post_category) || !is_array($post_category) ) {

		// 'post' requires at least one category.

		if ( 'post' == $post_type && 'auto-draft' != $post_status )

			$post_category = array( get_option('default_category') );

		else

			$post_category = array();

	}



	if ( empty($post_author) )

		$post_author = $user_ID;



	$post_ID = 0;



	// Get the post ID and GUID

	if ( $update ) {

		$post_ID = (int) $ID;

		$guid = get_post_field( 'guid', $post_ID );

		$post_before = get_post($post_ID);

	}



	// Don't allow contributors to set the post slug for pending review posts

	if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) )

		$post_name = '';



	// Create a valid post name.  Drafts and pending posts are allowed to have an empty

	// post name.

	if ( empty($post_name) ) {

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

			$post_name = sanitize_title($post_title);

		else

			$post_name = '';

	} else {

		// On updates, we need to check to see if it's using the old, fixed sanitization context.

		$check_name = sanitize_title( $post_name, '', 'old-save' );

		if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $ID ) == $check_name )

			$post_name = $check_name;

		else // new post, or slug has changed.

			$post_name = sanitize_title($post_name);

	}



	// If the post date is empty (due to having been new or a draft) and status is not 'draft' or 'pending', set date to now

	if ( empty($post_date) || '0000-00-00 00:00:00' == $post_date )

		$post_date = current_time('mysql');



	if ( empty($post_date_gmt) || '0000-00-00 00:00:00' == $post_date_gmt ) {

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

			$post_date_gmt = get_gmt_from_date($post_date);

		else

			$post_date_gmt = '0000-00-00 00:00:00';

	}



	if ( $update || '0000-00-00 00:00:00' == $post_date ) {

		$post_modified     = current_time( 'mysql' );

		$post_modified_gmt = current_time( 'mysql', 1 );

	} else {

		$post_modified     = $post_date;

		$post_modified_gmt = $post_date_gmt;

	}



	if ( 'publish' == $post_status ) {

		$now = gmdate('Y-m-d H:i:59');

		if ( mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false) )

			$post_status = 'future';

	} elseif( 'future' == $post_status ) {

		$now = gmdate('Y-m-d H:i:59');

		if ( mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false) )

			$post_status = 'publish';

	}



	if ( empty($comment_status) ) {

		if ( $update )

			$comment_status = 'closed';

		else

			$comment_status = get_option('default_comment_status');

	}

	if ( empty($ping_status) )

		$ping_status = get_option('default_ping_status');



	if ( isset($to_ping) )

		$to_ping = preg_replace('|\s+|', "\n", $to_ping);

	else

		$to_ping = '';



	if ( ! isset($pinged) )

		$pinged = '';



	if ( isset($post_parent) )

		$post_parent = (int) $post_parent;

	else

		$post_parent = 0;



	// Check the post_parent to see if it will cause a hierarchy loop

	$post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );



	if ( isset($menu_order) )

		$menu_order = (int) $menu_order;

	else

		$menu_order = 0;



	if ( !isset($post_password) || 'private' == $post_status )

		$post_password = '';



	$post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);



	// expected_slashed (everything!)

	$data = compact( array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'guid' ) );

	$data = apply_filters('wp_insert_post_data', $data, $postarr);

	$data = stripslashes_deep( $data );

	$where = array( 'ID' => $post_ID );



	if ( $update ) {

		do_action( 'pre_post_update', $post_ID );

		if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {

			if ( $wp_error )

				return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error);

			else

				return 0;

		}

	} else {

		if ( isset($post_mime_type) )

			$data['post_mime_type'] = stripslashes( $post_mime_type ); // This isn't in the update

		// If there is a suggested ID, use it if not already present

		if ( !empty($import_id) ) {

			$import_id = (int) $import_id;

			if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) {

				$data['ID'] = $import_id;

			}

		}

		if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {

			if ( $wp_error )

				return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error);

			else

				return 0;

		}

		$post_ID = (int) $wpdb->insert_id;



		// use the newly generated $post_ID

		$where = array( 'ID' => $post_ID );

	}



	if ( empty($data['post_name']) && !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {

		$data['post_name'] = sanitize_title($data['post_title'], $post_ID);

		$wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );

	}



	if ( is_object_in_taxonomy($post_type, 'category') )

		wp_set_post_categories( $post_ID, $post_category );



	if ( isset( $tags_input ) && is_object_in_taxonomy($post_type, 'post_tag') )

		wp_set_post_tags( $post_ID, $tags_input );



	// new-style support for all custom taxonomies

	if ( !empty($tax_input) ) {

		foreach ( $tax_input as $taxonomy => $tags ) {

			$taxonomy_obj = get_taxonomy($taxonomy);

			if ( is_array($tags) ) // array = hierarchical, string = non-hierarchical.

				$tags = array_filter($tags);

			if ( current_user_can($taxonomy_obj->cap->assign_terms) )

				wp_set_post_terms( $post_ID, $tags, $taxonomy );

		}

	}



	$current_guid = get_post_field( 'guid', $post_ID );



	if ( 'page' == $data['post_type'] )

		clean_page_cache($post_ID);

	else

		clean_post_cache($post_ID);



	// Set GUID

	if ( !$update && '' == $current_guid )

		$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );



	$post = get_post($post_ID);



	if ( !empty($page_template) && 'page' == $data['post_type'] ) {

		$post->page_template = $page_template;

		$page_templates = get_page_templates();

		if ( 'default' != $page_template && !in_array($page_template, $page_templates) ) {

			if ( $wp_error )

				return new WP_Error('invalid_page_template', __('The page template is invalid.'));

			else

				return 0;

		}

		update_post_meta($post_ID, '_wp_page_template',  $page_template);

	}



	wp_transition_post_status($data['post_status'], $previous_status, $post);



	if ( $update ) {

		do_action('edit_post', $post_ID, $post);

		$post_after = get_post($post_ID);

		do_action( 'post_updated', $post_ID, $post_after, $post_before);

	}



	do_action('save_post', $post_ID, $post);

	do_action('wp_insert_post', $post_ID, $post);



	return $post_ID;

}

3793

wp_insert_link

Definition:
function wp_insert_link( $linkdata, $wp_error = false ) {}

This function inserts/updates links into/in the database.

Parameters

  • array $linkdata: Elements that make up the link to insert.
  • bool $wp_error: Optional. If true return WP_Error object on failure.

Return values

returns:Value 0 or WP_Error on failure. The link ID on success.

Defined actions

  • edit_link
    do_action( 'edit_link', $link_id );
  • add_link
    do_action( 'add_link', $link_id );

Source code

function wp_insert_link( $linkdata, $wp_error = false ) {

	global $wpdb;



	$defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 );



	$linkdata = wp_parse_args( $linkdata, $defaults );

	$linkdata = sanitize_bookmark( $linkdata, 'db' );



	extract( stripslashes_deep( $linkdata ), EXTR_SKIP );



	$update = false;



	if ( !empty( $link_id ) )

		$update = true;



	if ( trim( $link_name ) == '' ) {

		if ( trim( $link_url ) != '' ) {

			$link_name = $link_url;

		} else {

			return 0;

		}

	}



	if ( trim( $link_url ) == '' )

		return 0;



	if ( empty( $link_rating ) )

		$link_rating = 0;



	if ( empty( $link_image ) )

		$link_image = '';



	if ( empty( $link_target ) )

		$link_target = '';



	if ( empty( $link_visible ) )

		$link_visible = 'Y';



	if ( empty( $link_owner ) )

		$link_owner = get_current_user_id();



	if ( empty( $link_notes ) )

		$link_notes = '';



	if ( empty( $link_description ) )

		$link_description = '';



	if ( empty( $link_rss ) )

		$link_rss = '';



	if ( empty( $link_rel ) )

		$link_rel = '';



	// Make sure we set a valid category

	if ( ! isset( $link_category ) || 0 == count( $link_category ) || !is_array( $link_category ) ) {

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

	}



	if ( $update ) {

		if ( false === $wpdb->update( $wpdb->links, compact('link_url', 'link_name', 'link_image', 'link_target', 'link_description', 'link_visible', 'link_rating', 'link_rel', 'link_notes', 'link_rss'), compact('link_id') ) ) {

			if ( $wp_error )

				return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error );

			else

				return 0;

		}

	} else {

		if ( false === $wpdb->insert( $wpdb->links, compact('link_url', 'link_name', 'link_image', 'link_target', 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_rel', 'link_notes', 'link_rss') ) ) {

			if ( $wp_error )

				return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error );

			else

				return 0;

		}

		$link_id = (int) $wpdb->insert_id;

	}



	wp_set_link_cats( $link_id, $link_category );



	if ( $update )

		do_action( 'edit_link', $link_id );

	else

		do_action( 'add_link', $link_id );



	clean_bookmark_cache( $link_id );



	return $link_id;

}

3791