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.
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.
1 Answer
Reset to default 2First 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/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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745355357a4624083.html
require_once( '../../../wp-load.php' );
. This will work since by default the theme files are inwp-content/themes/theme-name/
directory. – Fayaz Commented May 20, 2019 at 21:08