WordPress.org

Ready to get started?Download WordPress

Forums

Featured Image Path Doubles Up (extra http etc) (1 post)

  1. WebTechGlobal
    Member
    Posted 2 years ago #

    Has anyone ever experianced a problem where an image path stored in WordPress media as attachment/thumbnail or featured image doubles up?

    Must warn you I'm very tired now I'm shattered I fell sleep sitting up but I'm determined to solve this. Been working on this for 2 days trying to determine what is causing it in one blog but not all others. Everything is ruled out as best as I can. Server, theme, plugins.

    Why on a single site might this happen!?

    Functions are below, the last one is the one that the issue probably happens with. I'm now just running tests to try and determine which stage WordPress creates this invalid path but thought I would ask here.

    function eci_localmediaurlstring($urlstring,$postid)
    {
    // first we need to store images locally - this function returns id that way we can use them in various ways
    $mediaarray = eci_storeimageslocally($urlstring,$postid);

    $imagestring = '';
    $imagearray = array();

    // loop through each id putting the media data into array
    foreach($mediaarray as $key => $id)
    {
    //$imagestring .= wp_get_attachment_image( $id );
    $imagearray[] = wp_get_attachment_image_src( $id,array(1000,1000) );//$id, $size, $icon
    }

    // loop through the returns media data and extract the urls only
    $img = 0;
    foreach($imagearray as $key => $id)
    {
    if( $img != 0 )
    {
    $imagestring .= ',';
    }

    $imagestring = $id[0];

    ++$img;
    }

    eci_inf('Local Media URL Extract','$imagestring (pre return): '.$imagestring,__LINE__,__FILE__,__FUNCTION__,$set);

    return $imagestring;
    }

    /**
    * Initiates Adding Of Post Thumbnail as featured image to a post, also storing locally
    * specifically checks if giving post already has featured image or not and gets all post data
    * @param integer $post_id
    * @param array $imageurl (array of url)
    * @uses eci_createthumbnail_action
    */
    function eci_createthumbnail_initiate( $post_id, $imageurl )
    {
    global $wpdb;

    // check for existing thumbnail
    if ( get_post_meta( $post_id, '_thumbnail_id', true) || get_post_meta( $post_id, 'skip_post_thumb', true ) )
    {
    // finish the process as the post already has thumbnail
    return;
    }

    eci_inf('Create Thumbnail Initatiate','IMAGEURL (pre eci_createlocalmedia_action): '.$imageurl,__LINE__,__FILE__,__FUNCTION__,$set);

    // call action function to create the thumbnail in wordpress gallery
    $thumbid = eci_createlocalmedia_action( $imageurl, $post_id );

    // update post meta with new thumbnail
    if ( $thumbid )
    {
    update_post_meta( $post_id, '_thumbnail_id', $thumbid );
    }
    }// end apt_publish_post()

    /**
    * Takes a comma seperated string of image urls and stores them locally
    * @param comma seperated values $urlstring
    * @param integer $postid
    * @return array of local media id (mainly for use in custom fields)
    */
    function eci_storeimageslocally($urlstring,$postid)
    {
    global $wpdb;

    // split comma seperated values into array
    $urlarray = array();
    $urlarray = split(',', $urlstring);

    // array of wordpress locally stored media
    $mediaarray = array();

    // loop through each giving url putting the result into final array
    foreach($urlarray as $key => $url)
    {
    eci_inf('Store Images Locally','URL (pre eci_createlocalmedia_action): '.$url,__LINE__,__FILE__,__FUNCTION__,$set);

    // call action function to create the thumbnail in wordpress gallery
    $mediaarray[] = eci_createlocalmedia_action( $url, $postid );
    }

    return $mediaarray;
    }

    /**
    * Creates Thumbnail In WordPress Media System Using URL
    * @param unknown_type $imageurl
    * @param unknown_type $postid
    */
    function eci_createlocalmedia_action( $imageurl, $postid )
    {
    global $set;

    // get filename ????? can we not use basename here
    $filename = substr($imageurl, (strrpos($imageurl, '/'))+1);

    // confirm access to upload directory and get contents data for creating unique filename
    if (!(($uploads = wp_upload_dir(current_time('mysql')) ) && false === $uploads['error']))
    {
    return null;
    }
    else
    {
    $filename = wp_unique_filename( $uploads['path'], $filename );
    }

    // Move the file to the uploads dir
    $new_file = $uploads['path'] . "/$filename";

    // does server allow_url_fopen - if not use file_get_contents
    if (!ini_get('allow_url_fopen'))
    {
    $file_data = eci_curlthefilecontents($imageurl);
    }
    else
    {
    $file_data = @file_get_contents($imageurl);
    }

    // if we don't get contents of uploads folder then its a fail
    if (!$file_data)
    {
    return null;
    }

    eci_inf('New Media New File Path','Path: '.$new_file,__LINE__,__FILE__,__FUNCTION__,$set);

    // write image to content directory
    file_put_contents($new_file, $file_data);

    // Set correct file permissions
    $stat = stat( dirname( $new_file ));
    $perms = $stat['mode'] & 0000666;
    @ chmod( $new_file, $perms );

    // get filetype
    $wp_filetype = wp_check_filetype( $filename, $mimes = NULL );

    // extract returned array values from wp_check_filetype
    extract( $wp_filetype );

    // No file type! do not proceed further
    if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
    {
    return null;
    ############## ADD ECI LOG ENTRY HERE ###########
    }

    // build url for including in attachment object
    $url = $uploads['url'] . "/$filename";

    // build attachment object
    $att = array(
    'post_mime_type' => $type,
    'post_content' => '',
    'guid' => $url,
    'post_parent' => null,
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
    );

    eci_inf('New Media Attachment GUID URL','URL (pre wp_insert_attachment): '.$url,__LINE__,__FILE__,__FUNCTION__,$set);

    // action insert attachment now
    $thumbid = wp_insert_attachment($att, $url, $postid);

    // only update meta data if no errors from wp_insert_attachment - thuumb id returned
    if ( !is_wp_error( $thumbid ) )
    {
    // we required image functions (2.9 changes)
    require_once(ABSPATH . '/wp-admin/includes/image.php');

    var_dump($new_file,$thumbid, wp_generate_attachment_metadata( $thumbid, $new_file ));

    // update meta data
    // wp_update_attachment_metadata( $thumbid, wp_generate_attachment_metadata( $thumbid, $new_file ) );

    // return the new thumbs id
    return $thumbid;
    }

    return null;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.