function insert_links_to_keywords_and_dots($post_ID, $post, $update) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; if (wp_is_post_revision($post_ID)) return; if ($post->post_type !== 'post') return; remove_action('save_post', 'insert_links_to_keywords_and_dots', 10); $content = $post->post_content; // کلمات کلیدی $keyword_links = [ 'اینستاگرام' => 'https://sorenad.com/category/instagram-tutorial/', 'تلگرام' => 'https://sorenad.com/category/telegram-tutorial/', 'تبلیغات' => 'https://sorenad.com/category/ads/', 'مشتریان' => 'https://sorenad.com/category/customer-experience/', 'مارکتینگ' => 'https://sorenad.com/category/digital-marketing/', 'کسب و کار' => 'https://sorenad.com/category/business/' ]; // شمارنده کل لینک‌ها $total_links_added = 0; $max_links = 6; // استخراج تمام پاراگراف‌ها $dom = new DOMDocument(); libxml_use_internal_errors(true); $dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); $paragraphs = $xpath->query('//p'); // آرایه‌ای برای ذخیره نقاط (برای لینک‌دهی تصادفی به نقاط) $all_dots = []; // مرحله ۱: جمع‌آوری تمام نقاط موجود در متن برای انتخاب تصادفی بعداً foreach ($paragraphs as $paragraph) { $text = $paragraph->nodeValue; preg_match_all('/\.(\s|$)/u', $text, $matches, PREG_OFFSET_CAPTURE); foreach ($matches[0] as $match) { $all_dots[] = ['node' => $paragraph, 'offset' => $match[1]]; } } // مرحله ۲: اولویت‌دهی به کلمات کلیدی (تا رسیدن به سقف ۶ لینک) foreach ($paragraphs as $paragraph) { if ($total_links_added >= $max_links) break; foreach ($keyword_links as $keyword => $url) { if ($total_links_added >= $max_links) break; $text = $paragraph->nodeValue; // اگر کلمه وجود داشت و قبلا لینک نشده بود if (strpos($text, $keyword) !== false && strpos($paragraph->ownerDocument->saveHTML($paragraph), 'href=') === false) { // جایگزینی کلمه با لینک $replacement = '' . $keyword . ''; $new_html = str_replace($keyword, $replacement, $paragraph->ownerDocument->saveHTML($paragraph)); // جایگزینی پاراگراف قدیمی با جدید $fragment = $dom->createDocumentFragment(); $fragment->appendXML($new_html); $paragraph->parentNode->replaceChild($fragment, $paragraph); $total_links_added++; } } } // مرحله ۳: اگر هنوز جا داشتیم، به نقاط لینک بده if ($total_links_added < $max_links && !empty($all_dots)) { shuffle($all_dots); // تصادفی کردن نقاط foreach ($all_dots as $dot) { if ($total_links_added >= $max_links) break; // ... منطق لینک‌دهی به نقطه ... // (ساده‌سازی شد: اگر نقطه نیاز به تبدیل دارد اینجا انجام شود) $total_links_added++; } } $final_content = $dom->saveHTML(); wp_update_post([ 'ID' => $post_ID, 'post_content' => $final_content ]); add_action('save_post', 'insert_links_to_keywords_and_dots', 10, 3); } خلاقیت در پیامک Archives

مقالات مربوط به برچسب: خلاقیت در پیامک