Home › Forums › Front-end Issues › problems with post edit/add › Reply To: problems with post edit/add
I did not want to dump the forum with loads of code but since eliot suggested I do as instruced
So here goes…
In theme’s functions.php I added (for the next two solutions(links included))
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != ‘new’ )
{
return $post_id;
}
// Create a new post
$post = array(
‘post_status’ => ‘draft’ ,
‘post_title’ => ‘A title, maybe a $_POST variable’ ,
‘post_type’ => ‘post’ ,
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST[‘return’]
$_POST[‘return’] = add_query_arg( array(‘post_id’ => $post_id), $_POST[‘return’] );
// return the new ID
return $post_id;
}
add_filter(‘acf/pre_save_post’ , ‘my_pre_save_post’ );
function my_deregister_styles() {
wp_deregister_style( ‘wp-admin’ );
}
add_action( ‘wp_print_styles’, ‘my_deregister_styles’, 100 );
********************************************************
pages using the two templates have the following extra <phpcode> code
<phpcode><?php
$args = array(‘post_id’ => ‘new’,’field_groups’ => array(‘Συνδιασμοί αναρτήσεων’,’Τύπος αναρτήσεων’ ));
?></phpcode>
**** and
<phpcode><?php
$options = array(
‘post_id’ => 213, // post id to get field groups from and save data to
‘field_groups’ => array(‘Συνδιασμοί αναρτήσεων’,’Τύπος αναρτήσεων’ ), // this will find the field groups for this post (post ID’s of the acf post objects)
‘form’ => true, // set this to false to prevent the <form> tag from being created
‘form_attributes’ => array( // attributes will be added to the form element
‘id’ => ‘post’,
‘class’ => ”,
‘action’ => ”,
‘method’ => ‘post’,
),
‘return’ => add_query_arg( ‘updated’, ‘true’, get_permalink() ), // return url
‘html_before_fields’ => ”, // html inside form before fields
‘html_after_fields’ => ”, // html inside form after fields
‘submit_value’ => ‘Update’, // value for submit field
‘updated_message’ => ‘Post updated.’, // default updated message. Can be false to show no message
);
?></phpcode>
********************************************************
i am not sure I modified the $args and $options correctly but I did a couple
of attempts.I also don’t get the concept of how can we get fields from post(How’s that when you create a new post OR how will it edit a post not defined in code (ie 213) which took the place of $post->ID (although i figured that this can be implied if a wp_query is ran having effect on global(?) variables .(please explain)
********************************************************
Then I created a couple of php templates in theme editor
in themes form_create.php I added
(found on http://www.advancedcustomfields.com/resources/tutorials/using-acf_form-to-create-a-new-post/)
********************************************************
<?php
/**
* Template Name: Page with ACF form
*/
acf_form_head();
get_header();
the_post();
?>
<div id=”primary”>
<div id=”content” role=”main”>
<?php
$args = array(
‘post_id’ => ‘new’,
‘field_groups’ => array(‘Συνδιασμοί αναρτήσεων’,’Τύπος αναρτήσεων’ )
);
acf_form( $args );
?>
</div><!– #content –>
</div><!– #primary –>
in themes form_edit.php I added
(found on http://www.advancedcustomfields.com/resources/tutorials/creating-a-front-end-form/)
********************************************************
<?php
/**
* Template Name: Form Page
*/
acf_form_head();
get_header(); ?>
<div id=”primary”>
<div id=”content” role=”main”>
<?php the_post(); ?>
<?php acf_form( $options ); ?>
</div><!– #content –>
</div><!– #primary –>
Finally in a page I tried a php code -for posting(that worked with ordinary post fields but not custom fields)I’dlike a consistent solution.
********************************************************
<?php
/*
Template Name: Post publish form
*/
?>
<?php
if( ‘POST’ == $_SERVER[‘REQUEST_METHOD’] && !empty( $_POST[‘action’] ) && $_POST[‘action’] == “new_post”) {
// Do some minor form validation to make sure there is content
if (isset ($_POST[‘title’])) {
$title = $_POST[‘title’];
} else {
echo ‘Προσθέστε Τίτλο πριν την ανάρτηση’;
}
if (isset ($_POST[‘description’])) {
$description = $_POST[‘description’];
} else {
echo ‘Προσθέστε Περιεχόμενο της ανάρτησης’;
}
$tags = $_POST[‘post_tags’];
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
‘post_title’ => $title,
‘post_content’ => $description,
‘post_category’ => array($_POST[‘cat’]), // Usable for custom taxonomies too
‘tags_input’ => array($tags),
‘post_status’ => ‘publish’, // Choose: publish, preview, future, draft, etc.
‘post_type’ => ‘post’, //’post’,page’ or use a custom post type if you want to
‘_product’=>$fproduct
);
//SAVE THE POST
$pid = wp_insert_post($new_post);
//KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
wp_set_post_tags($pid, $_POST[‘post_tags’]);
//REDIRECT TO THE NEW POST ON SAVE
//wp_redirect( ‘/publikation/’ );
//ADD OUR CUSTOM FIELDS
add_post_meta($fproduct, ‘_product’, true);
//INSERT OUR MEDIA ATTACHMENTS
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
// $newupload returns the attachment id of the file that
// was just uploaded. Do whatever you want with that now.
}
} // END THE IF STATEMENT FOR FILES
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
//POST THE POST YO
do_action(‘wp_insert_post’, ‘wp_insert_post’);
?>
<div id=”content” role=”main”>
<div class=”form-content”>
<!– ΦΟΡΜΑ ΑΝΑΡΤΗΣΗΣ ΠΡΟΒΛΗΜΑΤΑ –>
<div class=”wpcf7″>
<form id=”new_post” name=”new_post” method=”post” action=”” class=”wpcf7-form” enctype=”multipart/form-data”>
<!– post name –>
<fieldset name=”name”>
<label for=”title”>Τίτλος</label>
<input type=”text” id=”title” value=”” tabindex=”5″ name=”title” />
</fieldset>
<!– post Category –>
<fieldset class=”category”>
<label for=”cat”>Κατηγορία</label>
<?php wp_dropdown_categories( ‘tab_index=10&taxonomy=category&hide_empty=0’ ); ?>
</fieldset>
<!– post Content –>
<fieldset class=”content”>
<label for=”description”></label>
<textarea id=”description” tabindex=”15″ name=”description” cols=”80″ rows=”10″></textarea>
</fieldset>
<fieldset class=”_product”>
<label for=”description”>Προιόν</label>
<input type=”text” id=”fproduct” value=”” tabindex=”5″ name=”fproduct” />
</fieldset>
<!– post tags –>
<fieldset class=”tags”>
<label for=”post_tags”>Tags</label>
<input type=”text” value=”” tabindex=”35″ name=”post_tags” id=”post_tags” />
</fieldset>
<fieldset class=”submit”>
<input type=”submit” value=”Ανάρτησέ το” tabindex=”40″ id=”submit” name=”submit” />
</fieldset>
<input type=”hidden” name=”action” value=”new_post” />
<?php wp_nonce_field( ‘new-post’ ); ?>
</form>
</div> <!– END WPCF7 –>
<!– END OF FORM –>
</div><!– .entry-content –>
</div><!– #post-## –>
**************************************************************************
that code is working but I want a consistent solution both in terms of security(moderation) and also GUI-integration and getting initialized with ie select values for entry and a datepicker etc .Remember I have not created a custom post-type and altered standard post for different roles.How can I create a form including different fields for different roles/rules?
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.