Home › Forums › Add-ons › Repeater Field › ReferenceError: wp is not defined – Can't upload images › Reply To: ReferenceError: wp is not defined – Can't upload images
Hi
in above solution we are not using acf form we are going to use htmlform mean create form in a wp page template with your required fileds for example if u create three fields in acf like Author Image Title Image then create similar form in html and
<form method=”post” action=”#” class=”acf-form form-horizontal ” id=”post” enctype=”multipart/form-data”>
<?php
$args = array(
‘post_id’ => ‘new’,
‘field_groups’ => array(1), //use wrong ID of your custom field Form so
form fields will not get shown`
‘form’ => false
);
acf_form($args);
?>
//file upload button
<div data-field_type=”file” data-field_key=”field_5343d9b2586f0file” data-field_name=”email” class=”field field_type-text field_key-field_5343d9b2586f0file” id=”acf-file”>
<p class=”label”><label for=”acf-field-file”>Image</label></p>
<div class=”acf-input-wrap”>
<input type=”file” id=”my_image_upload” name=”my_image_upload”>
</div>
<input id=”submit_my_image_upload” name=”submit_my_image_upload” type=”submit” value=”Submit” />
The above form will be on u r wordpress page
Now copy the following function to your themes function.php
//img upload//
function kv_handle_attachment($file_handler,$post_id,$set_thu=false) {
<code>// check to make sure its a successful upload</code>
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . “wp-admin” . ‘/includes/image.php’);
require_once(ABSPATH . “wp-admin” . ‘/includes/file.php’);
require_once(ABSPATH . “wp-admin” . ‘/includes/media.php’);
$attach_id = media_handle_upload( $file_handler, $post_id );
// If you want to set a featured image from your uploads.
if ($set_thu) set_post_thumbnail($post_id, $attach_id);
return $attach_id;
}
//New File Upload
add_filter(‘acf/pre_save_post’ , ‘my_pre_save_post’ );
function my_pre_save_post( $post_id ) {
// check if this is to be a new post`
if( $post_id != ‘new’ ) {
return $post_id;
}
$field = $_POST['fields'];
$post_title = $_POST['fullname'];
$post_content = $field['edit_test2'];
// Create a new post
require_once(ABSPATH . “wp-admin” . ‘/includes/image.php’);
require_once(ABSPATH . “wp-admin” . ‘/includes/file.php’);
require_once(ABSPATH . “wp-admin” . ‘/includes/media.php’);
$attachment_id = media_handle_upload( ‘my_image_upload’, $post_id );
$post = array(
‘post_status’ => ‘draft’ ,
‘post_title’ => $post_title,
‘post_content’ => $post_content,
‘post_type’ => ‘page’
);
$newpost_id=wp_insert_post($post);
if($newpost_id!=0)
{
add_post_meta($newpost_id, ‘picture’, $attachment_id );
// here u can add more fields which are present on u r form
}
}`
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!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc
— Advanced Custom Fields (@wp_acf) May 9, 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.