0

Hi I am new to hooks and have a query to add additional data to the existing array. Please check the below code.

I have some basic details and want to add the Phone number using add_filter(). If I give directly,it is working. But I need to do this using filter. Please help...

function give_import_donations_options() 
{
return (array) apply_filters(
        'give_import_donations_options',
        [
            'id'           => __( 'Donation ID', 'give' ),          
            'first_name'   => [
                __( 'Donor First Name', 'give' ),
                __( 'First Name', 'give' ),
                __( 'Name', 'give' ),
                __( 'First', 'give' ),
            ],
            'last_name'    => [
                __( 'Donor Last Name', 'give' ),
                __( 'Last Name', 'give' ),
                __( 'Last', 'give' ),
            ],
            'company_name' => [
                __( 'Company Name', 'give' ),
                __( 'Donor Company Name', 'give' ),
                __( 'Donor Company', 'give' ),
                __( 'Company', 'give' ),
            ],
           'country'      => __( 'Country', 'give' ),
           'notes'        => __( 'Notes', 'give' ),
            /*'give_phone'        => __( 'Phone', 'give' ),*/
        ]
    );
}' 

I am trying to add phone number using filter:

function additional_field
{
need to add code to add Phone Number...
}

add_filter('give_import_donations_options','additional_field...')

1 Answer 1

0

The code you provided looks correct, and it should work as expected. However, if you want to add the 'give_phone' option using a filter, you would need to modify the code to include the filter hook and the callback function. Here's the modified code:

function additional_field($options) {
    $options['give_phone'] = __( 'Phone', 'give' );
    return $options;
}
add_filter('give_import_donations_options', 'additional_field');

function give_import_donations_options() {
    return (array) apply_filters(
        'give_import_donations_options',
        [
            'id'           => __( 'Donation ID', 'give' ),          
            'first_name'   => [
                __( 'Donor First Name', 'give' ),
                __( 'First Name', 'give' ),
                __( 'Name', 'give' ),
                __( 'First', 'give' ),
            ],
            'last_name'    => [
                __( 'Donor Last Name', 'give' ),
                __( 'Last Name', 'give' ),
                __( 'Last', 'give' ),
            ],
            'company_name' => [
                __( 'Company Name', 'give' ),
                __( 'Donor Company Name', 'give' ),
                __( 'Donor Company', 'give' ),
                __( 'Company', 'give' ),
            ],
            'country'      => __( 'Country', 'give' ),
            'notes'        => __( 'Notes', 'give' ),
        ]
    );
}

$modified_options = give_import_donations_options();
print_r($modified_options);

Note: Place this code in your theme's functions.php file or in a custom plugin file and inform me if you have any issues.

3
  • Hi, Thank you for the immediate help. After adding this code, the existing and new option are not showing. I was searching in Google and added some code using array push. Showing the options. But don't know whether am writing syntax correctly or not. function importdropdownoptionadditional($args) { array_push($args, "give_phone", "Phone", "give"); return $args; } add_filter( 'give_import_donations_options','importdropdownoptionadditional',10,3);
    – Manikandan
    Commented Dec 21, 2023 at 7:02
  • The code you provided looks correct, and it should work as expected. However, if you want to add the 'give_phone' option using a filter, you would need to modify the code to include the filter hook and the callback function. I have updated my code. So you can check. Commented Dec 21, 2023 at 7:26
  • Thank you very much. Its working. I was struggling to learn and understand about this hooks for the past 2 days. Now understood little more.
    – Manikandan
    Commented Dec 21, 2023 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.