• Resolved jabbadu

    (@jabbadu)


    Hi!

    i have a question about the button field.
    How to handle multiple ajax button requests?

    I can handle one button easily with the provided function (my_acf_button_ajax()-example).

    But if i add more buttons to a fieldgroup the same function is executed. Is there a way to specify the function to execute?
    Or is the only way to wrap it in some if-else statements?

    I hope you understand what I mean ??

    Thanks a lot.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello!

    Yes you are right, you have to wrap your ouput inside an if() statement. Here is an example to use the same function for 2 differents buttons:

    
    add_action('wp_ajax_acfe/fields/button', 'my_acf_button_ajax');
    add_action('wp_ajax_nopriv_acfe/fields/button', 'my_acf_button_ajax');
    function my_acf_button_ajax(){
        
        /**
         * @bool/string $_POST['post_id'] Current post ID
         * @string      $_POST['field_key'] Button's field key
         * @string      $_POST['field_name'] Button's field name
         */
        
        if($_POST['field_name'] === 'my_field'){
        
            echo 'This is the Ajax for my_field!';
            die;
            
        }
        
        if($_POST['field_name'] === 'my_second_field'){
        
            echo 'This is the Ajax for my_second_field!';
            die;
            
        }
        
    }
    

    But you can also split it with 2 different functions:

    
    add_action('wp_ajax_acfe/fields/button', 'my_acf_button_ajax');
    add_action('wp_ajax_nopriv_acfe/fields/button', 'my_acf_button_ajax');
    function my_acf_button_ajax(){
        
        if($_POST['field_name'] === 'my_field'){
        
            echo 'This is the Ajax for my_field!';
            die;
            
        }
        
    }
    
    add_action('wp_ajax_acfe/fields/button', 'my_acf_second_button_ajax');
    add_action('wp_ajax_nopriv_acfe/fields/button', 'my_acf_second_button_ajax');
    function my_acf_second_button_ajax(){
        
        if($_POST['field_name'] === 'my_second_field'){
        
            echo 'This is the Ajax for my_second_field!';
            die;
            
        }
        
    }
    

    But you are right, it would be cool to just use one nomative hook and target a specific field, like: add_action('acfe/fields/button/name=my_field', 'my_function').

    I add it as feature request in the Trello board ?? https://trello.com/b/QEgpU7CL/acf-extended

    Regards.

    Thread Starter jabbadu

    (@jabbadu)

    you are breathtaking! ??

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Just to let you know that the latest 0.8.4.5 Update added this feature. You can now directly target a specific button ajax response, using the following code:

    
    add_action('acfe/fields/button/name=my_button', 'my_acf_button_ajax', 10, 2);
    function my_acf_button_ajax($field, $post_id){
        
        /**
         * @array       $field      Field array
         * @bool/string $post_id    Current post ID
         */
        
        echo 'Hello World';
        
    }
    

    The old hook wp_ajax_acfe/fields/button still works too!

    Have a nice day!

    Regards.

    Hello, thanks for this great feature! Is it possible to get somehow number of row if button is part of row in repeater? Goal is to check checkbox which is also in every row, but I can’t target the right row. So, every button should check it’s own checkbox in its row. There will be more functions connected to this and I need that right checkbox checked. Thank you in advance!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘(Ajax) Button-Field’ is closed to new replies.