Hallo
I have a site where everybody can upload projects. Now after upgrading the ACF Plugin, I read that I don’t need the function.php
But I need a solution to upload new projects with special rules. I will show you a snapshot of my code: function.php
` function my_pre_save_post( $post_id ) {
if( $post_id != ‘new’ )
{
return $post_id;
}
$anfangsDate = $_POST[‘fields’][‘field_1’];
$schlussDate = $_POST[‘fields’][‘field_2’];
if ($anfangsDate<$schlussDate) {update_field(‘field_1’,”);}
$title = $_POST[‘fields’][‘field_3’];
$content = $_POST[‘fields’][‘field_4’];
$timestamp = $_POST[‘fields’][‘field_5’];
foreach ( $_POST[‘fields’][‘field_6’] as $value )
{
$cat[] = $value;
}
if($timestamp == “”) {
$timestamp = time();
$date = date(‘Ymd’,$timestamp);
$post = array(
‘post_status’ => ‘draft’,
‘post_type’ => ‘projects-all’,
‘post_category’ => array( 10000 , $cat[0] , $cat[1] ),
‘post_title’ => $title,
‘post_content’ => $content
);
} else {
$post = array(
‘post_status’ => ‘draft’,
‘post_type’ => ‘projects-all’,
‘post_category’ => array( 10000 , $cat[0] , $cat[1] ),
‘post_title’ => $title,
‘post_content’ => $content
);
}
$post_id = wp_insert_post( $post );
return $post_id;
}
`
Please help!
ACF 5 uses the ‘acf’ in place of ‘fields’. you need to change each $_POST['fields'][...
to $_POST['acf'][...
Yes, that I do first, but then the frontpage will be empty.
Now I will be working step by step.
And T found this mistake
if( $post_id != 'new' )
-> if( $post_id != 'new_post' )
I’m not sure I understand you’re problem. I was under the impression that acf_form works the same in ACF4 and 5. If your still having problems can you explain in more detail so that I can understand what they are?
First I change:
$_POST['fields'][...
to $_POST['acf'][...
Second:
if( $post_id != 'new' )
->if( $post_id != 'new_post' )
Now I have only two problem.
First: When I create a new project, I will look for the start and end of a project. In the past I got a feedback when the end date was before the start date. Now nothing happen.
Under “Projekt einreichen” you will find what I mean (this is ACF V4, but I am testing on V5 for the future)
I control the div #acf-sdate
and #acf-adate
with a javascript:
jQuery(document).ready(function($) {
//Abschluss
$('#acf-adate > div input').change(
function () {
var datumStart = $('#acf-sdate > div > input').val();
var datumAbschluss = $('#acf-adate > div > input').val();
if (datumStart){
if (datumStart > datumAbschluss) {
$('#acf-adate > div > input').val('');
$('#acf-adate').addClass('error');
} else if ($('#acf-adate').hasClass('error')) {
$('#acf-adate').removeClass('error');
}
}
}
);
//Start
$('#acf-sdate > div input').change(
function () {
var datumStart = $('#acf-sdate > div > input').val();
var datumAbschluss = $('#acf-adate > div > input').val();
if (datumAbschluss){
if (datumStart > datumAbschluss) {
$('#acf-sdate > div > input').val('');
$('#acf-sdate').addClass('error');
} else if ($('#acf-sdate').hasClass('error')) {
$('#acf-sdate').removeClass('error');
}
}
}
);
}
);
Second: Everytime I push a project, I push two posts. One in projects-all and one in posts:
<?php acf_form(array(
'post_id'=> 'new_post',
'field_groups' => array ( 1098 ),
'return' => home_url('thank-you'),
'submit_value'=> 'Projekt einreichen'
)); ?>
First I’ll cover the second one about creating two posts. Change the value in your acf_form() args from 'post_id'=> 'new_post',
to 'post_id'=> 'new_project',
. The value new_post
triggers ACF to create a new “Post”, then in your pre save post filter change if( $post_id != 'new' )
to if( $post_id != 'new_project' )
Second, the first question about the jQuery. You need to change the ID values that you’re targeting and you’ll need to look at the HTML output of the ACF form for all the details. But basically your selectors should look something like $([data-key="field_0987654321"] input)
where the data-key value is the field key of the field you want to target.
You must be logged in to reply to this topic.
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.