admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('bg_remover_nonce')
]
);
}
add_action('wp_enqueue_scripts', 'bg_remover_enqueue_assets');
// Shortcode to render the tool
function bg_remover_shortcode() {
ob_start();
?>
'POST',
'headers' => [
'X-Api-Key' => $api_key,
],
'body' => [
'image_file' => new CURLFile($file['tmp_name'], $file['type'], $file['name']),
'size' => 'auto',
],
];
$response = wp_remote_post($url, $args);
if (is_wp_error($response)) {
wp_send_json_error('API request failed: ' . $response->get_error_message());
}
$body = wp_remote_retrieve_body($response);
$status = wp_remote_retrieve_response_code($response);
if ($status !== 200) {
wp_send_json_error('API error: ' . $body);
}
// Save the result temporarily
$upload_dir = wp_upload_dir();
$filename = 'bg_removed_' . uniqid() . '.png';
$filepath = $upload_dir['path'] . '/' . $filename;
file_put_contents($filepath, $body);
wp_send_json_success([
'url' => $upload_dir['url'] . '/' . $filename
]);
}
add_action('wp_ajax_bg_remover_process', 'bg_remover_process_image');
add_action('wp_ajax_nopriv_bg_remover_process', 'bg_remover_process_image');
?>
Background Remover
Upload an image to remove its background instantly.
Drag & Drop your image here or click to upload