Snippet not appearing after creation
-
Hello,
After creating and saving a php snippet, it doesn’t show anywhere. Maybe the reason is that this snippet also contains CSS? Or something else? The snippet is regularly working in another snippets plugin.
Thanks
-
Hi mariosem.
To help figure out what’s happening, could you share a bit more detail? Can you share the snippet code along with what you expect it to do? This will help us try to replicate the issue.
Also, are you able to save and see other snippets, or is the problem only with this specific one? If you try saving the snippet without activating it, does anything different happen?
If you’d prefer to share details about your setup privately, you can reach us using the form at https://wpcode.com/contact.
Hello,
Yes, I can save and see any other snippets. The snippet show 4 related posts, using a weighting of 65% tags and 35% categories, from the last 12 months, with a small thumbnail (120px). It is made by AI, as you can imagine. It works perfectly in another site of mine. As an aside, I just asked the AI to rewrite the code, and the new one works also with your plugin.
This is the code of the one that cannot be saved:
/**
* Related Posts Box
* Mostra 4 post correlati basati su tag (65%) e categorie (35%)
*/
// Aggiungi il box dei related posts dopo il contenuto
add_filter('the_content', 'phastidio_add_related_posts');
function phastidio_add_related_posts($content) {
// Esegui solo sui post singoli, non su pagine o archivi
if (!is_single() || !is_main_query()) {
return $content;
}
global $post;
$related_posts = phastidio_get_related_posts($post->ID, 4);
if (empty($related_posts)) {
return $content;
}
$related_html = '<div class="phastidio-related-posts">';
$related_html .= '<h3 class="related-posts-title">Potrebbero interessarvi</h3>';
$related_html .= '<div class="related-posts-grid">';
foreach ($related_posts as $related_post) {
$thumbnail = get_the_post_thumbnail($related_post->ID, 'medium', array('class' => 'related-post-thumb'));
if (!$thumbnail) {
$thumbnail = '<div class="related-post-no-thumb"><span>📄</span></div>';
}
$related_html .= '<div class="related-post-item">';
$related_html .= '<a href="' . get_permalink($related_post->ID) . '" class="related-post-link">';
$related_html .= '<div class="related-post-image">' . $thumbnail . '</div>';
$related_html .= '<h4 class="related-post-title">' . esc_html($related_post->post_title) . '</h4>';
$related_html .= '<span class="related-post-date">' . get_the_date('j F Y', $related_post->ID) . '</span>';
$related_html .= '</a>';
$related_html .= '</div>';
}
$related_html .= '</div></div>';
return $content . $related_html;
}
function phastidio_get_related_posts($post_id, $limit = 4) {
// Ottieni tag e categorie del post corrente
$tags = wp_get_post_tags($post_id);
$categories = wp_get_post_categories($post_id);
if (empty($tags) && empty($categories)) {
return array();
}
$tag_ids = array();
$category_ids = array();
foreach ($tags as $tag) {
$tag_ids[] = $tag->term_id;
}
if (!empty($categories)) {
$category_ids = $categories;
}
// Query per post degli ultimi 12 mesi
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 50,
'post__not_in' => array($post_id),
'date_query' => array(
array(
'after' => '12 months ago'
)
),
'orderby' => 'date',
'order' => 'DESC'
);
// Aggiungi filtro per tag o categorie se presenti
if (!empty($tag_ids) || !empty($category_ids)) {
$tax_query = array('relation' => 'OR');
if (!empty($tag_ids)) {
$tax_query[] = array(
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => $tag_ids
);
}
if (!empty($category_ids)) {
$tax_query[] = array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $category_ids
);
}
$args['tax_query'] = $tax_query;
}
$related_query = new WP_Query($args);
$scored_posts = array();
if ($related_query->have_posts()) {
while ($related_query->have_posts()) {
$related_query->the_post();
$related_id = get_the_ID();
// Calcola il punteggio
$score = 0;
// Punteggio tag (65%)
if (!empty($tag_ids)) {
$post_tags = wp_get_post_tags($related_id);
$matching_tags = 0;
foreach ($post_tags as $post_tag) {
if (in_array($post_tag->term_id, $tag_ids)) {
$matching_tags++;
}
}
$tag_score = ($matching_tags / count($tag_ids)) * 0.65;
$score += $tag_score;
}
// Punteggio categorie (35%)
if (!empty($category_ids)) {
$post_categories = wp_get_post_categories($related_id);
$matching_cats = 0;
foreach ($post_categories as $post_cat) {
if (in_array($post_cat, $category_ids)) {
$matching_cats++;
}
}
$cat_score = ($matching_cats / count($category_ids)) * 0.35;
$score += $cat_score;
}
if ($score > 0) {
$scored_posts[] = array(
'post' => get_post($related_id),
'score' => $score
);
}
}
wp_reset_postdata();
}
// Ordina per punteggio decrescente
if (!empty($scored_posts)) {
usort($scored_posts, function($a, $b) {
return $b['score'] <=> $a['score'];
});
}
// Restituisci i top N post
$result = array();
$count = 0;
foreach ($scored_posts as $scored_post) {
if ($count >= $limit) break;
$result[] = $scored_post['post'];
$count++;
}
return $result;
}
// Aggiungi gli stili CSS
add_action('wp_head', 'phastidio_related_posts_styles');
function phastidio_related_posts_styles() {
if (!is_single()) return;
?>
<style>
.phastidio-related-posts {
margin: 40px 0;
padding: 25px;
background: #f8f9fa;
border-radius: 8px;
border-left: 4px solid #0073aa;
}
.related-posts-title {
margin: 0 0 20px 0;
font-size: 22px;
font-weight: 600;
color: #333;
}
.related-posts-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 15px;
}
.related-post-item {
background: #fff;
border-radius: 6px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.related-post-item:hover {
transform: translateY(-4px);
box-shadow: 0 4px 16px rgba(0,0,0,0.15);
}
.related-post-link {
text-decoration: none;
color: inherit;
display: block;
}
.related-post-image {
position: relative;
width: 100%;
height: 120px;
overflow: hidden;
background: #e9ecef;
}
.related-post-image img {
width: 100%;
height: 100%;
object-fit: cover;
}
.related-post-no-thumb {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
font-size: 48px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #fff;
}
.related-post-title {
margin: 0;
padding: 12px 12px 8px;
font-size: 16px;
font-weight: 600;
line-height: 1.4;
color: #333;
}
.related-post-date {
display: block;
padding: 0 12px 12px;
font-size: 12px;
color: #666;
}
@media (max-width: 768px) {
.related-posts-grid {
grid-template-columns: 1fr;
}
.related-post-image {
height: 150px;
}
}
</style>
<?php
}Hi mariosem,
I tested the snippet on my end and did not run into an error.
Could you please enable debugging on your site and check the error message that appears when you try to activate this snippet? The error details will help us identify which part of the code is causing the issue and guide us toward a solution.
Hello, this is the error (site name edited):
[19-Nov-2025 06:46:55 UTC] PHP Fatal error: Cannot redeclare create_archived_post_status() (previously declared in /home2/XXXXXXX/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(663) : eval()’d code:2) in /home2/XXXXXX/public_html/wp-content/plugins/insert-headers-and-footers/includes/class-wpcode-snippet-execute.php(419) : eval()’d code on line 2
Actually, I think this is a different error, when today I activated your plugin and Code Snippets was also active. Currently, no other error is in the log. Moreover, no debug log is produced, after I changed the wp-config file, activating the debug.
Hi mariosem,
Based on the error message, it appears that one of your active snippets contains the function declaration
create_archived_post_status(), which is also declared in Code Snippets plugin.The issue is not with the snippet you sent us, but with another active snippet containing the same function. To resolve this, please deactivate the snippet containing this function
create_archived_post_status()in the Code Snippets plugin and than activate your snippet in WPCode.When moving snippets from another plugin, please make sure to deactivate them there first before activating them in WPCode to avoid conflicts.
Yes, I confirm what you wrote. But, talking about the subject of this thread, no debug log has been produced, and the snippet can neither be activated nor saved.
Hi mariosem,
Regarding your original snippet, I want to make sure I understand:
- After removing the other snippets from Code Snippets plugin and resolving the duplicate function issue, you are still unable to save or activate the snippet in WPCode.
- There are no errors shown and nothing appears in your debug log when you try to save or activate it.
Is that correct?
Yes, that’s correct.
Hi mariosem,
Thanks for the update.
Without additional details about your website setup, it’s difficult to determine the exact cause of the issue.
Could you please let me know if you still need help with this, or if the updated snippet you mentioned is now working as expected?
-
This reply was modified 4 months, 1 week ago by
markomiljanovic.
Hello,
As I wrote, the AI modified snippet is working properly.
Thanks
You must be logged in to reply to this topic.