Édition de poste frontal à l'aide d'un formulaire
3 réponses
- votes
-
- 2011-02-21
Si vous souhaitezmodifier unmessageexistant,essayezmonplugin Front-end Editor .
Si vous souhaitez créer denouveauxmessages,essayez l'un de ces éléments:
If you want to edit an existing post, try my Front-end Editor plugin.
If you want to create new posts, try one of these:
-
Merci Scribu.Votrepluginestfantastique,maisilne répondpas àmesbesoins.J'essaie demodifier unmessageexistant avec unformulaire.J'aiposé une question similaire sur lamodification duprofil d'un utilisateur avec unformulaire dans lefrontendet j'ai reçu cette réponse: http://wordpress.stackexchange.com/questions/9775/how-to-edit-a-user-profile-on-lefront-end Cela aparfaitementfonctionnépourmoi.S'ilexiste une solution similairepour éditer des articles,je vousen seraistrès reconnaissant.Thanks Scribu. Your plugin is fantastic, but it does not fit my need. I am trying to edit an existing post with a form. I asked a similar question about editing a user's profile with a form in the frontend and I received this response: http://wordpress.stackexchange.com/questions/9775/how-to-edit-a-user-profile-on-the-front-end This worked perfectly for me. If there is a similar solution for editing posts I would be so grateful.
- 0
- 2011-02-21
- Carson
-
est-ilpossible d'ajouter desmétabox avec lui??is it possible to add metaboxes along with it??
- 0
- 2011-06-17
- nickfrancis.me
-
@nickfancis.me utilise des commentairespour cegenre de choses - c'estpour ça qu'ils sont là!@nickfancis.me use comments for this sort of thing - that's what they're there for!
- 0
- 2011-06-17
- TheDeadMedic
-
@nickfrancis.me Lesmétaboxes sont strictement réservées aubackend.Peut-être que vous voulez dire des widgets?@nickfrancis.me Metaboxes are strictly for the backend. Maybe you mean widgets?
- 0
- 2011-06-17
- scribu
-
- 2013-07-15
Voici une solution debasepourmettre àjour un article/unepage. J'ai ajouté une démo rapide des champsmétapersonnalisés. C'est assezbasique,mais vousindiquera la direction de l'édition sansplugin des articles sur lefront-end. Cen'estpastrèsflexible,mais vouspouvez y ajoutertout ce dont vous avezbesoin.
Ajoutez ce code dans votreboucle:
<form id="post" class="post-edit front-end-form" method="post" enctype="multipart/form-data"> <input type="hidden" name="post_id" value="<?php the_ID(); ?>" /> <?php wp_nonce_field( 'update_post_'. get_the_ID(), 'update_post_nonce' ); ?> <p><label for="post_title">Title</label> <input type="text" id="post_title" name="post_title" value="<?php echo $post->post_title; ?>" /></p> <p><?php wp_editor( $post->post_content, 'postcontent' ); ?></p> <p><label for="post_title">Test</label> <?php $value = get_post_meta(get_the_ID(), 'edit_test', true); ?> <input type="text" id="edit_test" name="edit_test" value="<?php echo $value; ?>" /></p> <p><label for="post_title">Test 2</label> <?php $value = get_post_meta(get_the_ID(), 'edit_test2', true); ?> <input type="text" id="edit_test2" name="edit_test2" value="<?php echo $value; ?>" /></p> <input type="submit" id="submit" value="Update" /> </form>
Ajoutezensuite ce codeen haut de lapagepourtraiter leformulaire:
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && ! empty($_POST['post_title']) && isset($_POST['update_post_nonce']) && isset($_POST['postcontent']) ) { $post_id = $_POST['post_id']; $post_type = get_post_type($post_id); $capability = ( 'page' == $post_type ) ? 'edit_page' : 'edit_post'; if ( current_user_can($capability, $post_id) && wp_verify_nonce( $_POST['update_post_nonce'], 'update_post_'. $post_id ) ) { $post = array( 'ID' => esc_sql($post_id), 'post_content' => esc_sql($_POST['postcontent']), 'post_title' => esc_sql($_POST['post_title']) ); wp_update_post($post); if ( isset($_POST['edit_test']) ) update_post_meta($post_id, 'edit_test', esc_sql($_POST['edit_test']) ); if ( isset($_POST['edit_test2']) ) update_post_meta($post_id, 'edit_test2', esc_sql($_POST['edit_test2']) ); } else { wp_die("You can't do that"); } }
Here is a basic solutions for updating a post/page. I added a quick demo of custom meta fields. This is pretty basic, but will point you in the direction of plugin-less editing of posts on the front-end. This isn't super flexible, but you can add whatever you need to it.
Add this code into your loop:
<form id="post" class="post-edit front-end-form" method="post" enctype="multipart/form-data"> <input type="hidden" name="post_id" value="<?php the_ID(); ?>" /> <?php wp_nonce_field( 'update_post_'. get_the_ID(), 'update_post_nonce' ); ?> <p><label for="post_title">Title</label> <input type="text" id="post_title" name="post_title" value="<?php echo $post->post_title; ?>" /></p> <p><?php wp_editor( $post->post_content, 'postcontent' ); ?></p> <p><label for="post_title">Test</label> <?php $value = get_post_meta(get_the_ID(), 'edit_test', true); ?> <input type="text" id="edit_test" name="edit_test" value="<?php echo $value; ?>" /></p> <p><label for="post_title">Test 2</label> <?php $value = get_post_meta(get_the_ID(), 'edit_test2', true); ?> <input type="text" id="edit_test2" name="edit_test2" value="<?php echo $value; ?>" /></p> <input type="submit" id="submit" value="Update" /> </form>
Then add this code at the top of the page to process the form:
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && ! empty($_POST['post_title']) && isset($_POST['update_post_nonce']) && isset($_POST['postcontent']) ) { $post_id = $_POST['post_id']; $post_type = get_post_type($post_id); $capability = ( 'page' == $post_type ) ? 'edit_page' : 'edit_post'; if ( current_user_can($capability, $post_id) && wp_verify_nonce( $_POST['update_post_nonce'], 'update_post_'. $post_id ) ) { $post = array( 'ID' => esc_sql($post_id), 'post_content' => esc_sql($_POST['postcontent']), 'post_title' => esc_sql($_POST['post_title']) ); wp_update_post($post); if ( isset($_POST['edit_test']) ) update_post_meta($post_id, 'edit_test', esc_sql($_POST['edit_test']) ); if ( isset($_POST['edit_test2']) ) update_post_meta($post_id, 'edit_test2', esc_sql($_POST['edit_test2']) ); } else { wp_die("You can't do that"); } }
-
J'ai ajoutéesc_sql ().Ilm'est venu à l'esprit que vous devriez éviter que les données soient soumises àpartir d'unformulairepublic (semi-public) comme celui-là.I added esc_sql(). It just occurred to me that you should escape data being submitted from a public (semi-public) form like that.
- 0
- 2013-07-16
- Jake
-
Merci,maismonformulairen'est disponible quepour l'administrateur.Quoi qu'ilen soit,il sera utile sijamaisj'utilise ceformulairepour l'affichagepublic.Thanks, But my form is only available for Admin. Anyway it will be helpful if ever I use this form for public posting.
- 1
- 2013-07-17
- user1983017
-
- 2013-07-14
Lemoyen leplus simple serait d'utiliser quelque chose comme Ninja Forms avec l'extensionpayante suivante:
http://wpninjas.com/downloads/front-end-posting/
Vouspouvez également le coder vous-même. Vous allezessentiellement créer unformulaire,puis utiliser
wp_insert_post()
pour créer un article complet.Unexemple deformulaire:
<form action="" id="primaryPostForm" method="POST"> <fieldset> <label for="postTitle"><?php _e('Post Title:', 'framework') ?></label> <input type="text" name="postTitle" id="postTitle" class="required" /> </fieldset> <fieldset> <label for="postContent"><?php _e('Post Content:', 'framework') ?></label> <textarea name="postContent" id="postContent" rows="8" cols="30" class="required"></textarea> </fieldset> <fieldset> <input type="hidden" name="submitted" id="submitted" value="true" /> <button type="submit"><?php _e('Add Post', 'framework') ?></button> </fieldset>
et après l'envoi,vousenregistrez leprocessus quelque chose comme:
if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) { if ( trim( $_POST['postTitle'] ) === '' ) { $postTitleError = 'Please enter a title.'; $hasError = true; } $post_information = array( 'post_title' => wp_strip_all_tags( $_POST['postTitle'] ), 'post_content' => $_POST['postContent'], 'post_type' => 'post', 'post_status' => 'pending' ); wp_insert_post( $post_information ); }
Le code completet le didacticielproviennent de: http://wp.tutsplus.com/tutoriels/codage-créatif/publication-via-l'insertion-frontale/
The easiest way would be to use something like Ninja Forms with the following paid extension:
http://wpninjas.com/downloads/front-end-posting/
You could also code this yourself. Essentially you'll create a form, and then use
wp_insert_post()
to create a full post.A sample form:
<form action="" id="primaryPostForm" method="POST"> <fieldset> <label for="postTitle"><?php _e('Post Title:', 'framework') ?></label> <input type="text" name="postTitle" id="postTitle" class="required" /> </fieldset> <fieldset> <label for="postContent"><?php _e('Post Content:', 'framework') ?></label> <textarea name="postContent" id="postContent" rows="8" cols="30" class="required"></textarea> </fieldset> <fieldset> <input type="hidden" name="submitted" id="submitted" value="true" /> <button type="submit"><?php _e('Add Post', 'framework') ?></button> </fieldset>
and then on submit, you'd save process it something like:
if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) { if ( trim( $_POST['postTitle'] ) === '' ) { $postTitleError = 'Please enter a title.'; $hasError = true; } $post_information = array( 'post_title' => wp_strip_all_tags( $_POST['postTitle'] ), 'post_content' => $_POST['postContent'], 'post_type' => 'post', 'post_status' => 'pending' ); wp_insert_post( $post_information ); }
The full code and tutorial is from: http://wp.tutsplus.com/tutorials/creative-coding/posting-via-the-front-end-inserting/
-
Je veuxjustemettre àjour (modifier l'existant) lemessage/lapage,pas l'insérer.I Just want to update (edit existing) the post/page, not insert.
- 0
- 2013-07-14
- user1983017
-
Alorspourquoine pasessayer leplugin "Front End Editor" déjà référencépar Scribu?Then why not try the already-referenced "Front End Editor" plugin by Scribu?
- 0
- 2013-07-14
- helgatheviking
J'ai untype demessagepersonnalisé avec lesméta-boîtes standardet certains champspersonnalisés.Commentpuis-jemodifier un article via unformulaire dans l'interface?