本文整理汇总了PHP中wp_insert_category函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_insert_category函数的具体用法?PHP wp_insert_category怎么用?PHP wp_insert_category使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_insert_category函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: plugin_activate_example_activate
function plugin_activate_example_activate()
{
// Activation code here...
// let's create some categories upon activation
//http://codex.wordpress.org/Function_Reference/wp_insert_category
for ($i = 0; $i < 5; $i++) {
$catarr = array('cat_name' => 'My Category' . $i, 'category_description' => 'A Cool Category' . $i, 'category_nicename' => 'category-slug' . $i, 'category_parent' => '');
wp_insert_category($catarr);
}
// should be root path of the wp install
$wordpress_path = get_home_path();
require_once $wordpress_path . '/wp-load.php';
//not sure if this line is needed
//activate_plugin() is here:
require_once $wordpress_path . '/wp-admin/includes/plugin.php';
// we're going to activate our plugins that are dependencies
$plugins = array("filters-example", "js-example", "shortcode-example");
// see
//http://wordpress.stackexchange.com/questions/62967/why-activate-plugin-is-not-working-in-register-activation-hook
foreach ($plugins as $plugin) {
$plugin_path = $wordpress_path . 'wp-content/plugins/' . $plugin . '/' . $plugin . '.php';
if (file_exists($plugin_path) && is_plugin_inactive($plugin . '/' . $plugin . '.php')) {
// just double check that the plugin exists and unactivated
add_action('update_option_active_plugins', 'plugin_activation_dependencies');
}
}
}
示例2: frs_add_slidetype
function frs_add_slidetype()
{
$cat_name = htmlspecialchars($_POST['name']);
$catarr = array('cat_name' => $cat_name, 'taxonomy' => 'slide_type');
$new_cat_id = wp_insert_category($catarr);
$new_cat = get_term_by('id', $new_cat_id, 'slide_type');
$return = array('success' => true, 'slug' => $new_cat->slug);
echo json_encode($return);
die;
}
示例3: startedev_get_categoria
function startedev_get_categoria($categoria)
{
$categoria_woocomerce = array('cat_name' => $categoria, 'taxonomy' => 'product_cat');
$cat_woocommerce = wp_insert_category($categoria_woocomerce, true);
if (is_wp_error($cat_woocommerce)) {
return $cat_woocommerce->get_error_data('term_exists');
} else {
return $cat_woocommerce;
}
}
开发者ID:admigmarcelo,项目名称:startedev-plugin-distribuidora,代码行数:10,代码来源:startedev-plugin-distribuidora.php
示例4: setUp
function setUp()
{
parent::setUp();
$author = wp_insert_user(array("user_login" => "testuser", "user_pass" => "testing", "display_name" => "Tester"));
$category_id = wp_insert_category(array('cat_name' => 'Testing'));
wp_insert_post(array("post_name" => "test-post", "post_title" => "Test Post", "post_content" => "This is a test <strong>post</strong>.", "post_status" => "publish", "post_author" => $author, "post_category" => array($category_id), "tags_input" => array("tag1", "tag2"), "post_date" => "2014-01-01"));
wp_insert_post(array("post_name" => "test-page", "post_title" => "Test Page", "post_content" => "This is a test <strong>page</strong>.", "post_status" => "publish", "post_type" => "page", "post_author" => $author));
global $jekyll_export;
$jekyll_export->init_temp_dir();
}
示例5: part_categories
function part_categories($categories, &$existing_cat, $start_pos)
{
$start_time = time();
for ($i = $start_pos; $i < count($categories); $i++) {
if (!term_exists($categories[$i]->ID, 'product_cat')) {
//если категория не добавлена в базу
if ($categories[$i]->ID == 'Order') {
$parent_id = 0;
}
$parent_id = cat_exists($categories[$i]->ID_PARENT, $existing_cat, 'product_cat');
wp_insert_category(array('cat_name' => $categories[$i]->Description, 'category_nicename' => $categories[$i]->ID, 'category_parent' => $parent_id, 'taxonomy' => 'product_cat'));
}
if (time() - $start_time > 20) {
restart_load_categories($categories, $existing_cat, ++$i);
break;
}
}
}
示例6: csv_import_options_page
function csv_import_options_page()
{
if (empty($_FILES)) {
?>
<div>
<h2>Upload a csv file here to import categories</h2>
<form action="" method="post" enctype="multipart/form-data">
<?php
wp_nonce_field('csv-import');
?>
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="save" value="save">
</form>
</div>
<?php
} else {
if (!function_exists('wp_handle_upload')) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$uploadedfile = $_FILES['file'];
$upload_overrides = array('test_form' => false);
$movefile = wp_handle_upload($uploadedfile, $upload_overrides);
if ($movefile) {
echo "File is valid, and was successfully uploaded.\n";
$csv = array_map('str_getcsv', file($movefile['file']));
// the file should be a csv of categories.
$cnt = 0;
foreach ($csv as $row) {
$my_cat = array('cat_name' => $row[0], 'category_description' => $row[1], 'category_nicename' => $row[2], 'category_parent' => '');
// Create the category
$my_cat_id = wp_insert_category($my_cat);
if ($my_cat_id > 0) {
$cnt++;
}
}
echo "{$cnt} categories added";
// here you can do some stuff with this
} else {
echo "Possible file upload attack!\n";
}
}
}
示例7: pp_insert_project
function pp_insert_project($project_data, $wp_error = false)
{
if (!($project_category_id = pp_get_category_id('projects'))) {
return false;
// This shouldn't happen from the edit screen.
}
if (isset($project_data['project_parent']) && (!$project_data['project_parent'] || -1 == $project_data['project_parent'])) {
unset($project_data['project_parent']);
}
$project_defaults = array('project_ID' => 0, 'project_name' => '', 'project_description' => '', 'project_nicename' => '', 'project_parent' => $project_category_id, 'project_website' => '', 'project_blog' => '', 'project_svn' => '', 'project_trac' => '', 'project_intertrac' => '', 'project_activity' => '', 'project_overheard' => '');
$project_data = wp_parse_args($project_data, $project_defaults);
$category_data = array('cat_ID' => $project_data['project_ID'], 'cat_name' => $project_data['project_name'], 'category_description' => $project_data['project_description'], 'category_nicename' => $project_data['project_nicename'], 'category_parent' => $project_data['project_parent']);
$category_id = wp_insert_category($category_data, $wp_error);
if (!$wp_error && !$category_id) {
return false;
}
if ($wp_error && is_wp_error($category_id)) {
return $cat_ID;
}
$project_meta = array();
$project_meta['logo'] = $project_data['project_logo'];
$project_meta['website'] = $project_data['project_website'];
$project_meta['blog'] = $project_data['project_blog'];
$project_meta['svn'] = $project_data['project_svn'];
$project_meta['trac'] = $project_data['project_trac'];
$project_meta['intertrac'] = $project_data['project_intertrac'];
$project_meta['activity'] = $project_data['project_activity'];
$project_meta['overheard'] = $project_data['project_overheard'];
if ($project_meta['activity']) {
$project_meta['activity'] = str_replace("\r", '', $project_meta['activity']);
$project_meta['activity'] = explode("\n", $project_meta['activity']);
array_walk($project_meta['activity'], create_function('&$a', '$a = trim($a);'));
$project_meta['activity'] = array_filter($project_meta['activity']);
}
if ($project_meta['overheard']) {
$project_meta['overheard'] = str_replace("\r", '', $project_meta['overheard']);
$project_meta['overheard'] = explode("\n", $project_meta['overheard']);
array_walk($project_meta['overheard'], create_function('&$a', '$a = trim($a);'));
$project_meta['overheard'] = array_filter($project_meta['overheard']);
}
update_option('pp_project_meta_' . $category_id, $project_meta);
return true;
}
示例8: wp_newCategory
/**
* WordPress XML-RPC API
* wp_newCategory
*/
function wp_newCategory($args)
{
$this->escape($args);
$blog_id = (int) $args[0];
$username = $args[1];
$password = $args[2];
$category = $args[3];
if (!$this->login_pass_ok($username, $password)) {
return $this->error;
}
// Set the user context and make sure they are
// allowed to add a category.
set_current_user(0, $username);
if (!current_user_can("manage_categories")) {
return new IXR_Error(401, __("Sorry, you do not have the right to add a category."));
}
// If no slug was provided make it empty so that
// WordPress will generate one.
if (empty($category["slug"])) {
$category["slug"] = "";
}
// If no parent_id was provided make it empty
// so that it will be a top level page (no parent).
if (!isset($category["parent_id"])) {
$category["parent_id"] = "";
}
// If no description was provided make it empty.
if (empty($category["description"])) {
$category["description"] = "";
}
$new_category = array("cat_name" => $category["name"], "category_nicename" => $category["slug"], "category_parent" => $category["parent_id"], "category_description" => $category["description"]);
$cat_id = wp_insert_category($new_category);
if (!$cat_id) {
return new IXR_Error(500, __("Sorry, the new category failed."));
}
return $cat_id;
}
示例9: wp_newCategory
/**
* Create new category.
*
* @since 2.2.0
*
* @param array $args Method parameters.
* @return int|IXR_Error Category ID.
*/
public function wp_newCategory($args)
{
$this->escape($args);
$username = $args[1];
$password = $args[2];
$category = $args[3];
if (!($user = $this->login($username, $password))) {
return $this->error;
}
/** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
do_action('xmlrpc_call', 'wp.newCategory');
// Make sure the user is allowed to add a category.
if (!current_user_can('manage_categories')) {
return new IXR_Error(401, __('Sorry, you do not have the right to add a category.'));
}
// If no slug was provided make it empty so that
// WordPress will generate one.
if (empty($category['slug'])) {
$category['slug'] = '';
}
// If no parent_id was provided make it empty
// so that it will be a top level page (no parent).
if (!isset($category['parent_id'])) {
$category['parent_id'] = '';
}
// If no description was provided make it empty.
if (empty($category["description"])) {
$category["description"] = "";
}
$new_category = array('cat_name' => $category['name'], 'category_nicename' => $category['slug'], 'category_parent' => $category['parent_id'], 'category_description' => $category['description']);
$cat_id = wp_insert_category($new_category, true);
if (is_wp_error($cat_id)) {
if ('term_exists' == $cat_id->get_error_code()) {
return (int) $cat_id->get_error_data();
} else {
return new IXR_Error(500, __('Sorry, the new category failed.'));
}
} elseif (!$cat_id) {
return new IXR_Error(500, __('Sorry, the new category failed.'));
}
/**
* Fires after a new category has been successfully created via XML-RPC.
*
* @since 3.4.0
*
* @param int $cat_id ID of the new category.
* @param array $args An array of new category arguments.
*/
do_action('xmlrpc_call_success_wp_newCategory', $cat_id, $args);
return $cat_id;
}
示例10: process_post
//.........这里部分代码省略.........
$comment_post_ID = $post_id = $this->process_attachment($postdata, $remote_url);
if (!$post_id or is_wp_error($post_id)) {
return $post_id;
}
} else {
printf(__('Importing post <em>%s</em>...', 'wordpress-importer') . "\n", stripslashes($post_title));
$comment_post_ID = $post_id = wp_insert_post($postdata);
if ($post_id && $is_sticky == 1) {
stick_post($post_id);
}
}
if (is_wp_error($post_id)) {
return $post_id;
}
// Memorize old and new ID.
if ($post_id && $post_ID) {
$this->post_ids_processed[intval($post_ID)] = intval($post_id);
}
// Add categories.
if (count($categories) > 0) {
$post_cats = array();
foreach ($categories as $category) {
if ('' == $category) {
continue;
}
$slug = sanitize_term_field('slug', $category, 0, 'category', 'db');
$cat = get_term_by('slug', $slug, 'category');
$cat_ID = 0;
if (!empty($cat)) {
$cat_ID = $cat->term_id;
}
if ($cat_ID == 0) {
$category = $wpdb->escape($category);
$cat_ID = wp_insert_category(array('cat_name' => $category));
if (is_wp_error($cat_ID)) {
continue;
}
}
$post_cats[] = $cat_ID;
}
wp_set_post_categories($post_id, $post_cats);
}
// Add tags.
if (count($tags) > 0) {
$post_tags = array();
foreach ($tags as $tag) {
if ('' == $tag) {
continue;
}
$slug = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
$tag_obj = get_term_by('slug', $slug, 'post_tag');
$tag_id = 0;
if (!empty($tag_obj)) {
$tag_id = $tag_obj->term_id;
}
if ($tag_id == 0) {
$tag = $wpdb->escape($tag);
$tag_id = wp_insert_term($tag, 'post_tag');
if (is_wp_error($tag_id)) {
continue;
}
$tag_id = $tag_id['term_id'];
}
$post_tags[] = intval($tag_id);
}
wp_set_post_tags($post_id, $post_tags);
示例11: wp_update_category
/**
* Aliases wp_insert_category() with minimal args.
*
* If you want to update only some fields of an existing category, call this
* function with only the new values set inside $catarr.
*
* @since 2.0.0
*
* @param array $catarr The 'cat_ID' value is required. All other keys are optional.
* @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure.
*/
function wp_update_category($catarr)
{
$cat_ID = (int) $catarr['cat_ID'];
if (isset($catarr['category_parent']) && $cat_ID == $catarr['category_parent']) {
return false;
}
// First, get all of the original fields
$category = get_term($cat_ID, 'category', ARRAY_A);
_make_cat_compat($category);
// Escape data pulled from DB.
$category = wp_slash($category);
// Merge old and new fields with new fields overwriting old ones.
$catarr = array_merge($category, $catarr);
return wp_insert_category($catarr);
}
示例12: import
function import($file)
{
// Parse file
$dalil_items = self::parse($file);
foreach ($dalil_items as $dalil_item) {
$term = term_exists((string) $dalil_item['categorie'], 'w_dalil_category');
if (!$term) {
$catarr = array('cat_name' => (string) $dalil_item['categorie'], 'taxonomy' => 'w_dalil_category');
wp_insert_category($catarr, $wp_error);
if ($wp_error) {
exit;
}
}
$term = term_exists((string) $dalil_item['city'], 'w_dalil_city');
if (!$term) {
$catarr = array('cat_name' => (string) $dalil_item['city'], 'taxonomy' => 'w_dalil_city');
wp_insert_category($catarr, $wp_error);
if ($wp_error) {
exit;
}
}
}
// Initialises a variable storing the number of logs successfully imported.
$imported = 0;
set_time_limit(600);
foreach ($dalil_items as $dalil_item) {
// global $wpdb;
// $query = $wpdb->get_results('SELECT ID FROM ' . $wpdb->posts . ' WHERE post_title = "'. $dalil_item['title'].'"');
// if ( $query ) {
// wp_publish_post( $query[0]->ID );
// continue;
// }
$my_post = array('post_title' => $dalil_item['title'], 'post_type' => 'w_dalil_posttype', 'post_status' => 'publish');
$post_id = wp_insert_post($my_post, $wp_error);
if (null !== (string) $dalil_item['categorie']) {
$item_set_taxonomy = wp_set_object_terms($post_id, (string) $dalil_item['categorie'], 'w_dalil_category');
}
if (null !== (string) $dalil_item['city']) {
$item_set_taxonomy = wp_set_object_terms($post_id, (string) $dalil_item['city'], 'w_dalil_city');
}
if (!$wp_error) {
if (isset($dalil_item['address'])) {
$dalil_data['dalil-address'] = (string) $dalil_item['address'];
}
if (isset($dalil_item['phone'])) {
$dalil_data['dalil-phone'] = (string) $dalil_item['phone'];
}
if (isset($dalil_item['email'])) {
$dalil_data['dalil-email'] = (string) $dalil_item['email'];
}
if (isset($dalil_item['website'])) {
$dalil_data['dalil-website'] = (string) $dalil_item['website'];
}
update_post_meta($post_id, 'dalil_information', $dalil_data);
$imported++;
}
}
return $imported;
}
示例13: new_post
function new_post()
{
global $user_ID;
if (empty($_POST['action']) || $_POST['action'] != 'new_post') {
die('-1');
}
if (!is_user_logged_in()) {
die('<p>' . __('Error: not logged in.', 'p2') . '</p>');
}
if (!(current_user_can('publish_posts') || get_option('p2_allow_users_publish') && $user_ID)) {
die('<p>' . __('Error: not allowed to post.', 'p2') . '</p>');
}
check_ajax_referer('ajaxnonce', '_ajax_post');
$user = wp_get_current_user();
$user_id = $user->ID;
$post_content = $_POST['posttext'];
$tags = trim($_POST['tags']);
$title = $_POST['post_title'];
$post_type = isset($_POST['post_type']) ? $_POST['post_type'] : 'post';
// Strip placeholder text for tags
if (__('Tag it', 'p2') == $tags) {
$tags = '';
}
if (empty($title) || __('Post Title', 'p2') == $title) {
// For empty or placeholder text, create a nice title based on content
$post_title = p2_title_from_content($post_content);
} else {
$post_title = $title;
}
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
require_once ABSPATH . WPINC . '/category.php';
$accepted_post_cats = apply_filters('p2_accepted_post_cats', array('post', 'quote', 'status', 'link'));
$post_cat = in_array($_POST['post_cat'], $accepted_post_cats) ? $_POST['post_cat'] : 'status';
if (!category_exists($post_cat)) {
wp_insert_category(array('cat_name' => $post_cat));
}
$post_cat = get_category_by_slug($post_cat);
/* Add the quote citation to the content if it exists */
if (!empty($_POST['post_citation']) && 'quote' == $post_cat->slug) {
$post_content = '<p>' . $post_content . '</p><cite>' . $_POST['post_citation'] . '</cite>';
}
$post_content = p2_list_creator($post_content);
$post_id = wp_insert_post(array('post_author' => $user_id, 'post_title' => $post_title, 'post_content' => $post_content, 'post_type' => $post_type, 'post_category' => array($post_cat->cat_ID), 'tags_input' => $tags, 'post_status' => 'publish'));
echo $post_id ? $post_id : '0';
}
示例14: cherry_plugin_import_categories
function cherry_plugin_import_categories()
{
$nonce = $_POST['nonce'];
if (!wp_verify_nonce($nonce, 'import_ajax-nonce')) {
exit('instal_error');
}
if (session_id() != "import_xml") {
session_name("import_xml");
session_start();
}
do_action('cherry_plugin_import_categories');
$categories_array = $_SESSION['categories'];
$categories_array = apply_filters('wp_import_categories', $categories_array);
if (empty($categories_array)) {
exit('import_tags');
}
foreach ($categories_array as $cat) {
// if the category already exists leave it alone
$term_id = term_exists($cat['category_nicename'], 'category');
if ($term_id) {
if (is_array($term_id)) {
$term_id = $term_id['term_id'];
}
if (isset($cat['term_id'])) {
$_SESSION['processed_terms'][intval($cat['term_id'])] = (int) $term_id;
}
continue;
}
$category_parent = empty($cat['category_parent']) ? 0 : category_exists($cat['category_parent']);
$category_description = isset($cat['category_description']) ? $cat['category_description'] : '';
$catarr = array('category_nicename' => $cat['category_nicename'], 'category_parent' => $category_parent, 'cat_name' => $cat['cat_name'], 'category_description' => $category_description);
$id = wp_insert_category($catarr);
if (!is_wp_error($id)) {
if (isset($cat['term_id'])) {
$_SESSION['processed_terms'][intval($cat['term_id'])] = $id;
}
} else {
continue;
}
}
unset($_SESSION['categories']);
exit('import_tags');
}
示例15: add_categorie
/**
* Adds the categorie to the relevant site
*
* @param $categorie
*
* @return int
*/
public function add_categorie($categorie)
{
$categorie_args = array('cat_name' => $categorie->name, 'category_description' => $categorie->description, 'category_nicename' => $categorie->slug, 'category_parent' => $categorie->parent);
return wp_insert_category($categorie_args);
}