Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I upload an image to the media library using this? #7

Open
tjhunkin-inhance opened this issue Jan 31, 2024 · 1 comment
Open

Comments

@tjhunkin-inhance
Copy link

How do I upload an image to the media library using this?

@tjhunkin-inhance
Copy link
Author

This seems to work. Note that you need to edit the user on WordPress and add an application password on their profile for this to work.

$client = resolve(WordPress::class);
$client->setUrl('https://www.example.com');
$client->setUsername('***');
$client->setPassword('***');

$fileName = public_path('storage/81/307880052-1.jpg');

$file = new UploadedFile(
	$fileName,
	'307880052-1.jpg',
	'image/jpeg',
	filesize($fileName),
	false
);


$values = array(
	"date" => "2015-11-26 10:00:00",
	"date_gmt" => "2015-11-26 09:00:00",
	"modified" => "2015-11-26 10:00:00",
	"modified_gmt" => "2015-11-26 09:00:00",
	"status" => "future",
	"title" => "Titre media",
	"description" => "description media",
	"media_type" => "image",
	"source_url" => "https://example/storage/81/307880052-1.jpg"
);

$response = $client->media()->create($file,$values);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant