get_option(‘admin_email’) not working in wordpress when using ajax call

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:


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

Read more about this at: