Napisałem kawałek kodu, który zajmuje się automatycznym generowaniem treści do wordpressa. Klasa umożliwia tworzenie kategorii, podkategorii, wpisów oraz tagów do konkretnych wpisów. Przydaje się, gdy chcemy zmigrować dane z naszej bazy na strukturę wordpressa.
<?php // ścieżka do pliku konfiguracyjnego wordpressa define('WP_CONFIG_PATH', __DIR__.'/../wp-config.php'); class WordpressAutomat { // return new category id function insertCategory($name, $description, $nicename = NULL, $parentID = 0) { return wp_insert_category(array( 'cat_name' => $name, 'category_description' => $description, 'category_nicename' => $nicename, 'category_parent' => $parentID )); } // return new post id function insertPost($title, $content, $excerpt = '', $parentCategoryId = array(0), $authorId = 1) { return wp_insert_post(array( 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt, 'post_status' => 'publish', 'post_author' => $authorId, 'post_category' => $parentCategoryId )); } // return array of new tags function createTags($tags = array(), $postId) { return wp_set_object_terms($postId, $tags, 'post_tag', true); } } require WP_CONFIG_PATH; require ABSPATH . "wp-admin/includes/taxonomy.php";
A teraz jak się jej używa:
<?php $automat = new WordpressAutomat(); // dodaj kategorię $cat_id = $automat->insertCategory('title','description','friendly-url'); // dodaj podkategorię $cat2_id = $automat->insertCategory('title 2','description 2','friendly-url-2',$cat_id); // dodaj wpis $post_id = $automat->insertPost('title', 'content', 'excerpt', array($cat2_id)); // kategorii może być kilka $automat->createTags(array('tag 1','tag 2','tag 3'),$post_id);