Endless loop with wp_insert_post and wp_update_post

It sure can be written in a more efficient way (untested): $added = array(); global $wpdb; foreach($uniques as $unique){ $pagetitle = getTitle($unique); $new_post = array( ‘post_title’ => $unique, ‘post_status’ => ‘publish’, ‘post_type’ => ‘websites’ ); $pid = wp_insert_post($new_post); if ($pid) { $wpdb->query( $wpdb->prepare( “INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES (%d, ‘%s’, ‘%s’), (%d, ‘%s’, … Read more

Add image to post from external URL

For those who would know : $upload_dir = wp_upload_dir(); $image_data = file_get_contents($image_url); $filename = basename($image_url); if(wp_mkdir_p($upload_dir[‘path’])) $file = $upload_dir[‘path’] . “https://wordpress.stackexchange.com/” . $filename; else $file = $upload_dir[‘basedir’] . “https://wordpress.stackexchange.com/” . $filename; file_put_contents($file, $image_data); $wp_filetype = wp_check_filetype($filename, null ); $attachment = array( ‘post_mime_type’ => $wp_filetype[‘type’], ‘post_title’ => sanitize_file_name($filename), ‘post_content’ => ”, ‘post_status’ => ‘inherit’ ); $attach_id … Read more

How to allow data:image attribute in src tag during post insert?

Thanks to naththedeveloper from StackOverflow. His answer worked for me. Well, this was a nightmare to find, but I think I’ve resolved it after digging through the WordPress code which hooks in through wp_insert_post. Please add this to your functions.php file and check it works: add_filter(‘kses_allowed_protocols’, function ($protocols) { $protocols[] = ‘data’; return $protocols; }); … Read more

Create posts without login from frontend

You should not allow anonymous users to publish anything on your website without authentication. If you need to store custom data that is specified by users, you should use the custom fields instead. In your case, add_post_meta() comes in handy. After creating a post using wp_insert_post(), pass its ID to add_post_meta() and add custom fields … Read more

Not Able to Insert Taxonomy Term Using wp_insert_post()

Few points come to mind: There is a typo in “post_type” => “‘eyeglasses” (extra single quote). It should be: “post_type” => “eyeglasses”. Try putting the $term instead of array( $term ): “tax_input” => array( “models” => $term ) Also, is it models or model? e.g. “tax_input” => array( “model” => $term ) tax_input requires assign_terms … Read more

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