How to create a new PostBox in WordPress

0

I would like to know if it is possible, and how, to create a BoxPost type like that of the image, but inside the box , instead of categories I wanted something "Titulo", "Ano", "Director", "Cast", <select> .. these things.

And then, if possible, how to fetch the value of each element in return with the publication ...

    
asked by anonymous 14.12.2015 / 16:25

1 answer

1

There is no "BoxPost" and probably why you have not found how to do it yet, because the correct name is "Metabox".

You have full documentation on this and in Portuguese at link

In case it is very simple to do, you will have your "Metabox" and within it "Custom fields" (also easy to find with the name "Post meta").

Here's an example of how to register your "Metaboxes" and save your "Custom fields":

// Register metabox
add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );

// Salvar custom fields
add_action( 'save_post', 'myplugin_save_postdata' );

/* Adiciona uma meta box na coluna principal das telas de edição de Post e Página */
function myplugin_add_custom_box() {
    $screens = array( 'post', 'page' );
    foreach ($screens as $screen) {
        add_meta_box(
            'myplugin_sectionid',
            __( 'My Post Section Title', 'myplugin_textdomain' ),
            'myplugin_inner_custom_box',
            $screen
        );
    }
}

/* Imprime o conteúdo da meta box */
function myplugin_inner_custom_box( $post ) {

    // Faz a verificação através do nonce
    wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );

    // Os campos para inserção dos dados
    // Use get_post_meta para para recuperar um valor existente no banco de dados e usá-lo dentro do atributo HTML 'value' 
    $value = get_post_meta( $post->ID, '_my_meta_value_key', true );
    echo '<label for="myplugin_new_field">';
       _e("Description for this field", 'myplugin_textdomain' );
    echo '</label> ';
    echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
}

/* Quando o post for salvo, salvamos também nossos dados personalizados */
function myplugin_save_postdata( $post_id ) {

    // É necessário verificar se o usuário está autorizado a fazer isso
    if ( 'page' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }
    } else {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }

    // Agora, precisamos verificar se o usuário realmente quer trocar esse valor
    if ( ! isset( $_POST['myplugin_noncename'] ) || ! wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) ) {
        return;
    }

    // Por fim, salvamos o valor no banco

    // Recebe o ID do post
    $post_ID = $_POST['post_ID'];

    // Remove caracteres indesejados
    $mydata = sanitize_text_field( $_POST['myplugin_new_field'] );

    // Adicionamos ou atualizados o $mydata 
    update_post_meta( $post_ID, '_my_meta_value_key', $mydata );
}

Notice that Custom field has been saved in update_post_meta($post_ID, '_my_meta_value_key', $mydata); and therefore the field key is _my_meta_value_key .

This way it is possible to retrieve this value inside the post loop using get_post_meta( $post->ID, '_my_meta_value_key', true ); .

You can learn more about Custom Fields in the official documentation , but also in English.

    
15.12.2015 / 02:02