1

In order to validate the installation of WordPress instances, we are writing Python unit tests. One of the test should perform the following action: upload an image to WordPress.

In order to do that, I am using the Requests library.

When I inspect the form within /wp-admin/media-new.php page through Firebug (form information, I get the following information): Form Id: file-form Name Method: post Action: http://localhost:8000/wp-admin/media-new.php

Elements id: plupload-browse-button type: button value: Select Files

id:async-upload name: async-upload type: file label: Upload

id:html-upload name: html-upload type: submit value: Upload

id: post_id name: post_id type: hidden value: 0

id: _wpnonce name: _wpnonce type: hidden value: c0fc3b80bb

id: file-form name: _wp_http_referer type: hidden value: /wp-admin/media-new.php

I believe that the _wpnonce is a unique value generated for each session. Therefore, before trying to upload the file, I get the media-new.php page and grab the _wpnonce in the form (hence the variable in my code).

My code is the following:

    with open('1.jpg', 'rb') as f:
        upload_data = {'post_id': '0',
                       '_wp_http_referer': '/wp-admin/media-new.php',
                       '_wpnonce': wp_nonce,
                       'action': 'upload_attachement',
                       'name': '1.jpg',
                       'async-upload': f,
                       'html-upload': 'Upload'}
        upload_result = session.post('http://localhost:8000/wp-admin/media-new.php', upload_data)

The code runs fine and the upload_result.status_code equals 200.

However, the image never shows up in the media gallery of WordPress.

I believe this a simple error, but I can't figure out what I'm missing.

Thanks in advance for the help.

E. Jaep
  • 2,095
  • 1
  • 30
  • 56
  • I think this [link](https://wordpress.stackexchange.com/questions/240487/media-files-exist-in-upload-folder-but-not-showing-up) will help you. Check uploads folder permissions. – abhijit Sep 13 '17 at 18:13

1 Answers1

1

If you want to post files you should use the files parameter. Also the '_wpnonce' value is not enough to get authenticated, you need to have cookies.

url = 'http://localhost:8000/wp-admin/media-new.php'
data = {
    'post_id': '0',
    '_wp_http_referer': '/wp-admin/media-new.php',
    '_wpnonce': wp_nonce,
    'action': 'upload_attachement',
    'html-upload': 'Upload'
}
files = {'async-upload':('1.jpg', open('1.jpg', 'rb'))}
headers = {'Cookie': my_cookies}
upload_result = session.post(url, data=data, files=files, headers=headers)

I'm assuming that you have acquired valid cookies from your browser. If you want to get authenticated with requests check my answer to this post: login-wordpress-with-requests

t.m.adam
  • 15,106
  • 3
  • 32
  • 52
  • Thanks, it did work. However, I did not need the cookies. I believe I did not need them since I was still in the same session. – E. Jaep Sep 13 '17 at 18:59
  • Yes, if you have an authenticated session you don't need to worry about cookies. – t.m.adam Sep 13 '17 at 19:12