0

First of all I'm using twentyeleven theme, I created a custom template page to insert posts. It has 4 submit forms. it is working well, also setting feature image is ok for 3 forms except the first one it is not setting feature image for the post even it upload the image (using upload form and also using image from link) and attach it to the post, what makes me crazy is that I'm using the same function to set feature image, but I don't know why it is not working with the first submit form

This is my code for setting feature image:

$pid = wp_insert_post($new_post);
$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'] . '/' . $filename;
}else{
    $file = $upload_dir['basedir'] . '/' . $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 = wp_insert_attachment( $attachment, $file, $pid );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );

set_post_thumbnail( $pid, $attach_id );

I'm getting these errors

1- In the the home page and edit post in admin area:

Object of class WP_Error could not be converted to int in SITE_ROOT/public_html/wp-includes/post.php on line 4294 which leads me to wp_attachment_is_image function in the main post.php page

function wp_attachment_is_image( $post_id = 0 ) {
    $post_id = (int) $post_id;
    if ( !$post = get_post( $post_id ) )
        return false;

    if ( !$file = get_attached_file( $post->ID ) )
        return false;

    $ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false;

    $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );

    if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) )
        return true;
    return false;
}

I tested $post_id by is_int and fount that is is integer

2- In the home page : Object of class WP_Error could not be converted to int in SITE_ROOT/public_html/wp-includes/functions.php on line 3814

which leads me to _get_non_cached_ids in functions.php page

function _get_non_cached_ids( $object_ids, $cache_key ) {
    $clean = array();
    foreach ( $object_ids as $id ) { 
        $id = (int) $id;
        if ( !wp_cache_get( $id, $cache_key ) ) {
            $clean[] = $id;
        }
    }

    return $clean;
}

I tested $id by is_int() and I got that it is not integer

How can I fix that problem, and why only this first form is not setting feature image even it is attach the image to the post

  • I double check it using is_int and echo the message saying id 7548 is not int, How could 7548 be not int? – usama sulaiman Sep 30 '13 at 14:50
  • I used get_defined_vars() but I don't know where I have to look because I got hundreds of variables – usama sulaiman Sep 30 '13 at 17:11
  • I tried to echo $id which in foreach for _get_non_cached_ids function but I got that error: Catchable fatal error: Object of class WP_Error could not be converted to string in SITE_ROOT/public_html/wp-includes/functions.php on line 3814 – usama sulaiman Sep 30 '13 at 18:55
1

Finally I fix the problem

I keep tracking every where in my code and start to comment my code parts step by step.

as I said in my question I have a form that do upload the image (using upload form and also using image from link), and that was the main problem a conflict between uploading using upload form and using image link

So when I comment the function which I'm using to upload image from PC the code work perfectly

0

One of your function calls is returning a WP_Error object and you are trying to use that object as if it were an integer. This is the likely culprit:

$pid = wp_insert_post($new_post);

I can't say why as the code is out of context and you didn't include $new_post.

It is also possible that you are causing trouble for yourself by including a Core file directly:

require_once(ABSPATH . 'wp-admin/includes/image.php'); 

That file may require other files that are not being loaded properly.

  • @ s_ha_dum I use is_wp_error function to check if $pid has errors or not and I didn't get any error. also require_once(ABSPATH . 'wp-admin/includes/image.php'); is recommended by all users, What I can't know is that why only first form not working even I'm using same function to set featured image and the page contains 3 other working, also as I mentioned the not working one is adding contents and attaching image to post normally, and only has problem in setting featured image to that post – usama sulaiman Sep 30 '13 at 14:41
  • How did you use is_wp_error? – s_ha_dum Sep 30 '13 at 14:50
  • @ s_ha_dum I use it like so: $pid = wp_insert_post($new_post); if ( is_wp_error( $pid ) ) { $error_string = $pid->get_error_message(); echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';exit; }else{ echo 'pid is ok';exit; } I got pid is ok thank you for your help, I forgot to thank you at the beginning – usama sulaiman Sep 30 '13 at 15:04
  • @ s_ha_dum I can't solve the problem, I tried many ways but I can't, do you have any suggest? – usama sulaiman Oct 1 '13 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.