Als er verzocht word om dit naar het nederlands te vertelen graag z.s.m. melden en gooi ik het om naar nederlands.(en sorry voor mijn engels
)
I'm trying to save a bunch of images to some custom fields. Everything is working except the images.
I'm struggling with this for quite a bit, and i saw a pattern in the failure.
First save: Images get saved in a sort of attachment ID and is shown on the front end (for exmaple "807".)
Second save: Image id or link dissapears from the backend, front end shows a.... image(what!?)..
Third save: Everything is gone! (except all the other fields)!
I really need some help on this one..
Here is the code for a static custom field table.
(i have 3 tables. 1 if you make a new post, 1 you can edit and one you can add inside the current post)
<!-- FRONT - EDITABLE -->
<tr>
<td><input type="text" class="widefat" name="date[]"
value="<?php if ($field['date'] != '') echo esc_attr($field['date']); ?>"/></td>
<td><input type="text" class="widefat" name="name[]"
value="<?php if ($field['name'] != '') echo esc_attr($field['name']); ?>"/></td>
<td><input type="text" class="widefat" name="text[]"
value="<?php if ($field['text'] != '') echo esc_attr($field['text']); ?>"/></td>
<td>
<?php
// $image = get_template_directory_uri().'/images/image.png';
$image = wp_get_attachment_image_src($field['image'], 'medium'); $image = $image[0];
echo $image;
?>
<input type="hidden" class="custom_upload_image" name="image[]"
value="<img src=' <?php echo $image ?> ' class='custom_preview_image' alt='' /><br />"/>
<input class="custom_upload_image_button button" type="button" value="Choose Image"/>
<small><a href="#" class="custom_clear_image_button">Remove Image</a></small>
</td>
<td><a class="button remove-row" href="#">Remove</a></td>
</tr>
And here is the saving part:
add_action('save_post', 'hhs_repeatable_meta_box_save');
function hhs_repeatable_meta_box_save($post_id)
{
if (!isset($_POST['hhs_repeatable_meta_box_nonce']) ||
!wp_verify_nonce($_POST['hhs_repeatable_meta_box_nonce'], 'hhs_repeatable_meta_box_nonce')
)
return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (!current_user_can('edit_post', $post_id))
return;
$old = get_post_meta($post_id, 'repeatable_fields');
$new = array();
$dates = $_POST['date'];
$names = $_POST['name'];
$texts = $_POST['text'];
$images = $_POST['image'];
$count = count($names);
for ($i = 0; $i < $count; $i++) {
if (!empty($names[$i])) :
$new[$i]['name'] = $names[$i];
if (!empty($dates[$i])) {
$new[$i]['date'] = $dates[$i];
};
if (!empty($texts[$i])) {
$new[$i]['text'] = $texts[$i];
};
if (!empty($images[$i])) {
$new[$i]['image'] = $images[$i];
};
endif;
if (!empty($new) && $new != $old)
update_post_meta($post_id, 'repeatable_fields', $new);
elseif (empty($new) && $old)
delete_post_meta($post_id, 'repeatable_fields', $old);
}
}
I just want to have a image uploaded in a custom field : http://i.imgur.com/GtR9XuH.png and then it needs to be saved to the backend AND front end so it shows on the back end and front end!
This is an example of first time save :
Array ( [name] => 3123 [date] => 12 [text] => 123 [image] => 807 )
I really hope someone can help me out here!
I'm trying to save a bunch of images to some custom fields. Everything is working except the images.
I'm struggling with this for quite a bit, and i saw a pattern in the failure.
First save: Images get saved in a sort of attachment ID and is shown on the front end (for exmaple "807".)
Second save: Image id or link dissapears from the backend, front end shows a.... image(what!?)..
Third save: Everything is gone! (except all the other fields)!
I really need some help on this one..
Here is the code for a static custom field table.
(i have 3 tables. 1 if you make a new post, 1 you can edit and one you can add inside the current post)
<!-- FRONT - EDITABLE -->
<tr>
<td><input type="text" class="widefat" name="date[]"
value="<?php if ($field['date'] != '') echo esc_attr($field['date']); ?>"/></td>
<td><input type="text" class="widefat" name="name[]"
value="<?php if ($field['name'] != '') echo esc_attr($field['name']); ?>"/></td>
<td><input type="text" class="widefat" name="text[]"
value="<?php if ($field['text'] != '') echo esc_attr($field['text']); ?>"/></td>
<td>
<?php
// $image = get_template_directory_uri().'/images/image.png';
$image = wp_get_attachment_image_src($field['image'], 'medium'); $image = $image[0];
echo $image;
?>
<input type="hidden" class="custom_upload_image" name="image[]"
value="<img src=' <?php echo $image ?> ' class='custom_preview_image' alt='' /><br />"/>
<input class="custom_upload_image_button button" type="button" value="Choose Image"/>
<small><a href="#" class="custom_clear_image_button">Remove Image</a></small>
</td>
<td><a class="button remove-row" href="#">Remove</a></td>
</tr>
And here is the saving part:
add_action('save_post', 'hhs_repeatable_meta_box_save');
function hhs_repeatable_meta_box_save($post_id)
{
if (!isset($_POST['hhs_repeatable_meta_box_nonce']) ||
!wp_verify_nonce($_POST['hhs_repeatable_meta_box_nonce'], 'hhs_repeatable_meta_box_nonce')
)
return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (!current_user_can('edit_post', $post_id))
return;
$old = get_post_meta($post_id, 'repeatable_fields');
$new = array();
$dates = $_POST['date'];
$names = $_POST['name'];
$texts = $_POST['text'];
$images = $_POST['image'];
$count = count($names);
for ($i = 0; $i < $count; $i++) {
if (!empty($names[$i])) :
$new[$i]['name'] = $names[$i];
if (!empty($dates[$i])) {
$new[$i]['date'] = $dates[$i];
};
if (!empty($texts[$i])) {
$new[$i]['text'] = $texts[$i];
};
if (!empty($images[$i])) {
$new[$i]['image'] = $images[$i];
};
endif;
if (!empty($new) && $new != $old)
update_post_meta($post_id, 'repeatable_fields', $new);
elseif (empty($new) && $old)
delete_post_meta($post_id, 'repeatable_fields', $old);
}
}
I just want to have a image uploaded in a custom field : http://i.imgur.com/GtR9XuH.png and then it needs to be saved to the backend AND front end so it shows on the back end and front end!
This is an example of first time save :
Array ( [name] => 3123 [date] => 12 [text] => 123 [image] => 807 )
I really hope someone can help me out here!