Home › Forums › Add-ons › Repeater Field › Need repeater data on publishing (save_post only works only updating,not create) › Reply To: Need repeater data on publishing (save_post only works only updating,not create)
In case it helps, here is the code:
Few explanations on what is trying to be achieved:
• The user has a library of images that he uses to build comics. There are 2 repeater fields within one “comic” custom post type: “comic slides” (images within a comic) and “comic_bildkomposition” (layers of images within a slide that will be put into a final image)
• current_timestamp is being used as an ID for each comic that is being built.
• there are 2 repeater fields: “comic_slides” and nested in it there are the fields “comic_text” (wysiwyg field) and “comic_bildkomposition” (repeater field for the image layers within a slide)
CODE:
function lextoons_comic_builder( $ID ) {
$comic_slides = get_field(‘comic_slides’);
// $comic_excerpt = substr(get_field(‘comic_excerpt’), 0, 120);
[Author comment: this was a test in order to see if the function receives acf custom field data at all, which it does, “comic_excerpt” is a custom description field that is being put into the variable and printed just fine
$comic_timestamp = get_field (‘timestamp’);
$current_timestamp = time();
$timestamp_field_key = “field_53da164db054e”;
$comic_title = get_the_title();
//check if comic timestamp has been set yet for this post, set an ID based on timestamp if not
if ($comic_timestamp < 1 ) {
update_field( $timestamp_field_key, $current_timestamp, $ID );
$comic_timestamp = $current_timestamp;
$pathname = “”.$_SERVER[‘DOCUMENT_ROOT’].”wp-content/uploads/comics/”.$comic_timestamp.””;
//print $pathname;
mkdir($pathname);
}
//If an ID has been set before, delete all files in the folder of this comic for recreation afterwards
if ($comic_timestamp > 1 ) {
//echo “<br/>Files: <br/>”;
foreach (glob(“”.$_SERVER[‘DOCUMENT_ROOT’].”wp-content/uploads/comics/”.$comic_timestamp.”/*.*”) as $filename) {
// echo “$filename size ” . filesize($filename) . “<br/>”;
unlink($filename);
}
}
//Now, Loop through all Comic Slides and Generate a single Image out of each slides’ Layer
$slide_nr = 0;
foreach ($comic_slides as $comic_slide) {
$slide_nr++;
$slide_text = $comic_slide[‘comic_text’];
$slide_text = substr($slide_text, 3, -5);
$comic_layers =$comic_slide[‘comic_bildkomposition’];
$width = get_field (‘image_input_width’, ‘options’);
$height = get_field (‘image_input_height’, ‘options’);
$newwidth = get_field (‘image_output_width’, ‘options’);
$newheight = get_field (‘image_output_height’, ‘options’);
$image = imagecreatetruecolor($width, $height);
foreach ($comic_layers as $layer) {
unset ($layer_data);
// to make background transparent
imagealphablending($image, false);
$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparency);
imagesavealpha($image, true);
imagealphablending($image, true);
$layer_url = $layer[‘comic_layer’][‘url’];
print $layer_url;
$layer_data = imagecreatefrompng($layer_url);
imagecopy($image, $layer_data, 0, 0, 0, 0, $width, $height);
imagealphablending($image, false);
imagesavealpha($image, true);
}
$image2 = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image2, 0, 0, $white);
imagecopy($image2, $image, 0, 0, 0, 0, $width, $height);
//create a thumb file img for resizing
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $image2, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($thumb, “”.$_SERVER[‘DOCUMENT_ROOT’].”wp-content/uploads/comics/”.$comic_timestamp.”/”.$comic_title.”_”.$slide_nr.”.png”);
unset ($image);
unset ($image2);
unset ($thumb);
}
}
add_action( ‘acf/save_post’, ‘lextoons_comic_builder’, 20 );
[/CODE]
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.