0

I need to pass arrays to a function from multiple functions. I am using apply_filters_ref_array() for that. The problem is it only accepts the array passed from highest priority callbak. Here is the scenario:

//First Callbak
function first_callback() {

    $html['abc'] = 'xyz';

    return $html;

}
add_filter( 'process_args', 'first_callback' );


//Second Callbak
function second_callback() {

    $html['def'] = 'uvw';

    return $html;

}
add_filter( 'process_args', 'second_callback' );


//Third Callbak
function third_callback() {

    $html['ghi'] = 'rst';

    return $html;

}
add_filter( 'process_args', 'third_callback' );


//Callbak that processes $args
function process_args( $args ) {

    $args = apply_filters_ref_array( 'process_args', $args );

    print_r( $args );
}

Now, the print_r( $args ); prints the array passed from the last function.

Array ( 
    [ghi] => rst
)

I need to print it as:

Array (
    [abc] => xyz
    [def] => uvw
    [ghi] => rst
)

How do I do that?

Thanks

EDIT:
Current code I am working with

function pwpus_input_field_text( $field, $inputs, $html=array() ) {

    $html['text'] = '<input id="'.$field['id'].'" type="text" class="pwpus-text-input pwpus-input-size-'.$field['size'].'" name="'.$field['name'].'" value="'.$field['default'].'">';

    return $html;

}
add_filter( 'pwpus_input_fields', 'pwpus_input_field_text' );

function pwpus_input_field_tel( $field, $inputs, $html=array() ) {

    $html['tel'] = '<input id="'.$field['id'].'" type="tel" class="pwpus-text-input pwpus-input-size-'.$field['size'].'" name="'.$field['name'].'" value="'.$field['default'].'">';

    return $html;

}
add_filter( 'pwpus_input_fields', 'pwpus_input_field_tel' );

function pwpus_input_field_email( $field, $inputs, $html=array() ) {

    $html['email'] = '<input id="'.$field['id'].'" type="email" class="pwpus-text-input pwpus-input-size-'.$field['size'].'" name="'.$field['name'].'" value="'.$field['default'].'">';

    return $html;

}
add_filter( 'pwpus_input_fields', 'pwpus_input_field_email' );


function pwpus_shortcode_form( $fields, $inputs=array() ) {

    $inputs = apply_filters_ref_array( 'pwpus_input_fields', $inputs );
    //$inputs = do_action( 'pwpus_input_fields' );


    if ( array_key_exists( 'class', $fields ) && $fields['class'] != '' ) {
        $class = ' ' . $fields['class'];
    } else {
        $class = '';
    }

    if ( array_key_exists( 'callback', $fields ) && $fields['callback'] != '' ) {
        $callback = $fields['callback'];
    } else {
        $callback = 'pwusp_nocallback';
    }

    $html = '<form id="pwpus-shortcode-form" class="pwpus-shortcode-form' . $class . '" action="pwpus_parse_scform">';
        $html .= '<table class="pwpus-shortcode-form-table">';

        if ( array_key_exists( 'fields', $fields ) && is_array($fields['fields']) ) {

            foreach ( $fields['fields'] as $field ) {

                //$html .= pwpus_fields( $field );

                $html .= '<tr id="field-'. $field['id'] .'"><td><label for="'. $field['name'] .'">'. $field['label'] .'</label></td><td>:</td><td>' . $inputs[ $field['type'] ]. '<span class="pwpus-field-desc">'. $field['desc'] .'</span></td></tr>';

            }

        }

        $html .= '<input type="hidden" name="callback" value="'. $callback .'">';
        $html .= wp_nonce_field( 'pwpus-shortcode-nonce', 'pwpus-shortcode-nonce', false );
        $html .= '</table>';
        $html .= '<div class="pwpus-form-buttons"><button type="submit" id="pwpus-scform-submit" class="pwpus-scform-submit">' . __( 'Generate Shortcode', 'purewp' ) . '</button><button type="reset" id="pwpus-scform-reset" class="pwpus-scform-reset">' . __( 'Reset', 'purewp' ) . '</button></div>';
    $html .= '</form>';

    //return $html;

    print_r( $inputs );

}
0

You can return $html['abc'] and add $html as parameter in each function and to the filter. Exemple for the function:

function second_callback($html){
     $html['def']= 'uvw';

     return $html['def'];
}
0

You forgot to pass on the input argument for your filter callbacks, like:

function first_callback( $html = [] ) {

    $html['abc'] = 'xyz';

    return $html;

}
add_filter( 'process_args', 'first_callback' );

where we replace => for =.

Otherwise you're just replacing the filter value each time!

Update:

I see in your updated code you have e.g.

function pwpus_input_field_email( $field, $inputs, $html=array() ) {

    $html['email'] = '<input 
        id="'.$field['id'].'" 
        type="email" 
        class="pwpus-text-input pwpus-input-size-'.$field['size'].'" 
        name="'.$field['name'].'" 
        value="'.$field['default'].'">';

    return $html;

}
add_filter( 'pwpus_input_fields', 'pwpus_input_field_email' );

There are few problems here, that come to mind:

  • There might be other ways to implement this in general?
  • We must return the first input argument in the filter's callback.
  • I think the $html variable is not needed.
  • I think you could return $inputs instead of $html and use it instead?
  • There might be a confusion with variable names, regarding how you use $inputs with apply_filters_ref_array and in your filter callbacks.
  • I think we should just use apply_filters instead of apply_filters_ref_array to avoid this confusion.
  • If we use apply_filters, then we must register the filter callbacks with the correct priority and number of input arguments.
  • Remember to use esc_attr() when you construct the attributes for the input tags.
  • I guess $field should be called $fields where you check for e.g. non-empty $fields['email']['id'] before using it, etc

Hope it helps!

  • Thanks for stopping by. Still same here, the filter taking only the last item. – Abhik Oct 15 '16 at 13:43
  • Can you post the current code you're testing? @Abhik – birgire Oct 15 '16 at 13:46
  • Added to the original post. Please check. Sorry, the code is a bit messy. And yes, I am first time working on my own filters, so please pardon if I am doing anything silly. – Abhik Oct 15 '16 at 13:48
  • I see few problems there, but I wonder if this is the best approach? But you need to return the first input argument of your filter callbacks and you must register the filters with the correct number of input arguments. Note that we don't need to use apply_filters_ref_array to pass on a single array, we can just use apply_filters for that. @Abhik – birgire Oct 15 '16 at 15:16

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.