Support Forum
Support › MB Custom Post Type › PHP: BricksBuilder Front-End Form & Meta Box CPT – Image Upload ChallengeResolved
How can I successfully integrate the data from a BricksBuilder front-end form into a CPT created with Meta Box, ensuring image uploads work seamlessly without requiring user registration for submissions?
Code i use currently to get other fields data and creating the post:
`
function add_story( $form ) {
$fields = $form->get_fields();
$name = $fields['form-field-1'];
$email = $fields['form-field-2'];
$story = $fields['form-field-3'];
$new_post['post_type'] = 'story';
$new_post['post_status'] = 'draft';
$new_post['post_title'] = $name;
$new_post['post_content'] = $story;
$new_post['meta_input']['email'] = $email;
$post_id = wp_insert_post( $new_post );
}
add_action( 'bricks/form/custom_action', 'add_story', 10, 1 );
`
I have tried to extend the code but without success, also tried using the option to save the images localy first with the form-option also without any success.
`
function add_story( $form ) {
$fields = $form->get_fields();
$name = $fields['form-field-1'];
$email = $fields['form-field-2'];
$story = $fields['form-field-3'];
$uploaded_images = $form->get_uploaded_files();
$new_post = array(
'post_type' => 'story',
'post_status' => 'publish',
'post_title' => $name,
'post_content' => $story,
'meta_input' => array(
'email' => $email,
),
);
$post_id = wp_insert_post( $new_post );
foreach ( $uploaded_images['form-field-4'] as $image ) {
$image_url = $image['url'];
$image_name = basename( $image_url );
$image_path = WP_CONTENT_DIR . '/uploads/' . $image_name;
$image_data = file_get_contents( $image_url );
if ( $image_data !== false ) {
file_put_contents( $image_path, $image_data );
$image_attachment = array(
'post_mime_type' => $image['type'],
'post_title' => sanitize_file_name( $image_name ),
'post_content' => '',
'post_status' => 'inherit',
'post_parent' => $post_id,
);
$attach_id = wp_insert_attachment( $image_attachment, $image_path, $post_id );
if ( ! is_wp_error( $attach_id ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $image_path );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
} else {
error_log( 'Error inserting: ' . $attach_id->get_error_message() );
}
} else {
error_log( 'Error downloading: ' . $image_url );
}
}
}
add_action( 'bricks/form/custom_action', 'add_story', 10, 1 );
`
How can I successfully integrate the data from a BricksBuilder front-end form into a CPT created with Meta Box, ensuring image uploads work seamlessly without requiring user registration for submissions?
Code i use currently to get other fields data and creating the post:
function add_story( $form ) {
$fields = $form->get_fields();
$name = $fields['form-field-1'];
$email = $fields['form-field-2'];
$story = $fields['form-field-3'];
$new_post['post_type'] = 'story';
$new_post['post_status'] = 'draft';
$new_post['post_title'] = $name;
$new_post['post_content'] = $story;
$new_post['meta_input']['email'] = $email;
$post_id = wp_insert_post( $new_post );
}
add_action( 'bricks/form/custom_action', 'add_story', 10, 1 );
I have tried to extend the code but without success, also tried using the option to save the images localy first with the form-option also without any success.
function add_story( $form ) {
$fields = $form->get_fields();
$name = $fields['form-field-1'];
$email = $fields['form-field-2'];
$story = $fields['form-field-3'];
$uploaded_images = $form->get_uploaded_files();
$new_post = array(
'post_type' => 'story',
'post_status' => 'publish',
'post_title' => $name,
'post_content' => $story,
'meta_input' => array(
'email' => $email,
),
);
$post_id = wp_insert_post( $new_post );
foreach ( $uploaded_images['form-field-4'] as $image ) {
$image_url = $image['url'];
$image_name = basename( $image_url );
$image_path = WP_CONTENT_DIR . '/uploads/' . $image_name;
$image_data = file_get_contents( $image_url );
if ( $image_data !== false ) {
file_put_contents( $image_path, $image_data );
$image_attachment = array(
'post_mime_type' => $image['type'],
'post_title' => sanitize_file_name( $image_name ),
'post_content' => '',
'post_status' => 'inherit',
'post_parent' => $post_id,
);
$attach_id = wp_insert_attachment( $image_attachment, $image_path, $post_id );
if ( ! is_wp_error( $attach_id ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $image_path );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
} else {
error_log( 'Error inserting: ' . $attach_id->get_error_message() );
}
} else {
error_log( 'Error downloading: ' . $image_url );
}
}
}
add_action( 'bricks/form/custom_action', 'add_story', 10, 1 );
Sorry didnt put the divs!
Hello,
In the case of using Bricks Builder, please reach out to them if you have any issues with installation, configuration, compatibility, or usage.
Refer to our support policy https://metabox.io/support/topic/support-policy/
Hello Peter, thanks for the feedback, i will!
All the best Omar