2013-10-28 46 views
0

我正在使用此前端代碼創建帖子。目前它確實創建了一個帖子,但唯一可行的是標題。從前端開始工作

我也有一些自定義的metabox有一些字段(more_info,priority和duedate和)在我的文章中,但我不確定是否將它們正確地放入數組中。請注意,在後端這些額外的領域工作得很好。

這裏是我的代碼:

<?php 
if('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action'])) { 

if (isset ($_POST['title'])) { 
    $title = $_POST['title']; 
} else { 
    echo 'Please enter a title'; 
} 
if (isset ($_POST['description'])) { 
    $description = $_POST['description']; 
} 

// Add the content of the form to $post as an array 
$post = array(
    'post_title'  => $title, 
    'post_content' => $description, 
    'more_info' => $more_info, 
    'priority'  => $priority, 
    'duedate'  => $duedate, 
    'post_category' => $_POST['cat'], 
    'post_status' => 'publish', 
    'post_type' => $_POST['post'] 
); 
wp_insert_post($post); 
} 
do_action('wp_insert_post', 'wp_insert_post'); 

?> 

<form action="" id="new_post" method="post"> 
<label for="title">Task Name </label> 
<input type="text" id="title" name="title" value="" /> 

<label for="minfo">Additional information</label> 
<textarea name="minfo" id="minfo" value=""></textarea> 

<label>Due date</label> 
<input type="text" id="duedate" name="duedate" /> 


    <strong>Priority</strong> 
    <label><input type="radio" name="priority" value="low" id="low">Low</label> 
    <label><input type="radio" name="priority" value="normal" id="normal" checked>Normal</label> 
    <label><input type="radio" name="priority" value="high" id="high">High</label> 
    <label><input type="radio" name="priority" value="urgent" id="urgent">Urgent</label> 

<input type="submit" name="submit" value="Add" /> 

<input type="hidden" name="cat" id="cat" value="<?php echo $cat_id = get_query_var('cat'); ?>" /> 
<input type="hidden" name="post_type" id="post_type" value="post" /> 
<input type="hidden" name="action" value="post" /> 
<?php wp_nonce_field('new-post'); ?> 

</form> 

任何援助將是真棒。

回答

0

好吧,兩件事情:

  1. 你想在這裏做什麼目前尚不清楚給我。您是否需要用戶有能力將此元框中提交的值添加到任何帖子中?如果沒有,爲什麼不創建一個自定義帖子類型,你可以調整你的心臟的內容?
  2. 如果#1的答案是肯定的,那麼你仍然希望以不同的方式附加你的保存方法。喜歡的東西:

    add_meta_box(
        'my-custom-meta', // Unique ID 
        esc_html__('Custom Attributes', 'my_scope'), // Title 
        'my_custom_meta_box', // Callback function 
        'post', // Admin page (or post type) 
        'normal', // Context 
        'high' // Priority 
    ); 
    
    
    function my_custom_meta_box($object) { ?> 
    
        <?php wp_nonce_field('my_custom_inner', 'my_custom_inner_nonce'); ?> 
    
        <p> 
         <label for="minfo">Additional information</label> 
         <textarea name="minfo" id="minfo" value="<?php echo esc_attr(get_post_meta($object->ID, 'minfo', true)); ?>"></textarea> 
        </p> 
        <p> 
         <label>Due date</label> 
         <input type="text" id="duedate" name="duedate" value="<?php echo esc_attr(get_post_meta($object->ID, 'duedate', true)); ?>" /> 
        </p> 
        <p> 
         <strong>Priority</strong> 
         <label><input type="radio" name="priority" value="low" id="low"<?php if (get_post_meta($object->ID, 'priority', true) == 'low') { echo ' checked'; }?>>Low</label> 
         <label><input type="radio" name="priority" value="normal" id="normal"<?php if (get_post_meta($object->ID, 'priority', true) == 'normal') { echo ' checked'; }?>>Normal</label> 
         <label><input type="radio" name="priority" value="high" id="high"<?php if (get_post_meta($object->ID, 'priority', true) == 'high') { echo ' checked'; }?>>High</label> 
         <label><input type="radio" name="priority" value="urgent" id="urgent"<?php if (get_post_meta($object->ID, 'priority', true) == 'urgent') { echo ' checked'; }?>>Urgent</label> 
        </p> 
    
    <?php } 
    
    
    function save_my_meta($post_id) { 
        // Check if our nonce is set. 
        if (! isset($_POST['my_custom_inner_nonce'])) 
         return $post_id; 
    
        $nonce = $_POST['my_custom_inner_nonce']; 
    
        // Verify that the nonce is valid. 
        if (! wp_verify_nonce($nonce, 'my_custom_inner')) 
         return $post_id; 
    
        // If this is an autosave, our form has not been submitted, so we don't want to do anything. 
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
         return $post_id; 
    
        // Check the user's permissions. 
        if ($_REQUEST['post_type'] == 'post') { 
         if (! current_user_can('edit_post', $post_id)) 
          return $post_id; 
        } 
    
        /* Cast the returned vaiables to an array for processing */ 
        $my_meta['minfo'] = $_REQUEST['minfo']; 
        $my_meta['duedate'] = $_REQUEST['duedate']; 
        $my_meta['priority'] = $_REQUEST['priority']; 
    
        foreach ($my_meta as $key => $value) { // Cycle through the $events_meta array! 
         if(get_post_meta($post_id, $key)) { // If the custom field already has a value 
          update_post_meta($post_id, $key, sanitize_text_field($value)); 
         } else { 
          add_post_meta($post_id, $key, sanitize_text_field ($value)); 
         } 
         if(!$value) delete_post_meta($post_id, $key); // Delete if blank 
        } 
    } 
    add_action('save_post', 'save_my_meta'); 
    
+0

我已經有「職位」這在後端(看起來像這樣i.imgur.com/URRFHpO.png)正常工作的自定義metabox。它只是試圖從前端(從我的一個模板中的表單)創建帖子。 – PHPnoob