How to create a new Postbox in Wordpress

Asked

Viewed 178 times

0

I would like to know if it is possible, and how, to create a kind of Boxpost like the image, only inside the box box, instead of categories wanted something to ask for.. "Title", "Year", "Director", "Cast", a <select>.. those things.

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

inserir a descrição da imagem aqui

  • give a look ai partner see if help http://www.marcelotorresweb.com/usando-taxonomia-e-posts-personalizados-no-wordpress-3-0/

  • Maybe what you want is to create a new custom taxonomy and make it subordinate to normal posts (instead of subordinating to a CPT, custom post type).

2 answers

3


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

It has complete documentation about this and in Portuguese in https://codex.wordpress.org/pt-br:add_meta_box

In case it is quite simple to be done, you will get your "Metabox" and inside 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 );
}

Note that Custom field was saved in update_post_meta($post_ID, '_my_meta_value_key', $mydata); and with that the key to the field is _my_meta_value_key.

This way it is possible to recover 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 official documentation also, but in English.

  • From what I understand it’s not even Metabox but taxonomy

  • @Otto just read: 'instead of categories wanted something to ask for.. "Title", "Year", "Director", "Cast", a <select>.. Those things.". So it doesn’t seem to be a taxonomy.

  • It’s really a bit confusing understanding based on the image ....

  • Tip: add_action('save_post', 'funk', 10, 2); function funk($post_id, $post_object) {}

0

The secret is to put the parameter side in function add_meta_box

function custom_add_meta_boxes() {
    function custom_add_meta_box_teste() {
        ?>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore adipisci reprehenderit laborum quos cupiditate vitae? Voluptatum unde veniam optio dolores! Eos vel tenetur maiores placeat! Asperiores ipsum tempore ut in?</p>
        <?php
    }
    add_meta_box( 'teste', 'Teste', 'custom_add_meta_box_teste', null, 'side' );
}
add_action( 'add_meta_boxes', 'custom_add_meta_boxes' );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.