Tell us what you are trying to do?
I have created a Post from for a custom post and would like to apply a default featured image rather than relying on my users to select an image when they enter a post.
Its not working for me if you could take a look it would be great.
My code looks like this...
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==356)
{
if (empty($_POST['_featured_image']))
{
// add it to saved post meta
add_post_meta( $post_id, "_thumbnail_id",'367' );
}
}
}
The image ID is 367 and the ID for the post form is 356 I have attached screenshot of custom code
I just tried your code and it appeared to work, but on closer inspection I realise it won't work properly because image fields are uploaded via ajax before the form itself is submitted.
I reworked your code to check whether there is already a thumbnail attached, and if not, to then add it.
<?php
add_action('cred_save_data', 'tssupp_default_thumbnail', 10, 2);
function tssupp_default_thumbnail($post_id, $form_data) {
if ($form_data['id'] == 356) {
$current_thumb = get_post_meta($post_id, '_thumbnail_id', true);
if (empty($current_thumb)) {
add_post_meta($post_id, "_thumbnail_id", '367');
}
}
}
Still same problem I'm afraid post gets added but still not adding image as posts featured image
If I manually add image via post form it does get added as expected to the post but if no image manually added the default does not get added to the post.
I have been unable to add another image of the code snippet (window does not open).
But settings are
Run mode set to Run Always
Run context WordPress admin and Ajax calls