I am writing a plugin for wp. At the moment, at the stage of outputting images via a plugin using ajax. Actually, here is the code snippet of the function handler in php:

if(!empty($_POST['img_filter'])) { $source = __DIR__.'/MIW 600/'.$_POST['img_filter'].'.jpg'; echo $source; } 

and jQuery code snippet:

 jQuery("#filter_number").on("keyup", function () { var d = jQuery("#filter_number").val(); if(d != null && d != '') { jQuery.ajax({ type: "POST", url: ajax_object.ajaxurl, data: { 'action': 'ajax_object', 'img_filter': d }, success: function (data) { jQuery("#drawing").attr('src', data); console.log(data); } }); } }); 

After sending an ajax request, the "data" contains the path to the file, but the picture itself is not displayed, and the console displays an error:

 Not allowed to load local resource: file:///C:/OpenServer/domains/wordpress-test/wp-content/plugins/oil-filters/MIW%20600/H1013.jpg` 

Who knows how to fix this problem?

    1 answer 1

    I believe that the matter is in the ways. __DIR__ should be used when you need an absolute path to the directory, and you like the image url? Try this

     $source = WP_PLUGIN_URL . '/oil-filters/MIW%20600/' . $_POST['img_filter'] . '.jpg'; 
    • And it really helped, thanks a lot) - Successful_12