1

I'm trying to write a custom bulk importer for store locations but I am struggling with which action hook to use.

I have written it and tested it on a clean WordPress installation and it works fine there but when I try to use it alongside WooCommerce I get an error on every admin page.

I've traced the fault back and I think it is being caused by the filter hook I am trying to use.

add_filter('pre_update_option',[$this,'importLocations'],10,3);

My import method looks like this:

public function importLocations($newvalue,$option,$oldvalue){
    if('stockist_settings'==$option && !empty($newvalue['fileimport'])){

        $file=get_attached_file($newvalue['fileimport'][0]);

        $locations=$this->parseCSVToArray($file);

        if(!empty($locations)){
            $prefix = $this->getAttributes()['slug']."_";
            foreach($locations as $location){

                // Checks that the longitude and latitude are filled out
                if(empty($location['Lat']) || empty($location['Lng'])){
                    $geocoder=new Geocoder();
                    $lnglat = $geocoder->geocodePostcode($location['Postcode']);
                    $location['Lat']=$lnglat['latitude'];
                    $location['Lng']=$lnglat['longitude'];
                }

                $social=$this->parseSocialMedia($location);

                $v = new Valitron\Validator($location);
                $v->rules($this->setImportRules());
                if($v->validate()) {

                    $payload=[
                        'post_title'    =>  $location['CompanyName'],
                        'post_type'     =>  $this->getAttributes()['slug'],
                        'post_status'   =>  'publish',
                        'meta_input'    =>  [
                            $prefix.'address1'      =>  $location['Address1'],
                            $prefix.'address2'      =>  $location['Address2'],
                            $prefix.'address3'      =>  $location['Address3'],
                            $prefix.'city'          =>  $location['City'],
                            $prefix.'postcode'      =>  $location['Postcode'],
                            $prefix.'phonenumber'   =>  $location['PhoneNumber'],
                            $prefix.'website'       =>  $location['Website'],
                            $prefix.'email'         =>  $location['Email'],
                            $prefix.'longitude'     =>  $location['Lng'],
                            $prefix.'latitude'      =>  $location['Lat'],
                            $prefix.'socialrepeat_group'=>$social
                        ]
                    ];

                    $id=wp_insert_post($payload);

                } else {
                    // Errors
                    print_r($v->errors());
                }
            }

            wp_redirect(admin_url('/edit.php?post_type=stockists'),301);
        }
    }
}

Is there a better filter hook this I should use or am I approaching the bulk import from the wrong direction entirely?

  • 1
    I don't think it's a good idea to redirect via that hook (which is a filter hook). Secondly, your function would be applied when updating any options, so you should always return $newvalue; at the end of your function. – Sally CJ Oct 16 at 1:54
  • @SallyCJ That is the crux of my question, is there a better filter or action to use? – Burgi Oct 16 at 8:20
  • If you need to run the import process every time the option stockist_settings is being updated (either via a form submission or programatically), then you can use the pre_update_option hook, but don't redirect and be sure to return the $newvalue. Or if you're not actually modifying the $newvalue and just reading the file path, then it's better to use an action hook like update_option or updated_option. But even so, redirection should still be avoided. – Sally CJ Oct 16 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.