0

我正在使用这个前端代码来创建一个帖子。目前它确实创建了一个帖子,但唯一有效的是标题。

我的帖子中还有一个包含一些字段(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>

任何帮助都会很棒。

4

1 回答 1

0

Okay, couple things:

  1. It's not clear to me what you're trying to do here. Do you need users to have the ability to add the values submitted in this meta box to any post? If not, why not create a custom post type that you can tweak to your heart's content?
  2. If the answer to #1 is yes, then you're still going to want to attach your save methods differently. Something like:

    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' );
    
于 2013-10-28T14:22:00.067 回答