WP_Import::process_terms PHP Method

process_terms() public method

Doesn't create a term its slug already exists
public process_terms ( )
        function process_terms()
        {
            $this->terms = apply_filters('wp_import_terms', $this->terms);
            if (empty($this->terms)) {
                return;
            }
            foreach ($this->terms as $term) {
                // if the term already exists in the correct taxonomy leave it alone
                $term_id = term_exists($term['slug'], $term['term_taxonomy']);
                if ($term_id) {
                    if (is_array($term_id)) {
                        $term_id = $term_id['term_id'];
                    }
                    if (isset($term['term_id'])) {
                        $this->processed_terms[intval($term['term_id'])] = (int) $term_id;
                    }
                    continue;
                }
                if (empty($term['term_parent'])) {
                    $parent = 0;
                } else {
                    $parent = term_exists($term['term_parent'], $term['term_taxonomy']);
                    if (is_array($parent)) {
                        $parent = $parent['term_id'];
                    }
                }
                $description = isset($term['term_description']) ? $term['term_description'] : '';
                $termarr = array('slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent));
                $id = wp_insert_term($term['term_name'], $term['term_taxonomy'], $termarr);
                if (!is_wp_error($id)) {
                    if (isset($term['term_id'])) {
                        $this->processed_terms[intval($term['term_id'])] = $id['term_id'];
                    }
                } else {
                    printf(__('Failed to import %s %s', 'wordpress-importer'), esc_html($term['term_taxonomy']), esc_html($term['term_name']));
                    if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) {
                        echo ': ' . $id->get_error_message();
                    }
                    echo '<br />';
                    continue;
                }
            }
            unset($this->terms);
        }

Usage Example

Example #1
0
 function process_terms()
 {
     $term_translations = array();
     // store this for future usage as parent function unsets $this->terms
     foreach ($this->terms as $term) {
         if ('post_translations' == $term['term_taxonomy']) {
             $this->post_translations[] = $term;
         }
         if ('term_translations' == $term['term_taxonomy']) {
             $term_translations[] = $term;
         }
     }
     parent::process_terms();
     global $polylang;
     $polylang->model->clean_languages_cache();
     // to update the languages list if needed
     if (($options = get_option('polylang')) && empty($options['default_lang']) && ($languages = $polylang->model->get_languages_list())) {
         // assign the default language if importer created the first language
         $default_lang = reset($languages);
         $options['default_lang'] = $default_lang->slug;
         update_option('polylang', $options);
     }
     $this->remap_terms_relations($term_translations);
     $this->remap_translations($term_translations, $this->processed_terms);
 }
All Usage Examples Of WP_Import::process_terms