add_filter( 'the_content', 'epicpixel_smart_product_injector' );
function epicpixel_smart_product_injector( $content ) {
// 1. بررسی‌های اولیه و امنیتی
if ( ! is_singular( array( 'post', 'product' ) ) || ! class_exists( 'WooCommerce' ) ) {
return $content;
}
// --- تنظیمات: بعد از هر چند کاراکتر تبلیغ نشان دهد؟ ---
$char_threshold = 1500;
// 2. محاسبه هوشمند تعداد محصولات مورد نیاز
$content_length = mb_strlen( strip_tags( $content ) );
$products_needed = floor( $content_length / $char_threshold );
// تضمین نمایش حداقل یک محصول حتی در متون کوتاه
if ( $products_needed &lt; 1 ) { $products_needed = 1; } // 3. کوئری محصولات (موجود + تصادفی + مرتبط) $args = array( 'post_type' =&gt; 'product',
'posts_per_page' =&gt; $products_needed,
'orderby'        =&gt; 'rand',
'post_status'    =&gt; 'publish',
'meta_query'     =&gt; array(
array(
'key'     =&gt; '_stock_status',
'value'   =&gt; 'instock',
),
),
);
// شرط‌های یافتن محصول مرتبط (Contextual)
if ( is_product() ) {
global $post;
$terms = get_the_terms( $post-&gt;ID, 'product_cat' );
$cat_ids = array();
if ( $terms ) {
foreach ( $terms as $term ) {
$cat_ids[] = $term-&gt;term_id;
}
}
$args['tax_query'] = array(
array(
'taxonomy' =&gt; 'product_cat',
'field'    =&gt; 'term_id',
'terms'    =&gt; $cat_ids,
),
);
$args['post__not_in'] = array( $post-&gt;ID );
} elseif ( is_singular( 'post' ) ) {
$categories = get_the_category();
if ( ! empty( $categories ) ) {
$search_keyword = $categories[0]-&gt;name;
$args['s'] = $search_keyword;
}
}
$product_query = new WP_Query( $args );
if ( ! $product_query-&gt;have_posts() ) {
wp_reset_postdata();
return $content;
}
$found_products = $product_query-&gt;posts;
$product_index = 0;
$products_count = count( $found_products );
// 4. تزریق محتوا بین پاراگراف‌ها
// نکته: اینجا تگ پی را به صورت ایمن نوشتیم تا قالب بهم نریزد
$paragraphs = explode( '&lt;/p&gt;', $content );
$new_content = '';
$chars_since_last_ad = 0;
$ad_shown_counter = 0;
foreach ( $paragraphs as $index =&gt; $paragraph ) {
if ( empty( trim( $paragraph ) ) ) continue;
$new_content .= $paragraph . '&lt;/p&gt;';
$chars_since_last_ad += mb_strlen( strip_tags( $paragraph ) );
if ( $chars_since_last_ad &gt; $char_threshold &amp;&amp; $product_index &lt; $products_count ) { $product_html = epicpixel_get_product_html( $found_products[ $product_index ] ); $new_content .= $product_html; $chars_since_last_ad = 0; $product_index++; $ad_shown_counter++; } } // 5. شرط حداقل یکبار نمایش if ( $ad_shown_counter == 0 &amp;&amp; isset( $found_products[0] ) ) { $new_content .= epicpixel_get_product_html( $found_products[0] ); } wp_reset_postdata(); return $new_content; } // تابع کمکی برای ساخت ظاهر HTML function epicpixel_get_product_html( $post_object ) { $product = wc_get_product( $post_object-&gt;ID );
if ( ! $product ) return '';
$html  = '&lt;div class="epicpixel-inline-product"&gt;';
$html .= '  &lt;div class="g-prod-thumb"&gt;' . $product-&gt;get_image( 'thumbnail' ) . '&lt;/div&gt;';
$html .= '  &lt;div class="g-prod-info"&gt;';
$html .= '      &lt;h4 class="g-prod-title"&gt;' . $product-&gt;get_name() . '&lt;/h4&gt;';
$html .= '      &lt;a href="' . $product-&gt;get_permalink() . '" class="g-prod-btn"&gt;مشاهده و دانلود&lt;/a&gt;';
$html .= '  &lt;/div&gt;';
$html .= '&lt;/div&gt;';
return $html;
}