Wordpress Documentation

advertise, Anuncio

Custom Fields

🧩 add_meta_box() + save_post

				
					// Agregar metabox
add_action('add_meta_boxes', function () {
  add_meta_box('info_extra', 'Información Extra', 'render_info_extra', 'libro', 'normal', 'default');
});

function render_info_extra($post) {
  $valor = get_post_meta($post->ID, '_isbn', true);
  wp_nonce_field('guardar_info_extra', 'info_extra_nonce');
  echo '<p><label>ISBN:</label><br>';
  echo '<input type="text" name="isbn" value="' . esc_attr($valor) . '" style="width:100%"></p>';
}

// Guardar datos
add_action('save_post', function ($post_id) {
  if (!isset($_POST['info_extra_nonce']) || !wp_verify_nonce($_POST['info_extra_nonce'], 'guardar_info_extra')) return;
  if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
  if (!current_user_can('edit_post', $post_id)) return;

  update_post_meta($post_id, '_isbn', sanitize_text_field($_POST['isbn']));
});

				
			

📥 get_post_meta() + update_post_meta()

				
					// Obtener meta
$isbn = get_post_meta($post_id, '_isbn', true);

// Guardar/Actualizar meta
update_post_meta($post_id, '_isbn', '1234567890');