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.

I have gone through the Wordpress Codex and several other threads/tutorials about this topic, however I am extremely confused about what I am doing wrong.

Javascript File

function load_blog_posts_function() 
     url: "wp-content/themes/Divi/js/stockchart/blog-list-query.php",
     data: ({action : 'load_blog_posts_sidebar'}),
     success: function(data) {

PHP File

function load_blog_posts_sidebar()
$stockSymbol = $_GET['symbol'];
/* Set up the query arguments. */
$options = get_option("widget_rpwe_widget");
$args = array('fb' => $cat);
$default_args = apply_filters( 'rpwe_default_query_arguments', $args );
$rpwewidget = get_posts($default_args);
//echo $options;
echo("hello world");

I also read that some code was needed in my functions.php, so I have this:

$dirName = dirname(__FILE__);
$baseName = basename(realpath($dirName));
require_once ("$dirName/js/stockchart/blog-list-query.php");

add_action("wp_ajax_nopriv_load_blog_posts_sidebar", "load_blog_posts_sidebar");
add_action("wp_ajax_load_blog_posts_sidebar", "load_blog_posts_sidebar");

I currently get two errors when I try to run this, the more important one is

Call to undefined function get_option()

which tells me that my php script is not recognizing Wordpress functions and I also get this one:

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at .../www.3weekwebsites.com/wp-content/themes/Divi/js/stockchart/blog-list-query.php:12) in .../www.3weekwebsites.com/wp-content/themes/Divi/header.php on line 1

Could anyone please share some insight here? I would really appreciate it. :)

1 Answer 1


You're firing a request to a PHP file that won't have the WordPress library loaded - hence the undefined function error.

You could manually load in WordPress with something like require '../../../wp-load.php'.

Or, better yet, use the awesome AJAX API. Just fire your request to wp-admin/admin-ajax.php and then register a callback in your plugin or theme that matches the action parameter (in your case, load_blog_posts_sidebar):

function wpse_147692_load_blog_posts_sidebar() {
    // Do something!

add_action( 'wp_ajax_nopriv_load_blog_posts_sidebar', 'wpse_147692_load_blog_posts_sidebar' );
add_action( 'wp_ajax_load_blog_posts_sidebar',        'wpse_147692_load_blog_posts_sidebar' );
  • 1
    Loading the wp-load.php is never suggested. You should always make the call to admin-ajax.php as you have mentioned. Commented Jun 5, 2014 at 15:37
  • Ok so I changed my ajax - url: "wp-admin/admin-ajax.php" and I moved my php script into functions.php - function load_blog_posts_sidebar() {...}, however now it appears that my php script isn't executing? Commented Jun 5, 2014 at 16:08
  • Use an inspector like Firebug or HTTPFox to ensure your AJAX request is resolving to the correct URL (http://example.com/wp-admin/admin-ajax.php). Commented Jun 5, 2014 at 17:19
  • I figured it out today! I ended up using the Ajax API. Thanks to everyone for your help :) Commented Jun 6, 2014 at 19:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.