dreamsoftin / flutter_wordpress

Flutter WordPress API
https://pub.dartlang.org/packages/flutter_wordpress
MIT License
198 stars 122 forks source link

featuredMedia doesn't get uploaded in createPost function #42

Closed GarvMaggu closed 4 years ago

GarvMaggu commented 4 years ago

When creating a post, featuredMedia is not getting uploaded to Wordpress nor it is setting as a featuredImage for the post, I tried passing many random .jpg and .png URLs from the internet but none of them worked. However, title, content, excerpt, and rest everything is working fine and a post is being created but without the Featured Media

GarvMaggu commented 4 years ago

Here is my code:

createPost() async {
  wp.User user;
  Future<wp.User> response = wordPress.authenticateUser(
    username: '*****',
    password: '***********',
  );
  await response.then((value) {
    user = value;
  });
  print('-------------');
  print(response);
  final post = await wordPress.createPost(
    post: wp.Post(
      title: 'Hello',
      content: 'Example content',
      excerpt: 'Example excerpt',
      authorID: user.id,
      commentStatus: wp.PostCommentStatus.closed,
      pingStatus: wp.PostPingStatus.closed,
      status: wp.PostPageStatus.publish,
      format: wp.PostFormat.standard,
      featuredMedia: 'https://upload.wikimedia.org/wikipedia/en/e/e6/Example_of_a_Shader.png',
    ),
  );
caercam commented 4 years ago

You can't upload media that way. You'd have to upload the file using the media endpoint from the REST API (which the flutter_wordpress does not support) and use the newly uploaded media ID when creating your new oost.

GarvMaggu commented 4 years ago

You can't upload media that way. You'd have to upload the file using the media endpoint from the REST API (which the flutter_wordpress does not support) and use the newly uploaded media ID when creating your new oost.

Ohh, okay. But then why there are two different properties for featuredMedia in createPost function? featuredMedia -> String (Source URL of the image) featuredMediaID -.> Integer (Media ID from Wordpress Media Library)

What is the function of featuredMedia property?

caercam commented 4 years ago

featuredMedia is a Post property that contains the media path (if any) when you fetch a Post, while featuredMediaID contains the media ID; but when you create a new Post featuredMedia is completely discarded and only featuredMediaID is actually sent to the API.

GarvMaggu commented 4 years ago

featuredMedia is a Post property that contains the media path (if any) when you fetch a Post, while featuredMediaID contains the media ID; but when you create a new Post featuredMedia is completely discarded and only featuredMediaID is actually sent to the API.

Ohh, perfect, completely understood it now. Thanks a lot for clearing it out.

GarvMaggu commented 4 years ago

You can add these lines from this commit to upload files to WordPress Media Library: https://github.com/dreamsoftin/flutter_wordpress/pull/45/commits/7ba21f6124817e8cb40f968582e06a9084fb831c