1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
function import_terms_icons() { // do stuff here $url = "https://example.com/wp-content/functions/arabic-cats.json"; $json = file_get_contents($url); $json_data = json_decode($json, true); //print_r($json_data); $args = array( 'taxonomy' => 'job_listing_category', 'orderby' => 'name', 'show_count' => 1, 'pad_counts' => false, 'hide_empty' => 0, 'hierarchical' => 1, 'exclude' => '', 'include' => '', 'number' => '', 'hierarchical' => 1, 'echo' => true ); // Getting Categories $_categories = get_categories($args); //sample of single term insert /* $cc = $_categories[0]; $file_url = 'https://example.com/wp-content/uploads/2019/11/asian-cars.png'; $wp_upload_dir = wp_upload_dir(); $filetype = wp_check_filetype( basename( $file_url ), null ); $attachment = array( 'guid' => $wp_upload_dir['url'] . '/' . basename( $file_url ), 'post_parent' => $cc->term_taxonomy_id, 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_url ) ), 'post_type' => 'attachment', 'post_content' => '', 'post_status' => 'inherit', 'post_mime_type' => $filetype['type'], ); $attach_id = wp_insert_attachment( $attachment, $file_url ); update_field("field_5bcaebaa7db8b", $attach_id, 'job_listing_category_'.$cc->term_taxonomy_id ); */ $i = 0; $wp_upload_dir = wp_upload_dir(); foreach( $_categories as $category ) { foreach( $json_data as $categoryx ) { if($categoryx["Term Name"] == $category->name ){ if($categoryx["Category Icon"]){ //echo $categoryx["Category Icon"];//Term Name $file_url = $categoryx["Category Icon"]; $filetype = wp_check_filetype( basename( $file_url ), null ); $attachment = array( 'guid' => $wp_upload_dir['url'] . '/' . basename( $file_url ), 'post_parent' => $category->term_taxonomy_id, 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_url ) ), 'post_type' => 'attachment', 'post_content' => '', 'post_status' => 'inherit', 'post_mime_type' => $filetype['type'], ); $attach_id = wp_insert_attachment( $attachment, $file_url ); $field_acf_static_name = 'field_5bcaebaa7db8b'; update_field($field_acf_static_name, $attach_id, 'job_listing_category_'.$category->term_taxonomy_id ); $i++; echo "</br>".$categoryx["Term Name"]." - ".$i."</br>"; } } } //code for printing out acf value /* $image_id = get_field( 'category_icon', 'job_listing_category_'.$category->term_taxonomy_id, false); $image = wp_get_attachment_image_src($image_id, $size); echo '<img src="'.$image[0].'" />'; */ } exit(); } //add_action('wp_head', 'import_terms_icons'); |