Question: So I've been working in Wordpress lately with Ajax and I managed to get some stock data into my site from my database. However, I am now trying to use Ajax to load Wordpress posts into my left sidebar, which requires the use of Wordpress functions and it seems as if I am not doing this properly.get\_option() shows NULL on this.

JS

```
 url:"<?php echo get_template_directory_uri(); ?>/sendContactForm.php",//request URL
                type:"POST",//Request type GET/POST
                data: $(_this).serialize()

```

PHP(sendContactForm.php)

```
$result = new stdClass();
$from = $_REQUEST['email'];
$message = $_REQUEST['message'];
$sender = $_REQUEST['email'];
$result->message = $message;  
$result->subject = $_REQUEST['name'] . " send you a message";
$result->to = "get_option('admin_email');

$result->to = "get_option('admin_email');  showing NULL

```

Answer: 
I confess that I'm slightly confused by how your sending email function loads posts into your sidebar...the code does not seem to be doing the thing that you described.

Are you loading WordPress in sendContactForm.php? Is what you've shown here the only code in sendContactForm.php?

`get_option()` is a WordPress function, so you need to load WordPress to do this. This is usually done by putting something at the top of your php file like:

`require('../../../wp-load.php');`

But that's a bit hacky.

The proper way to do AJAX in WordPress is to send your POST request to `/wp-admin/admin-ajax.php` with an parameter like `action=sendContactForm` and then create an action hook like this:

```
add_action('wp_ajax_sendContactForm', 'my_email_function');
add_action('wp_ajax_nopriv_sendContactForm', 'my_email_function');

function my_email_function() {
    // process request here
    wp_die();
}

```

Read more about this at: <https://codex.wordpress.org/AJAX_in_Plugins>
You can get the email address in the WordPress code and add it to a data attribute like: 

```
<div class="container">
    <div class="whatever-your-class" data-email="<?php echo get_option('admin_email'); ?>">
    </div>
</div>

```

And then in your AJAX code, something like this: 

```
$post = $(this);
$.ajax({
   url: "test.php",   // where you are posting to
   type: "GET",
   data: { email: $($email).attr("data-email") }, 
   success: function(response) {
      alert(response);  
   }
}); 

```

Does that make sense? You are echoing the email address on to your page template and then using jQuery to grab it and place it in your AJAX call. It's messy but it should work.