0

所以我大部分时间都在工作 - 我可以用这个从前端创建一个类别......

<?php 
if(isset($_POST['submit'])){
if(!empty($_REQUEST['newcat'])){
$cat_ID = get_cat_ID( $_POST['newcat'] );    
//If not create new category  
if($cat_ID == 0) {  
$cat_name = $_POST['newcat'];  

$parenCatID = 0;
$new_cat_ID = wp_create_category($cat_name,$parenCatID);  
echo 'Category added successfully';
}  

else {echo 'That category already exists';}

}
}
?>

<form action="" method="post">
<label for="newcat">Project Name</label>
<input type="text" name="newcat" value="" />

<label for="description">Description</label>
<input type="text" name="description" value="" />

<input type="submit" name="submit" value="Submit" />
</form>

...但我不确定如何修改 PHP,因此也添加了描述。

有任何想法吗?

提前致谢。

4

1 回答 1

1

wp_create_category不允许添加描述,您需要改为使用wp_insert_category。您还应该清理输入数据:

<?php 
if( isset( $_POST['submit'] ) ) {
    if( !empty( $_REQUEST['newcat'] ) ) {

        $cat_ID = get_cat_ID( sanitize_title_for_query($_POST['newcat']) );  

        // Check if category exists
        if($cat_ID == 0) {

            $cat_name = sanitize_text_field($_POST['newcat']);  
            $cat_desc = sanitize_text_field($_POST['description']);
            $cat_slug = sanitize_title_with_dashes($cat_name);

            $my_cat = array(
                'cat_name' => $cat_name, 
                'category_description' => $cat_desc, 
                'category_nicename' => $cat_slug, 
                'category_parent' => 0
            );

            if( wp_insert_category( $my_cat ) ) {
                echo 'Category added successfully';
            } else {
                echo 'Error while creating new category';
            }

        } else {
            echo 'That category already exists';
        }
    }
}
?>
于 2013-10-16T08:25:54.067 回答