wordpress.org – Wyodrębnianie i zastępowanie treści postów HTML za pomocą PHP
Pracuję nad aktualizacją/zastąpieniem niektórych danych w postach na blogu WordPress (ponad 1000) z 2 kategorii, z których muszę wyodrębnić określoną treść HTML z dwóch tagów p i zastąpić jeden z tagów p nową treścią. Próbuję kierować reklamy na określone elementy w kodzie HTML, takie jak link/obraz i cena/obniżona cena.
Próbuję wydobyć z tego elementu rabat i pierwotną cenę.
<p><span style="font-weight:bold;font-style: italic"><a target="_blank" href="
" rel="nofollow sponsored noopener">117 EUR</a></span> instead of 296 EUR</p>
Próbuję także wyodrębnić adres URL i link do obrazu z tego elementu i zastąpić go nową strukturą HTML.
<p><a style="font-size: 26px;text-decoration: none" target="_blank" href="
" rel="nofollow sponsored noopener">Go to: <img decoding="async" width="130" style="border-radius:20px" src="wp-content/uploads/2023/07/image.png"></a> </p>
Nowa struktura zamiany drugiego elementu HTML to
<p>
<a href="' . site_url() . '/?sn=' . $encoded . '" class="product-link_wrap" target="_blank" rel="nofollow sponsored">
<div class="button-block">
<div class="button-image">
<img src="wp-content/uploads/2023/12/image.png" alt="Product Image" class="webpexpress-processed">
</div>
<div class="product-title">
<span class="product-name">' . $blog_post_titles . '</span>
</div>
<div class="prices-container">
<span class="original-price">' . $original_price . ' EUR</span>
<span class="discount-price">' . $discount_price . ' EUR</span>
</div>
<div class="button-container">
<button class="product-button"><span>Go to </span><svg xmlns=" enable-background="new 0 0 24 24" viewBox="0 0 24 24"><path d="M15.5,11.3L9.9,5.6c-0.4-0.4-1-0.4-1.4,0s-0.4,1,0,1.4l4.9,4.9l-4.9,4.9c-0.2,0.2-0.3,0.4-0.3,0.7c0,0.6,0.4,1,1,1c0.3,0,0.5-0.1,0.7-0.3l5.7-5.7c0,0,0,0,0,0C15.9,12.3,15.9,11.7,15.5,11.3z"/></svg></button>
</div>
</div>
</a>
</p>
To jest moja próba osiągnięcia pożądanego rezultatu, jednak z jakiegoś powodu pierwsze 6, 7, a następnie inne losowe posty nie wydają się skutecznie lub wcale zastępować dane, a także niektóre dopasowania cen nie działają, i wygeneruj pusty kontener cen, zwłaszcza jeśli ceny zawierają 333 lub 4444 znaki. Treść jest taka sama w każdym poście, z wyjątkiem cen… Próbowałem z datą i zapytaniem podatkowym i bez nich, ale wynik był taki sam. Nie jestem zbyt dobry we wzorach wyrażeń regularnych, nie jestem pewien, czy to lub co jest przyczyną takiego zachowania.
$args = [
'post_type' => 'post',
'posts_per_page' => $limit,
'offset' => $offset,
'date_query' => [['after' => '1 month ago']],
'tax_query' => [[
'relation' => 'OR',
[ 'taxonomy' => 'category', 'field' => 'slug', 'terms' => ['category-1', 'category-2']],
]],
];
$query = new WP_Query($args);
if ($query->have_posts()) {
$processed_posts = 0;
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$post_content = get_post_field('post_content', $post_id);
$urlPattern = '/href="([^"]+)"/';
$imageSrcPattern = '/src="([^"]+)"/';
if (preg_match('/<a target="_blank" href="([^"]+)" rel="nofollow sponsored noopener">([\d,]+) EUR<\/a><\/span> instead of ([\d,]+) EUR<\/p>/si', $post_content, $price_matches)) {
$discountPrice = isset($price_matches[2]) ? str_replace(',', '', $price_matches[2]) : '';
$originalPrice = isset($price_matches[3]) ? str_replace(',', '', $price_matches[3]) : '';
error_log("Discount Price: " . $discountPrice . ", Original Price: " . $originalPrice);
}
preg_match($urlPattern, $post_content, $urlMatches);
$productUrl = isset($urlMatches[1]) ? $urlMatches[1] : '';
preg_match($imageSrcPattern, $post_content, $imageSrcMatches);
$imageSrc = isset($imageSrcMatches[1]) ? $imageSrcMatches[1] : '';
$new_structure = "<a href=\"{$productUrl}\" class=\"product-link_wrap\" target=\"_blank\">
<div class=\"button-block\">
<div class=\"button-image\">
<img src=\"{$imageSrc}\" alt=\"Product Image\" class=\"webpexpress-processed\">
</div>
<div class=\"product-title\">
<span class=\"product-name\">" . esc_html(get_the_title($post_id)) . "</span>
</div>
<div class=\"prices-container\">
<span class=\"original-price\">{$originalPrice}</span>
<span class=\"discount-price\">{$discountPrice} SEK</span>
</div>
<div class=\"button-container\">
<button class=\"product-button\"><span>Go to Product</span><svg xmlns=\" enable-background=\"new 0 0 24 24\" viewBox=\"0 0 24 24\"><path d=\"M15.5,11.3L9.9,5.6c-0.4-0.4-1-0.4-1.4,0s-0.4,1,0,1.4l4.9,4.9l-4.9,4.9c-0.2,0.2-0.3,0.4-0.3,0.7c0,0.6,0.4,1,1,1c0.3,0,0.5-0.1,0.7-0.3l5.7-5.7c0,0,0,0,0,0C15.9,12.3,15.9,11.7,15.5,11.3z\"/></svg></button>
</div>
</div>
</a>";
$pattern = '/<a style="font-size: 26px; text-decoration: none;" href="([^"]+)" target="_blank" rel="nofollow sponsored noopener">Go to: <img style="border-radius: 20px;" src="([^"]+)" width="130" \/><\/a>/';
$updated_content = preg_replace($pattern, $new_structure, $post_content);
wp_update_post([
'ID' => $post_id,
'post_content' => $updated_content,
]);
wp_reset_postdata();
}
}