theme development - I want to get the home root path

I have created a file for ajax in thememythemeajax-form.php and there was an error while using this code:$db_result =

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.

Share Improve this question edited May 20, 2019 at 9:20 Fayaz 9,0172 gold badges33 silver badges51 bronze badges asked May 20, 2019 at 5:18 FluttererFlutterer 1159 bronze badges 5
  • 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 Commented May 20, 2019 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 Commented May 20, 2019 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 Commented May 20, 2019 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 Commented May 20, 2019 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 Commented May 20, 2019 at 21:08
Add a comment  | 

1 Answer 1

Reset to default 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/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

相关推荐

  • theme development - I want to get the home root path

    I have created a file for ajax in thememythemeajax-form.php and there was an error while using this code:$db_result =

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信