• Resolved Dan-BTP

    (@dan-btp)


    I am running on a Windows 7 system with ASP.NET and .NET Framework 4.5. I am writing a VB.NET program to generate graphics files (JPEG and PNG). I need to insert these into the media library for a WordPress blog. I am brand new to WordPress and PHP. I need a script to do this. (I know how to do it through the dashboard of my blog, but I need to be able to automate it.)

    I found a PHP code snippet that looks close.

    <?php
      $filename = 'C:\MyFolder\MyImage.jpg';
      $wp_filetype = wp_check_filetype(basename($filename), null );
      $wp_upload_dir = wp_upload_dir();
      $attachment = array(
         'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
         'post_mime_type' => $wp_filetype['type'],
         'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
         'post_content' => '',
         'post_status' => 'inherit'
      );
      $attach_id = wp_insert_attachment( $attachment, $filename, 0 );
      // you must first include the image.php file
      // for the function wp_generate_attachment_metadata() to work
      require_once( ABSPATH . 'wp-admin/includes/image.php' );
      $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
      wp_update_attachment_metadata( $attach_id, $attach_data );
    ?>

    But I don’t know how to tell it which blog to insert it into.

    And I don’t know if I can use Windows syntax for specifying the file name.

    Please help! Thanks!

  • The topic ‘Script to add images to media library’ is closed to new replies.