I can’t access wordpress functions from an ajax php call

You are making a new request to the server and you appear to be loading a page that is not loading WordPress core functions. I’d need to see your HitCount.php to confirm this but I can’t think of another explanation.

Loading WordPRess files piecemeal is tricky and prone to breakage as core code changes. You can probably get it working by loading wp-load.php, wp-settings.php, or wp-blog-header.php. I believe the latter is the officially correct one but it will load theme functions you probably don’t need. I don’t know for sure about that because I haven’t used any of those tricks since the AJAX API became available, which brings me to…

The safest thing to do is use the AJAX API, and that is what I recommend unless you really know WordPRess core files and load sequence very very well, and also need extremely quick response. The AJAX API loads a lot so it can be slow, though in personal experience I have never noticed a lag worth worrying about.

Leave a Comment