Referencing a directory in another folder wordpress image.php

Code to upload an image to wordpress site.

  function upload_image($file, $post_id) {

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

    // upload image to server
   media_sideload_image($file['url'], $post_id);

    // get the newly uploaded image
    $attachments = get_posts( array(
        'post_type' => 'attachment',
        'number_posts' => 1,
        'post_status' => null,
        'post_parent' => $post_id,
        'orderby' => 'post_date',
        'order' => 'DESC',) 
    );

    // returns the id of the image
    return $attachments[0]->ID;
}

$result1 = attach_image_url('http://www.americanmattress.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/p/a/parmelee_fm2.jpg','0');
print_r($result1);

Here is the problem:

Read More
  • Function is located in RootWPFolder/Somefolder/ThisFunction.php
  • The required files are at RootWPFolder/wp-admin/includes/file.php

I am getting following error:

Fatal error: require_once(): Failed opening required
‘ABSPATHwp-admin/includes/media.php’
(include_path=’.:/opt/alt/php54/usr/share/pear:/opt/alt/php54/usr/share/php’)

Related posts

Leave a Reply

1 comment

  1. The error message

    Failed opening required ‘ABSPATHwp-admin/includes/media.php’

    Suggests that ABSPATH isn’t defined. Open wp-load.php in the root, and make sure

    define( 'ABSPATH', dirname(__FILE__) . '/' ); 
    

    is set. If so, then use these to be safe instead of relative directories:

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