I’m building a CRM-Theme for WordPress. So i post, edit and delete posts from the Frontend. I’m on a good way to resolve all problems, i think. But there is one failure. At the index there is a “edit” and “delete” button. If i click on “edit” I’m getting to a form. I can change the post_tags there. Changes will be saved, but the single words don’t get separated. All words in the input-field are one huge tag – i can see it in a widget at the sidebar. But i use separators… If you have 2 minutes:
Here (in the index.php) i enable to delete a post:
if( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
set_query_var( 'postid1', $_POST['postid'] ); /* set the "postid" value from the delete button of the post we choose to delete into "postid1" */
wp_delete_post( get_query_var( 'postid1'), true ); /* delete the post we choosed */
};
get_header(); ?>
In the loop of the index you can find the following snippet. First form is for edit, second form for delete:
<form class="edit" action="<?php echo esc_url( home_url( '/' ) ); ?>editieren-formular/" method="post">
<input type="hidden" name="postid" value="<?php the_ID(); ?>" /> <?php /* get the post ID into "postid" and later pass it to "editieren-formular.php" */ ?>
<input type="submit" value="Edit" />
</form>
<form class="delete" action="" method="post">
<input type="hidden" name="postid" value="<?php the_ID(); ?>" /> <?php /* get the post ID into "postid" and later delete the post */ ?>
<input type="submit" value="Delete" />
</form>
Everything fine. Now the editieren-formular.php, which is a page-template for a site, that is called “Editiere Formular”. “editieren-formular” is the slug.
<?php
/*
Template Name: Editieren Formular
*/
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "edit_post" && isset($_POST['postid'])) {
$post_to_edit = array();
$post_to_edit = get_post($_POST['postid']);
/* these are the fields that we are editing in the form below. */
$title = $_POST['title'];
$description = $_POST['description'];
$vorname = $_POST['vorname'];
$name = $_POST['name'];
/* this code will save the title and description into the post_to_edit array */
$post_to_edit->post_title = $title;
$post_to_edit->post_content = $description;
/* honestly i can't really remember why i added this code but it is a must */
$pid = wp_update_post($post_to_edit);
/* save taxonomies: post ID, form name, taxonomy name, if it appends(true) or rewrite(false) */
wp_set_post_terms($pid, array($_POST['cat']),'firmen',false);
wp_set_post_terms($pid, array($_POST['post_tags']),'post_tag',false);
//UPDATE CUSTOM FIELDS WITH THE NEW INFO
//CHANGE TO YOUR CUSTOM FIELDS AND ADD AS MANY AS YOU NEED
update_post_meta($pid, 'vorname', $vorname);
update_post_meta($pid, 'name', $name);
//REDIRECT USER WHERE EVER YOU WANT AFTER DONE EDITING
wp_redirect( 'http://crm-wordpress-theme.wellseo.de' );
...
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
get_header();
$post_to_edit = get_post($_POST['postid']);
$terms = get_the_terms($post_to_edit->ID, 'firmen');
$tags = strip_tags( get_the_term_list( $post_to_edit->ID, 'post_tag', '', ', ', '' ) );
<?php $term_name = strip_tags( get_the_term_list( $post_to_edit->ID, 'category', '', ', ', '' ) ); ?> <!-- get the category name of this post -->
<?php $term_obj = get_term_by('name', $term_name, 'category'); ?> <!-- get the current term object -->
<?php $term_id = $term_obj->term_id ;?> <!-- get this post's term id -->
<?php $args = array(
'selected' => $term_id,
'name' => 'cat',
'class' => 'postform',
'tab_index' => 10,
'depth' => 2,
'hierarchical' => 1,
'hide_empty' => false ); /* array for wp_dropdown_category to display with the current post category selected by default */ ?>
<div id="content" role="main">
<form id="edit_post" name="edit_post" method="post" action="" enctype="multipart/form-data">
<fieldset name="title">
<label for="title">Title:</label><br />
<input type="text" id="title" value="<?php echo $post_to_edit->post_title; ?>" tabindex="5" name="title" />
</fieldset>
<fieldset id="category">
<label for="cat" ><?php wp_dropdown_categories( $args ); ?></label>
</fieldset>
<fieldset class="content">
<label for="description">Discount Description:</label><br />
<textarea id="description" tabindex="15" name="description"><?php echo $post_to_edit->post_content; ?></textarea>
</fieldset>
<!-- BELOW ARE THE CUSTOM FIELDS -->
<fieldset class="vorname">
<label for="vorname">Vorname:</label><br />
<input type="text" value="<?php echo get_post_meta($post_to_edit->ID,'vorname', true); ?>" id="vorname" tabindex="20" name="vorname" />
</fieldset>
<fieldset class="name">
<label for="name">Name:</label><br />
<input type="text" value="<?php echo get_post_meta($post_to_edit->ID,'name', true); ?>" id="name" tabindex="20" name="name" />
</fieldset>
<fieldset class="tags">
<label for="post_tags">Additional Keywords (comma separated):</label><br />
<input type="text" value="<?php echo $tags; ?>" tabindex="35" name="post_tags" id="post_tags" />
</fieldset>
<fieldset class="submit">
<input type="submit" value="Speichern" tabindex="40" id="submit" name="submit" />
</fieldset>
<input type="hidden" name="postid" value="<?php echo $post_to_edit->ID; ?>" />
<input type="hidden" name="action" value="edit_post" />
<input type="hidden" name="change_cat" value="" />
<?php // wp_nonce_field( 'new-post' ); ?>
</form>
</div><!-- #content -->
<?php get_sidebar(); ?>
<?php get_sidebar('two'); ?>
<?php get_footer(); ?>
Ok, found a solution by myself. In the past i saved post_tags with
Now i save the post_tags with
and it works. This way is the same method, i use to publish a new post from the frontend. Changed only this, nothing else.
When you’re trying to insert
$_POST['post_tags']
it’s actually persisting one long string. What you call ‘separating’ is just creating one single string from all the tags with some separators here:The end result though is a simple string so you need to split that before calling
wp_set_post_terms
– explode it as an array and then you’ll get an array with the tags instead of a comma-separated string.