The is_success() WordPress PHP function determines if an operation is successful.
Usage
is_success( $result );
Example:
Input:
$result = wp_insert_post( $post_data ); is_success( $result );
Output:
true or false
Parameters
$result(mixed): The result of an operation, like a database query or a post insertion.
More information
See WordPress Developer Resources: is_success()
Examples
Check if a post was successfully inserted
In this example, we are using the is_success() function to check if a post was inserted successfully.
$post_data = array(
'post_title' => 'My Sample Post',
'post_content' => 'This is a sample post content.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8, 39 )
);
$result = wp_insert_post( $post_data );
if ( is_success( $result ) ) {
echo "Post successfully inserted!";
} else {
echo "There was an error inserting the post.";
}
Check if a user was successfully updated
In this example, we are using the is_success() function to check if a user was updated successfully.
$user_id = 1;
$user_data = array(
'ID' => $user_id,
'user_email' => '[email protected]'
);
$result = wp_update_user( $user_data );
if ( is_success( $result ) ) {
echo "User successfully updated!";
} else {
echo "There was an error updating the user.";
}
Check if a taxonomy was successfully created
In this example, we are using the is_success() function to check if a taxonomy was created successfully.
$taxonomy_data = array(
'name' => 'Genre',
'taxonomy' => 'genre'
);
$result = wp_insert_term( $taxonomy_data['name'], $taxonomy_data['taxonomy'] );
if ( is_success( $result ) ) {
echo "Taxonomy successfully created!";
} else {
echo "There was an error creating the taxonomy.";
}
Check if a comment was successfully added
In this example, we are using the is_success() function to check if a comment was added successfully.
$comment_data = array(
'comment_post_ID' => 1,
'comment_author' => 'Jane Doe',
'comment_content' => 'This is a sample comment.',
'comment_type' => ''
);
$result = wp_insert_comment( $comment_data );
if ( is_success( $result ) ) {
echo "Comment successfully added!";
} else {
echo "There was an error adding the comment.";
}
Check if a custom post type was successfully registered
In this example, we are using the is_success() function to check if a custom post type was registered successfully.
function my_custom_post_type() {
$args = array(
'public' => true,
'label' => 'My Custom Post Type',
'description' => 'A custom post type for my website.'
);
$result = register_post_type( 'my-custom-post-type', $args );
if ( is_success( $result ) ) {
echo "Custom post type successfully registered!";
} else {
echo "There was an error registering