0

I have been struggling with this for a while now.

What I found is this:

<div id="include"></div>
        <script>
$(function(){
    $("#include").load("/test.php");
});
    </script>   

This does work when in test.php is a simple code like:

<?php echo 'echo world'; ?>

But it doesn't work when I want to grab Wordpress variables.

<?php   $current_user_id =  $current_user->ID;
echo $current_user_id; ?>

There's nothing outputed. No error. Just a blank.

It's like it doesn't send the variables to the test.php. So test.php doesn't echo values filled. When I use <?php include 'test.php';?> it does fill the variables and works as I wanted.

So basically, .load simply loads the file.

But I want <?php include;?> to be executed instead of .load.

When I use <?php include;?> I can grab variables, which I need.

Is there other command instead of .load to do that?

Any help would be appreciated.

7
  • stackoverflow.com/questions/9652328/… this answer your question? Commented Jul 13, 2021 at 11:58
  • No it doesn't. As I said .load doesn't include php. It only loads. I have already seen this thread. Commented Jul 13, 2021 at 12:04
  • Please elaborate on “doesn’t work”. With full PHP error reporting turned on, as well as WP_DEBUG on, do you see an error? When you are loading your PHP file, is there code before your first usage of $current_user that boots WordPress, since calling a PHP manually won’t boot it for you? Commented Jul 13, 2021 at 12:07
  • Sorry. There's nothing outputed. No error. Just a blank. It's like it doesn't send the variables to the test.php. So test.php output a blank. When I use <?php include 'test.php';?> it does fill the variables and works as I wanted. Commented Jul 13, 2021 at 12:12
  • 1
    You will need to include the basic WP loader into your own script then, for something like this to work - see wordpress.stackexchange.com/questions/245364/… Commented Jul 13, 2021 at 13:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.