global

$

wpdb

;

$

array_data

=

csv_to_array

(

‘csv_key.csv’

)

;

 

foreach

(

$

array_data

as

$

data

)

{

    

        

$

data

=

trim

(

$

data

)

;

    

if

(

$

data

!

=

)

    

{

            

// Replace with your desired taxonomy.

              

$

term_exists

=

term_exists

(

$

data

,

<

taxonomy

>

)

;

              

if

(

$

term_exists

!

==

0

&& $term_exists !== null) {

 

                   echo $term_exists->name.” exists!<br/>”;

              

}

else

{

 

                  

$

term

=

wp_insert_term

(

$

data

,

‘<taxonomy>’

)

;

                  

echo

$

term

>

name

.

” inserted Successfully<br/>”

;

          

}

    

}

}

 

// function to convert csv data into array

function

csv_to_array

(

$

filename

=

,

$

delimiter

=

‘,’

)

{

    

if

(

!

file_exists

(

$

filename

)

|

|

!

is_readable

(

$

filename

)

)

        

return

FALSE

;

 

    

$

header

=

NULL

;

    

$

data

=

array

(

)

;

    

if

(

(

$

handle

=

fopen

(

$

filename

,

‘r’

)

)

!

==

FALSE

)

    

{

        

while

(

(

$

row

=

fgetcsv

(

$

handle

,

1000

,

$

delimiter

)

)

!

==

FALSE

)

        

{

            

array_push

(

$

data

,

$

row

)

;

        

}

        

fclose

(

$

handle

)

;

    

}

    

    

return

flatter_array

(

$

data

)

;

}

// To convert multi dimensional array to single dimension

function

flatter_array

(

$

array

)

{

  

if

(

!

is_array

(

$

array

)

)

{

    

return

FALSE

;

  

}

  

$

result

=

array

(

)

;

  

foreach

(

$

array

as

$

key

=

>

$

value

)

{

    

if

(

is_array

(

$

value

)

)

{

      

$

result

=

array_merge

(

$

result

,

array_flatten

(

$

value

)

)

;

    

}

    

else

{

      

$

result

[

$

key

]

=

$

value

;

    

}

  

}

  

return

$

result

;

}

 

Leave a Reply

Your email address will not be published.