Hello,
I’ve stumbled on the “403 Forbidden error” for which many explanations/solutions can be found in many places on the net.
However, I struggle for several hours for solving this error in my situation : inside a WordPress plugin.
I have a WordPress plugin, let’s call it “foobar”, that processes a shortcode.
My plugin’s php file, foobar.php, builds and returns at some point the Javascript code:
$myvariable = '
$.ajax({
url: "'.plugin_dir_url( __FILE__ ) .'myPHPFunction.php",
'.type: "post",
success: function(response) { console.log("return from myPHPFunction.php:"+response); },
error: function(xhr,status,error) { console.log("status="+status+",xhr="+xhr+",errth="+error);}
});';
return $myvariable;
The myPHPFunction.php file is in the same directory as the plugin’s.
myPHPFunction.php is very powerful : ![😉]()
<?php
echo "abcd";
?>
When I open a page containing the shortcode, it builds and runs the Javascript and I get in the browser console :
status=error,xhr=<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /wp-content/plugins/foobar/myPHPFunction.php
on this server.</p>
</body></html>
,errth=Forbidden
I’ve checked the following:
– myPHPFunction.php has chmod 777
– All other plugins deactivated
If it is due to any rule in my .htaccess, I don’t know what to check..
I have tried to include myPHPFunction.php with a:
include(plugin_dir_path( __FILE__ ) .'myPHPFunction.php');
but it didn’t change anything..
Note that when I run the generated Javascript in an HTML page at the root of my website :
(excerpt)
<body>
<script type="text/javascript">
jQuery(document).ready(function($){
$.ajax({
url: "myPHPFunction.php",
type: "post",
success: function(response) { console.log("return from myPHPFunction.php:"+response); }
});
});
</script>
</body>
myPHPFunction.php being also at the root, I get:
return from myPHPFunction.php:abcd
so it works.
That’s definitely the context of running the code from my plugin’s php page that makes the difference.
Any help appreciated..
At last, just in case..
I’m using MAMP 4.4.1, WordPress 5.3, PHP 7.2.1, Firefox 71.0 (64 bits) ..
Thanks.