admin管理员组文章数量:1415467
I'm trying to use a class from another file within a wp_ajax hook And the call returns a 500 status code
I added the class with incloud (),
I tried to put the whole class inside the function and it worked fine.
Is there a way to use a class file within the wp_ajax function?
This is the code in the function.php file:
add_action( 'init', 'ajax_import_rows_init' );
function ajax_import_rows_init() {
wp_register_script( 'ajax_import_rows', get_stylesheet_directory_uri().'/js/ajax_import_rows.js', array('jquery') );
wp_enqueue_script( 'ajax_import_rows' );
$rows_nonce = wp_create_nonce( 'ajax_rows_nonce' );
wp_localize_script( 'ajax_import_rows', 'ajax_import_opject', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => $rows_nonce
));
add_action('wp_ajax_import_list_rows', 'response_list_rows');
add_action('wp_ajax_nopriv_import_list_rows', 'response_list_rows');
}
function response_list_rows(){
check_ajax_referer( 'ajax_rows_nonce', 'nonce' );
include_once(get_stylesheet_directory_uri() . '/fileds.php');
$post_id = $_POST['post_id'];
$list_rows = new fields_ACF($post_id);
$list_rows_json = json_encode( $list_rows -> fileds);
echo( $list_rows_json);
die();
}
This is the class in the fields.php file:
class fields_ACF{
public $fileds =array();
public function fields_ACF($post_id){
$income = $this -> income($post_id);
$expenses = $this -> expenses($post_id);
$debts = $this -> debts($post_id);
return(
$this -> fileds["income"] = $income and
$this -> fileds["expenses"] = $expenses and
$this -> fileds["debts"] = $debts
);
//$this -> fileds = array();
}
//פונקציה להכנסות
private function income($post_id){
$a =array();
$fileds = array();
if( have_rows('revenue', $post_id) ):
while(have_rows('revenue', $post_id) ): the_row();
/*****הוצאת כמות שיש מכל קטגוריה***********/
array_push($a, get_sub_field('id_cat', $post_id));
$count_rows_registered = array_count_values($a);
endwhile;
endif;
if( have_rows('types_income', 'option') ):
while(have_rows('types_income', 'option') ): the_row();
$type_no = get_sub_field('type_income_no');
$title = get_sub_field('type_income_name');
if( have_rows('cat_income', 'option') ):
while( have_rows('cat_income', 'option') ): the_row();
$cat_income_no = get_sub_field('cat_income_no');
$cat_income_name = get_sub_field('cat_income_name');
if( have_rows('settings_cat') ):
while( have_rows('settings_cat') ): the_row();
$day_field = get_sub_field('day_in_months');
$month_field = get_sub_field('months');
endwhile;
endif;
$fileds[$type_no]['name']=$title;
$fileds[$type_no]['content'][$cat_income_no]['name']= $cat_income_name;
$fileds[$type_no]['content'][$cat_income_no]['settings']=[
'day_field' => $day_field,
'month_field' => $month_field
];
$fileds[$type_no]['content'][$cat_income_no]['count_rows_registered']=$count_rows_registered[$cat_income_no];
$fileds[$type_no]['content'][$cat_income_no]['rows_registered']=[];
endwhile;
endif;
endwhile;
endif;
if( have_rows('revenue', $post_id) ):
while(have_rows('revenue', $post_id) ): the_row();
/********הכנסת תוכן השורות במערך הכללי********/
$row =array(
'index_row' => get_row_index(),
'date_registered' => get_sub_field('Date_typed'),
'type' => get_sub_field('type'),
'ID_type' => get_sub_field('id_type'),
'category_name' => get_sub_field('category_Revenue'),
'ID_category' => get_sub_field('id_cat'),
'dete_in_payment' => get_sub_field('Date_payment'),
'Amount' => get_sub_field('total'),
'method_payment' => get_sub_field('method')
);
$fileds[$row['ID_type']]['content'][$row['ID_category']]['rows_registered'][]=$row;
endwhile;
endif;
return($fileds);
}
//פונקציה להוצאות
private function expenses($post_id){
$a =array();
$fileds = array();
if( have_rows('expenses', $post_id) ):
while(have_rows('expenses', $post_id) ): the_row();
/*****הוצאת כמות שיש מכל קטגוריה***********/
array_push($a, get_sub_field('id_cat', $post_id));
$count_rows_registered = array_count_values($a);
endwhile;
endif;
if( have_rows('types_expenses', 'option') ):
while(have_rows('types_expenses', 'option') ): the_row();
$type_no = get_sub_field('type_expenses_no');
$title = get_sub_field('type_expenses_name');
if( have_rows('cat_expenses', 'option') ):
while( have_rows('cat_expenses', 'option') ): the_row();
$cat_income_no = get_sub_field('cat_expenses_no');
$cat_income_name = get_sub_field('cat_expenses_name');
if( have_rows('settings_cat') ):
while( have_rows('settings_cat') ): the_row();
$day_field = get_sub_field('day_in_months');
$month_field = get_sub_field('months');
endwhile;
endif;
$fileds[$type_no]['name']=$title;
$fileds[$type_no]['content'][$cat_income_no]['name']= $cat_income_name;
$fileds[$type_no]['content'][$cat_income_no]['settings']=[
'day_field' => $day_field,
'month_field' => $month_field
];
$fileds[$type_no]['content'][$cat_income_no]['count_rows_registered']=$count_rows_registered[$cat_income_no];
$fileds[$type_no]['content'][$cat_income_no]['rows_registered']=[];
endwhile;
endif;
endwhile;
endif;
if( have_rows('expenses', $post_id) ):
while(have_rows('expenses', $post_id) ): the_row();
/********הכנסת תוכן השורות במערך הכללי********/
$row =array(
'index_row' => get_row_index(),
'date_registered' => get_sub_field('Date_typed'),
'type' => get_sub_field('type'),
'ID_type' => get_sub_field('id_type'),
'category_name' => get_sub_field('category_expenses'),
'ID_category' => get_sub_field('id_cat'),
'dete_in_payment' => get_sub_field('Date_payment'),
'Amount' => get_sub_field('total'),
'method_payment' => get_sub_field('method')
);
$fileds[$row['ID_type']]['content'][$row['ID_category']]['rows_registered'][]=$row;
endwhile;
endif;
return $fileds;
}
//פונקציה לחובות
private function debts($post_id){
$a =array();
$fileds = array();
if( have_rows('debts', $post_id) ):
while(have_rows('debts', $post_id) ): the_row();
/*****הוצאת כמות שיש מכל קטגוריה***********/
array_push($a, get_sub_field('id_cat', $post_id));
$count_rows_registered = array_count_values($a);
endwhile;
endif;
if( have_rows('types_income', 'option') ):
while(have_rows('types_income', 'option') ): the_row();
$type_no = get_sub_field('type_income_no');
$title = get_sub_field('type_income_name');
if( have_rows('cat_income', 'option') ):
while( have_rows('cat_income', 'option') ): the_row();
$cat_income_no = get_sub_field('cat_income_no');
$cat_income_name = get_sub_field('cat_income_name');
if( have_rows('settings_cat') ):
while( have_rows('settings_cat') ): the_row();
$day_field = get_sub_field('day_in_months');
$month_field = get_sub_field('months');
endwhile;
endif;
$fileds[$type_no]['name']=$title;
$fileds[$type_no]['content'][$cat_income_no]['name']= $cat_income_name;
$fileds[$type_no]['content'][$cat_income_no]['settings']=[
'day_field' => $day_field,
'month_field' => $month_field
];
$fileds[$type_no]['content'][$cat_income_no]['count_rows_registered']=$count_rows_registered[$cat_income_no];
$fileds[$type_no]['content'][$cat_income_no]['rows_registered']=[];
endwhile;
endif;
endwhile;
endif;
if( have_rows('debts', $post_id) ):
while(have_rows('debts', $post_id) ): the_row();
/********הכנסת תוכן השורות במערך הכללי********/
$row =array(
'index_row' => get_row_index(),
'date_registered' => get_sub_field('Date_typed'),
'type' => get_sub_field('type'),
'ID_type' => get_sub_field('id_type'),
'category_name' => get_sub_field('category_debts'),
'ID_category' => get_sub_field('id_cat'),
'dete_in_payment' => get_sub_field('total_debt'),
'Amount' => get_sub_field('total'),
'method_payment' => get_sub_field('method')
);
$fileds[$row['ID_type']]['content'][$row['ID_category']]['rows_registered'][]=$row;
endwhile;
endif;
return $fileds;
}
}
Again I note that when I put the class code inside the function directly - it works
I'm trying to use a class from another file within a wp_ajax hook And the call returns a 500 status code
I added the class with incloud (),
I tried to put the whole class inside the function and it worked fine.
Is there a way to use a class file within the wp_ajax function?
This is the code in the function.php file:
add_action( 'init', 'ajax_import_rows_init' );
function ajax_import_rows_init() {
wp_register_script( 'ajax_import_rows', get_stylesheet_directory_uri().'/js/ajax_import_rows.js', array('jquery') );
wp_enqueue_script( 'ajax_import_rows' );
$rows_nonce = wp_create_nonce( 'ajax_rows_nonce' );
wp_localize_script( 'ajax_import_rows', 'ajax_import_opject', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => $rows_nonce
));
add_action('wp_ajax_import_list_rows', 'response_list_rows');
add_action('wp_ajax_nopriv_import_list_rows', 'response_list_rows');
}
function response_list_rows(){
check_ajax_referer( 'ajax_rows_nonce', 'nonce' );
include_once(get_stylesheet_directory_uri() . '/fileds.php');
$post_id = $_POST['post_id'];
$list_rows = new fields_ACF($post_id);
$list_rows_json = json_encode( $list_rows -> fileds);
echo( $list_rows_json);
die();
}
This is the class in the fields.php file:
class fields_ACF{
public $fileds =array();
public function fields_ACF($post_id){
$income = $this -> income($post_id);
$expenses = $this -> expenses($post_id);
$debts = $this -> debts($post_id);
return(
$this -> fileds["income"] = $income and
$this -> fileds["expenses"] = $expenses and
$this -> fileds["debts"] = $debts
);
//$this -> fileds = array();
}
//פונקציה להכנסות
private function income($post_id){
$a =array();
$fileds = array();
if( have_rows('revenue', $post_id) ):
while(have_rows('revenue', $post_id) ): the_row();
/*****הוצאת כמות שיש מכל קטגוריה***********/
array_push($a, get_sub_field('id_cat', $post_id));
$count_rows_registered = array_count_values($a);
endwhile;
endif;
if( have_rows('types_income', 'option') ):
while(have_rows('types_income', 'option') ): the_row();
$type_no = get_sub_field('type_income_no');
$title = get_sub_field('type_income_name');
if( have_rows('cat_income', 'option') ):
while( have_rows('cat_income', 'option') ): the_row();
$cat_income_no = get_sub_field('cat_income_no');
$cat_income_name = get_sub_field('cat_income_name');
if( have_rows('settings_cat') ):
while( have_rows('settings_cat') ): the_row();
$day_field = get_sub_field('day_in_months');
$month_field = get_sub_field('months');
endwhile;
endif;
$fileds[$type_no]['name']=$title;
$fileds[$type_no]['content'][$cat_income_no]['name']= $cat_income_name;
$fileds[$type_no]['content'][$cat_income_no]['settings']=[
'day_field' => $day_field,
'month_field' => $month_field
];
$fileds[$type_no]['content'][$cat_income_no]['count_rows_registered']=$count_rows_registered[$cat_income_no];
$fileds[$type_no]['content'][$cat_income_no]['rows_registered']=[];
endwhile;
endif;
endwhile;
endif;
if( have_rows('revenue', $post_id) ):
while(have_rows('revenue', $post_id) ): the_row();
/********הכנסת תוכן השורות במערך הכללי********/
$row =array(
'index_row' => get_row_index(),
'date_registered' => get_sub_field('Date_typed'),
'type' => get_sub_field('type'),
'ID_type' => get_sub_field('id_type'),
'category_name' => get_sub_field('category_Revenue'),
'ID_category' => get_sub_field('id_cat'),
'dete_in_payment' => get_sub_field('Date_payment'),
'Amount' => get_sub_field('total'),
'method_payment' => get_sub_field('method')
);
$fileds[$row['ID_type']]['content'][$row['ID_category']]['rows_registered'][]=$row;
endwhile;
endif;
return($fileds);
}
//פונקציה להוצאות
private function expenses($post_id){
$a =array();
$fileds = array();
if( have_rows('expenses', $post_id) ):
while(have_rows('expenses', $post_id) ): the_row();
/*****הוצאת כמות שיש מכל קטגוריה***********/
array_push($a, get_sub_field('id_cat', $post_id));
$count_rows_registered = array_count_values($a);
endwhile;
endif;
if( have_rows('types_expenses', 'option') ):
while(have_rows('types_expenses', 'option') ): the_row();
$type_no = get_sub_field('type_expenses_no');
$title = get_sub_field('type_expenses_name');
if( have_rows('cat_expenses', 'option') ):
while( have_rows('cat_expenses', 'option') ): the_row();
$cat_income_no = get_sub_field('cat_expenses_no');
$cat_income_name = get_sub_field('cat_expenses_name');
if( have_rows('settings_cat') ):
while( have_rows('settings_cat') ): the_row();
$day_field = get_sub_field('day_in_months');
$month_field = get_sub_field('months');
endwhile;
endif;
$fileds[$type_no]['name']=$title;
$fileds[$type_no]['content'][$cat_income_no]['name']= $cat_income_name;
$fileds[$type_no]['content'][$cat_income_no]['settings']=[
'day_field' => $day_field,
'month_field' => $month_field
];
$fileds[$type_no]['content'][$cat_income_no]['count_rows_registered']=$count_rows_registered[$cat_income_no];
$fileds[$type_no]['content'][$cat_income_no]['rows_registered']=[];
endwhile;
endif;
endwhile;
endif;
if( have_rows('expenses', $post_id) ):
while(have_rows('expenses', $post_id) ): the_row();
/********הכנסת תוכן השורות במערך הכללי********/
$row =array(
'index_row' => get_row_index(),
'date_registered' => get_sub_field('Date_typed'),
'type' => get_sub_field('type'),
'ID_type' => get_sub_field('id_type'),
'category_name' => get_sub_field('category_expenses'),
'ID_category' => get_sub_field('id_cat'),
'dete_in_payment' => get_sub_field('Date_payment'),
'Amount' => get_sub_field('total'),
'method_payment' => get_sub_field('method')
);
$fileds[$row['ID_type']]['content'][$row['ID_category']]['rows_registered'][]=$row;
endwhile;
endif;
return $fileds;
}
//פונקציה לחובות
private function debts($post_id){
$a =array();
$fileds = array();
if( have_rows('debts', $post_id) ):
while(have_rows('debts', $post_id) ): the_row();
/*****הוצאת כמות שיש מכל קטגוריה***********/
array_push($a, get_sub_field('id_cat', $post_id));
$count_rows_registered = array_count_values($a);
endwhile;
endif;
if( have_rows('types_income', 'option') ):
while(have_rows('types_income', 'option') ): the_row();
$type_no = get_sub_field('type_income_no');
$title = get_sub_field('type_income_name');
if( have_rows('cat_income', 'option') ):
while( have_rows('cat_income', 'option') ): the_row();
$cat_income_no = get_sub_field('cat_income_no');
$cat_income_name = get_sub_field('cat_income_name');
if( have_rows('settings_cat') ):
while( have_rows('settings_cat') ): the_row();
$day_field = get_sub_field('day_in_months');
$month_field = get_sub_field('months');
endwhile;
endif;
$fileds[$type_no]['name']=$title;
$fileds[$type_no]['content'][$cat_income_no]['name']= $cat_income_name;
$fileds[$type_no]['content'][$cat_income_no]['settings']=[
'day_field' => $day_field,
'month_field' => $month_field
];
$fileds[$type_no]['content'][$cat_income_no]['count_rows_registered']=$count_rows_registered[$cat_income_no];
$fileds[$type_no]['content'][$cat_income_no]['rows_registered']=[];
endwhile;
endif;
endwhile;
endif;
if( have_rows('debts', $post_id) ):
while(have_rows('debts', $post_id) ): the_row();
/********הכנסת תוכן השורות במערך הכללי********/
$row =array(
'index_row' => get_row_index(),
'date_registered' => get_sub_field('Date_typed'),
'type' => get_sub_field('type'),
'ID_type' => get_sub_field('id_type'),
'category_name' => get_sub_field('category_debts'),
'ID_category' => get_sub_field('id_cat'),
'dete_in_payment' => get_sub_field('total_debt'),
'Amount' => get_sub_field('total'),
'method_payment' => get_sub_field('method')
);
$fileds[$row['ID_type']]['content'][$row['ID_category']]['rows_registered'][]=$row;
endwhile;
endif;
return $fileds;
}
}
Again I note that when I put the class code inside the function directly - it works
Share Improve this question edited Aug 20, 2019 at 11:52 sbc asked Aug 20, 2019 at 8:22 sbcsbc 12 bronze badges 6- Please include the code of what you've attempted in the question. – Jacob Peattie Commented Aug 20, 2019 at 9:03
- Soon I will add – sbc Commented Aug 20, 2019 at 9:44
- I added it in response – sbc Commented Aug 20, 2019 at 11:44
- That's not how this site works. Responses must be answers. Edit your question and add it there. – Jacob Peattie Commented Aug 20, 2019 at 11:47
- Sorry, I'm new, how do you edit? – sbc Commented Aug 20, 2019 at 11:51
2 Answers
Reset to default 0Use include (yourclass.php) only if the file yourclass.php is on the same folder. Otherwise you will need to put the full path to your file or use a variable for the plugin path: public $plugin_path; $this->plugin_path = plugin_dir_path( dirname( dirname( FILE ) ) );
and include the file using plugin_path.
Firstly, you've spelt the filename incorrectly:
include_once(get_stylesheet_directory_uri() . '/fileds.php');
"fileds.php" should be "fields.php", judging by what you called it elsewhere in your question.
Secondly, on many (if not most) severs you can't use a URL with include_once
. You need to get the path to the file. Use get_theme_file_path()
to do this:
include_once get_theme_file_path( 'fields.php' );
本文标签: phpuse with class file into wpajax hook
版权声明:本文标题:php - use with class file into wp_ajax hook 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745231489a2648841.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论