Hi @kwoodall,
When trying to activate the snippet on the website are you getting any error messages?
Are the 2 sites identical? The first thing I would look at is making sure any function names used in the snippet are unique.
Temporarily I’ve put the code in functions.php where it works. But here’s the code I’m trying to make a snippet.
function wpsites_auto_set_featured_image() {
global $post;
$featured_image_exists = has_post_thumbnail($post->ID);
if (!$featured_image_exists) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
wp_reset_query();
}
}
}
}
add_action('save_post', 'wpsites_auto_set_featured_image');
Hi @kwoodall,
Thank you for sharing the code, I tried it on my end and didn’t run into any issues activating the snippet.
When you try to activate the snippet, what is the error message that you are receiving from the plugin?
Can you please also try activating it from the list of snippets using the toggle there instead of from the edit screen?
I tried activating it both ways. And in both cases, it fails to activate and there’s no error message.
Hi @kwoodall,
Do you have other WPCode snippets active that might be interfering?
If you can share private access to the site with us please reach out via the form at https://wpcode.com/contact and we’ll gladly take a look to figure out what is preventing the snippet from being activated.