Ajouter une méta-boîte spéciale au type de message personnalisé
1 réponses
- votes
-
- 2015-05-01
L'ensemble du codeest de répliquer leprocessus,pas de donner le code réel. Vous devez comprendre comment votreprocessuspeut être adapté à ce scénario. J'ai commentétoutes les lignespossibles,veuillez consulter les commentairesen lignepourbien comprendre.
Étapen ° 1
Tout d'abord,lapremièrepartie de votre code crée une Meta Boxpersonnalisée,alors utilisez lapartiepour créer votreméta-boxen premier:
<?php //making the meta box (Note: meta box != custom meta field) function wpse_add_custom_meta_box_2() { add_meta_box( 'custom_meta_box-2', // $id 'Dauer2', // $title 'show_custom_meta_box_2', // $callback 'project', // $page 'normal', // $context 'high' // $priority ); } add_action('add_meta_boxes', 'wpse_add_custom_meta_box_2'); ?>
Étapen ° 2
Votreméta-boîteest doncprêtemaintenant. Vous devezmaintenant créer des champs deformulairepour obtenir les données utilisateur,et pour celanous utilisons lafonction
$callback
quenous venons de déclarer:<?php //showing custom form fields function show_custom_meta_box_2() { global $post; // Use nonce for verification to secure data sending wp_nonce_field( basename( __FILE__ ), 'wpse_our_nonce' ); ?> <!-- my custom value input --> <input type="number" name="wpse_value" value=""> <?php } ?>
Étapen ° 3
Lors de la sauvegarde de lapublication,les deux champs
post
des valeurs,maintenantnous devons lesenregistrer là oùnous voulons lesenregistrer.<?php //now we are saving the data function wpse_save_meta_fields( $post_id ) { // verify nonce if (!isset($_POST['wpse_our_nonce']) || !wp_verify_nonce($_POST['wpse_our_nonce'], basename(__FILE__))) return 'nonce not verified'; // check autosave if ( wp_is_post_autosave( $post_id ) ) return 'autosave'; //check post revision if ( wp_is_post_revision( $post_id ) ) return 'revision'; // check permissions if ( 'project' == $_POST['post_type'] ) { if ( ! current_user_can( 'edit_page', $post_id ) ) return 'cannot edit page'; } elseif ( ! current_user_can( 'edit_post', $post_id ) ) { return 'cannot edit post'; } //so our basic checking is done, now we can grab what we've passed from our newly created form $wpse_value = $_POST['wpse_value']; //simply we have to save the data now global $wpdb; $table = $wpdb->base_prefix . 'project_bids_mitglied'; $wpdb->insert( $table, array( 'col_post_id' => $post_id, //as we are having it by default with this function 'col_value' => intval( $wpse_value ) //assuming we are passing numerical value ), array( '%d', //%s - string, %d - integer, %f - float '%d', //%s - string, %d - integer, %f - float ) ); } add_action( 'save_post', 'wpse_save_meta_fields' ); add_action( 'new_to_publish', 'wpse_save_meta_fields' ); ?>
Comme voustraitez avec unetablepersonnalisée,nousnousen tenons à la classe $ wpdb pour stockeren toute sécurité les donnéesnécessaires.
Veuilleznoter que cen'estpas le code dont vous avezbesoin,c'est l'idéeet leprocessuspar lesquels vouspouvezmaintenantfaçonner votre chemin. N'oubliezpas deux choses:
-
show_custom_meta_box_2()
est la zone où setrouve votreformulaire HTML,traitez cettepartie comme un simpleformulaire HTML,et -
wpse_save_meta_fields()
accroché aux actionsnécessaires,enregistrera les données duformulaire lorsque lemessage serapublié/sauvegardé/mis àjour. Effectuez unnettoyageet une validation appropriésici.
J'espère que cela aide. Merci à @toschopour le chat récent . & lt; 3
The whole bunch of code is to replicate the process, not to give the actual code. You have to figure out how your process can be meet up with this scenario. I commented on every possible line, please see the inline comments to get proper understanding.
Step #1
First of all, the first portion of your code makes a Custom Meta Box, so use the portion to make your meta box first:
<?php //making the meta box (Note: meta box != custom meta field) function wpse_add_custom_meta_box_2() { add_meta_box( 'custom_meta_box-2', // $id 'Dauer2', // $title 'show_custom_meta_box_2', // $callback 'project', // $page 'normal', // $context 'high' // $priority ); } add_action('add_meta_boxes', 'wpse_add_custom_meta_box_2'); ?>
Step #2
So your meta box is ready now. Now you have to make some Form fields to get user data, and for that we are using the
$callback
function what we just declared:<?php //showing custom form fields function show_custom_meta_box_2() { global $post; // Use nonce for verification to secure data sending wp_nonce_field( basename( __FILE__ ), 'wpse_our_nonce' ); ?> <!-- my custom value input --> <input type="number" name="wpse_value" value=""> <?php } ?>
Step #3
On the post save the two fields will
post
values, now we have to save 'em where we want to save 'em.<?php //now we are saving the data function wpse_save_meta_fields( $post_id ) { // verify nonce if (!isset($_POST['wpse_our_nonce']) || !wp_verify_nonce($_POST['wpse_our_nonce'], basename(__FILE__))) return 'nonce not verified'; // check autosave if ( wp_is_post_autosave( $post_id ) ) return 'autosave'; //check post revision if ( wp_is_post_revision( $post_id ) ) return 'revision'; // check permissions if ( 'project' == $_POST['post_type'] ) { if ( ! current_user_can( 'edit_page', $post_id ) ) return 'cannot edit page'; } elseif ( ! current_user_can( 'edit_post', $post_id ) ) { return 'cannot edit post'; } //so our basic checking is done, now we can grab what we've passed from our newly created form $wpse_value = $_POST['wpse_value']; //simply we have to save the data now global $wpdb; $table = $wpdb->base_prefix . 'project_bids_mitglied'; $wpdb->insert( $table, array( 'col_post_id' => $post_id, //as we are having it by default with this function 'col_value' => intval( $wpse_value ) //assuming we are passing numerical value ), array( '%d', //%s - string, %d - integer, %f - float '%d', //%s - string, %d - integer, %f - float ) ); } add_action( 'save_post', 'wpse_save_meta_fields' ); add_action( 'new_to_publish', 'wpse_save_meta_fields' ); ?>
As you are dealing with custom table, so we are sticking with $wpdb class to securely store necessary data.
Please note, this is not the code what you require, this is the idea and the process how you can now shape your path. Just remember two things:
show_custom_meta_box_2()
is the area where your HTML form is, treat this part like a simple HTML form, andwpse_save_meta_fields()
hooked to necessary actions, will save the data of the form when the post is published / saved / updated. Take proper sanitization and validation here.
Hope it helps. Thanks to @toscho for the recent chat. <3
-
Wow - super!Je vais l'essayermaintenantet fournir des commentaires.Mercibeaucoup!!!Wow - great! I will try it now and provide feedback. Many thanks!!!
- 0
- 2015-05-01
- cgscolonia
-
J'aitrouvé une solutionet vos conseils ont ététrès utiles!Merciencore :)I found a solution and your hints were very helpfull! Thanks again :)
- 0
- 2015-05-05
- cgscolonia
J'ai réussi à ajouter uneméta-boîte àmontype demessagepersonnalisé (voir le code ci-dessous) Maintenant,je dois ajouter une autreméta-boîte. Avec cetteboîte,je veux afficher/modifier une valeur d'une autretable appelée wp_project_bids_mitglied. Cetableau contient une ligne avec lesidentifiants depublicationet une ligne avec les valeurs (0,1,2) queje voudrais éditer/afficher dansmonbackend. Comment dois-je changer le codepour atteindre cet objectif? Merci!