wp_insert_post with a form

后端 未结 3 420
甜味超标
甜味超标 2020-12-13 22:42


        
相关标签:
3条回答
  • 2020-12-13 23:12

    This should work.

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

    If input with name new_post and value of 0 then add the post. No action needed for the form, but you should keep the PHP part on the top of the header.

    0 讨论(0)
  • 2020-12-13 23:27

    try this code

      <?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>
    
    0 讨论(0)
  • 2020-12-13 23:36

    leave the form action blank means it will flow form data from entry point of application to the current url in address bar

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

    Thanks

    0 讨论(0)
提交回复
热议问题