1

I have created a file for ajax in theme/mytheme/ajax-form.php and there was an error while using this code:

$db_result = $wpdb->get_results( 'select * from batch_number' );

Error: get_result is not a function

Then I've included wp-config.php and wp-load.php at the top, so the issue was fixed and I got the result, but now the issue is: I'm hard coding the path of wp-config.php file. Instead, can we take the root path of my project?

I've tried many things, but no luck. I've tried this:

require_once( $_SERVER['DOCUMENT_ROOT'] . '/wptheme/wp-load.php' );

wptheme is my project's root folder and I don't want to hardcode it like that.

  • 2
    Any particular reason you're not using admin-ajax or the REST API? Those are the preferred way to do AJAX on WordPress. – Jacob Peattie May 20 at 5:24
  • basically i am learning wordpress coding , i dont know much about wordpress but somehow i have tried and achieved ajax but only this issue is left – Flutterer May 20 at 5:26
  • Hi, welcome to WPSE! It's OK that you are learning, testing & breaking some things yourself. There is nothing wrong in that. However, it's better if you learn from the correct source, otherwise you may not even know that you are doing it wrong, until it's too late. Try to learn from the documents found HERE. – Fayaz May 20 at 9:24
  • @Fayaz ye i will definitely look the correct way , but i am willing to learn this thing it is done but i just dont want to hard code the path of wp-config file so i want to know how can we call this – Flutterer May 20 at 14:57
  • There is no correct answer. Because in WordPress it's possible to change Theme directory. However, if theme directory isn't changed, then you can use relative path, like: require_once( '../../../wp-load.php' );. This will work since by default the theme files are in wp-content/themes/theme-name/ directory. – Fayaz May 20 at 21:08
2

First of all your ajax doing approach is wrong when WordPress offer us great way to do this. Read this one why you should't use your current approach and then read https://codex.wordpress.org/AJAX_in_Plugins how you can do ajax with wp_ajax hook. It's really simple and easy. using this hook you don't need to call external file neither need to load wp-load.php

Anyway to include wp-load.php file just use ABSPATH like this

$rootPath = str_replace(ABSPATH,"",getcwd());

Then include

include( $rootPath . '/wp-load.php' );

Moral, For using ajax in WordPress used WordPress wp_ajax hook instead any others.

  • yes you are right i will try to use built in features but right now i am learning so i can practice different things right ? – Flutterer May 20 at 6:07
  • i have tried your way of getting path but it is not working first erro ABSPATH is not defined ... second if i define ABSPATH as it is defined in wp-config.php then it is getting the path of the theme folder . not the root directory – Flutterer May 20 at 6:09
  • 1
    Yeah sure, I did this type of practice before learning about WordPress feature :P . So my opinion it's not bad thing to do but just let you know that WordPress offer great feature to do this and off-course use WordPress feature when you did in real project. – Mohammad Limon May 20 at 6:10
  • ABSPATH is a PHP constant defined by WordPress at the bottom of wp-config.php: – Mohammad Limon May 20 at 6:11
  • :) yes i know i have seen it on all other files wp-config file is auto load but if i create file in themes directory forexample like this ajax-form.php then in this file we have to call wp-config file ... and ABSPATH is not callable here – Flutterer May 20 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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