0

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

  • Please include the code of what you've attempted in the question. – Jacob Peattie Aug 20 '19 at 9:03
  • Soon I will add – sbc Aug 20 '19 at 9:44
  • I added it in response – sbc Aug 20 '19 at 11:44
  • That's not how this site works. Responses must be answers. Edit your question and add it there. – Jacob Peattie Aug 20 '19 at 11:47
  • Sorry, I'm new, how do you edit? – sbc Aug 20 '19 at 11:51
0

Use 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.

|improve this answer|||||
  • I wrote like this: include (get_stylesheet_directory_uri (). /fileName.php); The file itself in the root directory of the daughter template – sbc Aug 20 '19 at 8:42
  • make sure to put the quotes: include (get_stylesheet_directory_uri () . '/fileName.php'); – dragos.nicolae Aug 20 '19 at 8:49
  • Yes yes, that's how I originally wrote. – sbc Aug 20 '19 at 8:53
  • make sure you are using get_stylesheet_directory_uri() function for the right purpose - you need to check WP codex and also check this link maybe you will find useful info: stackoverflow.com/questions/38349068/… – dragos.nicolae Aug 20 '19 at 9:09
  • It's okay, I used it correctly. – sbc Aug 20 '19 at 9:38
0

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' ); 
|improve this answer|||||

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.