Prevent empty Post Title on form submit via front end post (wp_insert_post_)

Simply put the wp_insert_post call inside your conditional check so its only called if the post title is not empty, something like this: if (empty($_POST[‘my_title’])){ echo ‘error: please insert a title’; } else { $title = $_POST[‘my_title’]; $new_post = array( ‘post_title’ => $title, ‘post_status’ => ‘publish’, ‘post_type’ => ‘post’); $pid = wp_insert_post($new_post); }

Does wp_insert_post validate the input?

The short answer; absolutely. wp_insert_post() will only SQL escape the content. Use the KSES library & wp_kses() to filter out the nasties, or esc_html() to escape all HTML. Most importantly, check out the codex on data validation (read: sanitization). A Note On KSES: Use wp_filter_kses() or wp_kses_data() to apply the same KSES rules as post … Read more

How do I programmatically add items of content to a custom post type?

You should first run the wp_insert_post() which will return the post ID. Then use that post ID to add your custom fields. Use add_post_meta() to add the custom fields. $post_id = wp_insert_post( $args ); add_post_meta( $post_id, ‘longitude’, $my_long ); add_post_meta( $post_id, ‘latitude’, $my_lat ); For image attachments, you can refer to this question: How to … Read more

Add PHP block template to content using wp_insert_post

I’ve modified serialize_block a bit: function serialize_block_template( $block ) { $block_content=””; if (isset($block[2])) { $index = 0; foreach ( $block[2] as $chunk ) { $block_content .= is_string( $chunk ) ? $chunk : serialize_block_template( $block[2][ $index++ ] ); } } return get_comment_delimited_block_content( $block[0], $block[1], $block_content ); } Disclaimer: could have some bugs, only tested it for … Read more

Insert new term during new post creation

first you should set the taxnomy in custom post type you have just set only for post title post content post comments you did not set for custom taxnomy $post = array(‘tax_input’ => [ array( <taxonomy> => <array | string> ) ] // For custom taxonomies. Default empty. ‘page_template’ => [ <string> ] // Requires … Read more

save_post + insert_post = infinite loop

This is because the first time you go round the loop $post is the current post. But the second time you go around the loop, $post has not changed. The same thing happens the 3rd, 4th, 5th, etc Because the $post variable is the current post of that page, not the post you’ve just saved/inserted, … Read more

Can we have a post without a slug?

I know this is really old question, but today I was dealing with something similar, and I have found a solution – if wp_unique_post_slug() calling is performance bottleneck, and post_name value is not important, then set post_name manualy to some random value. My wp_insert_post post action was taking too long lately (20-30s). It saves custom … Read more

When and Where to use wp_insert_post()

Use some type of conditional tag to check if those posts exist or not. If they do not exist, have them created with wp_insert_post. Do this logic inside the AddMyPages function and not around the add_action function. Example You want to add a page with a parent ID only if it does not exist, and … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)