• Resolved borkk85

    (@borkk85)


    I’m working on updating/replacing certain data in WordPress blog posts(1000+) from 2 categories from which I need to extract specific HTML content from two p tags and replace one of the p tags with new content. I’m trying to target specific elements within the HTML, such as link/image and price/discount price.

    I am attempting to extract the discount and original price from this element.

    <p><span style="font-weight:bold;font-style: italic"><a target="_blank" href="https://www.example.com/
    " rel="nofollow sponsored noopener">117 EUR</a></span> instead of 296 EUR</p>
    

    Also I am trying to extract the url and image link from this element, and replace it with a new HTML structure.

    <p><a style="font-size: 26px;text-decoration: none" target="_blank" href="https://www.example.com/
    " rel="nofollow sponsored noopener">Go to: <img decoding="async" width="130" style="border-radius:20px" src="https://www.example.com/wp-content/uploads/2023/07/image.png"></a> </p>
    

    The new replace structure for the second HTML element is

    <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="https://www.example.com/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="https://www.w3.org/2000/svg" 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>
    

    This is my attempt at achieving the desired result, however for some reason, the first 6, 7 and then other random posts do not seem to replace the data successfully or at all and also some of the price matches doesn’t seem to work, and produce empty prices container, especially if the prices have 333 or 4444 characters in them. The content is the same in every post, except for the prices… I have tried with and without the date and tax_query, the result was the same. I am not that good at regex patterns, I am not sure if that or what is causing this behavior.

     $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=\"https://www.w3.org/2000/svg\" 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();
        
            }
    
    }
    
  • The topic ‘Bulk edit certain p tag elements from Blog Posts content.’ is closed to new replies.