<?php
function ajax_get_remote_image(){
// Endpoint: /wp-admin/admin-ajax.php?action=ajax_get_remote_image&q=
$tmphtml = '';
if ( !isset($_GET["q"]) ) exit;
$url = $_GET["q"];
$post_array = array();
$header_authorization = "";
$response = wp_remote_get( $url, array( // wp_remote_post
'method' => 'GET', // 'POST'
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(
// 'Authorization' => 'Bearer ' . $header_authorization,
),
'body' => $post_array,
'cookies' => array()
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else {
$tmphtml = wp_remote_retrieve_body($response);
}
echo $tmphtml;
// Exit is required.
exit();
}
add_action('wp_ajax_ajax_get_remote_image', 'ajax_get_remote_image'); // For Logged-in Users
add_action('wp_ajax_nopriv_ajax_get_remote_image', 'ajax_get_remote_image' ); // For Unauthenticated Users
Related Keywords: Developer, How-to, Solved, Sample Code