繁体   English   中英

带表单的wp_insert_post

[英]wp_insert_post with a form

<?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>

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

还有什么应该进入表格的行动?

谢谢您的帮助。

这应该工作。

<?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部分保留在标题的顶部。

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

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

谢谢

试试这段代码

  <?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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM