$attachment = [
'post_title' => wp_basename($file['file']),
'post_content' => $file['url'],
'post_mime_type' => $file['type'],
'post_parent' => $post_id,
'guid' => $file['url'],
];
$attachment_id = wp_insert_attachment($attachment, $file['file']);
wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file['file']));
set_post_thumbnail($post_id, $attachment_id);
<script>
let response = await fetch({{ route('admin.page.storeFiles', $page->id) }}, {
method: 'POST',
body: formData,
});
</script>
public function storeFiles(Request $request, Page $page)
{
$file = $request->file('file')->getRealPath();
$media = $page->addMedia($file)->toMediaCollection('page_images');
return response()->json($media);
}
Route::post('{page}', 'storeFiles')->name('admin.page.storeFiles');
let response = await fetch(config.storeFilesUrl, {
method: 'POST',
headers: {
'X-CSRF-TOKEN': config.csrf,
},
body: formData,
});
let image = await response.json();
namespace App\Http\Controllers;
use App\Models\Page;
use VanOns\Laraberg\Blocks\BlockParser;
class HomeController extends Controller
{
//
public function index(BlockParser $parser)
{
$page = Page::where('slug', 'home')->firstOrFail();
$blocks = $parser->parse($page->content);
$output = '';
foreach ($blocks as $block) {
$output .= $block->render();
}
$page->content = $output;
return view('home', ['page' => $page]);
}
}
global $ts_mail_errors, $phpmailer;
$send = wp_mail($user_email, $title, $message, $headers);
if ($send) {
// nice
} else {
if (isset($phpmailer)) {
$ts_mail_errors[] = $phpmailer->ErrorInfo;
}
if ($ts_mail_errors) {
// errors
}
}