带表单的wp_insert_post

时间:2010-12-01 06:52:22

标签: php wordpress

<?php

$submitted = $_POST['submit'];
$post-title= $_POST['post_title'];
$post-content= $_POST['post_content'];


$new_post = array(
            'post_title' => '$post-title',
            'post_content' => '$post-content',
            'post_status' => 'publish',
            'post_author' => $user_ID,

        );
if(isset($submitted)){
wp_insert_post($new_post);
}

?>

<form method="post" action=" "> 
<input type="text" name="post_title" size="45" id="input-title"/>

<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea> 


<input type="hidden" name="cat" value="7,100"/> 

<input class="subput round" type="submit" name="submit" value="Post"/>
</form>

我在表单中测试它并且工作正常。但是对于某些原因,我似乎无法让它与表格一起工作。任何想法?

该表格的动作应该是什么?

感谢您的帮助。

3 个答案:

答案 0 :(得分:10)

这应该有用。

<?php 
if(isset($_POST['new_post']) == '1') {
    $post_title = $_POST['post_title'];
    $post_category = $_POST['cat'];
    $post_content = $_POST['post_content'];

    $new_post = array(
          'ID' => '',
          'post_author' => $user->ID, 
          'post_category' => array($post_category),
          'post_content' => $post_content, 
          'post_title' => $post_title,
          'post_status' => 'publish'
        );

    $post_id = wp_insert_post($new_post);

    // This will redirect you to the newly created post
    $post = get_post($post_id);
    wp_redirect($post->guid);
}      
?>      

<form method="post" action=""> 
    <input type="text" name="post_title" size="45" id="input-title"/>
    <?php wp_dropdown_categories('orderby=name&hide_empty=0&exclude=1&hierarchical=1'); ?>
    <textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea> 
    <input type="hidden" name="new_post" value="1"/> 
    <input class="subput round" type="submit" name="submit" value="Post"/>
</form>

如果输入名称 new_post 且值为0,则添加帖子。表单不需要任何操作,但您应该将PHP部分保留在标题的顶部。

答案 1 :(得分:0)

将表单操作留空意味着它会将表单数据从应用程序的入口点流向地址栏中的当前URL

<form method="post" action=""> 

由于

答案 2 :(得分:0)

尝试此代码

  <?php 
if(isset($_POST['submit'])) {


    $post_title = $_POST['post_title'];
    $post_category = $_POST['cat'];
    $post_content = $_POST['post_content'];
    $custom1= $_POST['add_info'];
    $image_name=$_FILE['feture_image']['name']; 

    $image_url=$_FILE['feture_image']['tmp_name'];

    $new_post = array(
          'post_type' => 'news', 
          'post_content' => $post_content, 
          'post_title' => $post_title,
          'post_status' => 'publish'
        );

    $post_id = wp_insert_post($new_post);
    $post = get_post($post_id);



    add_post_meta($post_id, '_add_info_data', $custom1);    
    wp_set_object_terms( $post_id, array($post_category), '< category_slug_name >' ); 



     $upload_dir = wp_upload_dir();
    $image_data = file_get_contents($image_url);
    $filename = basename($image_url);
    if(wp_mkdir_p($upload_dir['path']))     $file = $upload_dir['path'] . '/' . $filename;
    else                                    $file = $upload_dir['basedir'] . '/' . $filename;
    file_put_contents($file, $image_data);

    $wp_filetype = wp_check_filetype($filename, null );
    $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => sanitize_file_name($filename),
        'post_content' => '',
        'post_status' => 'inherit'
    );
    $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
    $res1= wp_update_attachment_metadata( $attach_id, $attach_data );
    $res2= set_post_thumbnail( $post_id, $attach_id );







}      


?>      






<form method="post" enctype="multipart/form-data" > 
    <input type="text" name="post_title" size="45" id="input-title"/>
    <?php wp_dropdown_categories('orderby=name&hide_empty=0&exclude=1&hierarchical=1&taxonomy=< taxonomy name >&post_type=< post_type >'); ?>
    <textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea> 
    <input type="file" name="feture_image">
    <input type="text" name="add_info">
    <input class="subput round" type="submit" name="submit" value="Post"/>   
</form>