1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
// Function to insert "Hello, World!" after a specified percentage of the content function insert_hello_world_after_percentage($content, $percentage) { // Check if the content is a post or a page if (is_single() || is_page()) { // Create a DOMDocument to parse the HTML content $dom = new DOMDocument(); libxml_use_internal_errors(true); // Disable libxml errors $dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8')); libxml_clear_errors(); // Calculate the position corresponding to the specified percentage of the paragraphs $paragraphs = $dom->getElementsByTagName('p'); $percentage_pos = ceil($paragraphs->length * ($percentage / 100)); // Generate "Hello, World!" content or your ad code $hello_world_content = '<p>Hello, World!</p>'; // Replace the line above with your actual ad code // Insert the ad after the specified percentage of the paragraphs if ($percentage_pos > 0 && $percentage_pos <= $paragraphs->length) { $referenceNode = $paragraphs->item($percentage_pos - 1); $newNode = $dom->createDocumentFragment(); $newNode->appendXML($hello_world_content); $referenceNode->parentNode->insertBefore($newNode, $referenceNode->nextSibling); } // Save the modified HTML $modified_content = $dom->saveHTML(); return $modified_content; } // For other types of content, return the original content return $content; } // Add the filter to modify the content for 10% add_filter('the_content', function ($content) { return insert_hello_world_after_percentage($content, 10); }); // Add the filter to modify the content for 20% add_filter('the_content', function ($content) { return insert_hello_world_after_percentage($content, 20); }); // Add the filter to modify the content for 30% add_filter('the_content', function ($content) { return insert_hello_world_after_percentage($content, 30); }); |
fdgfdg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Function to insert "Hello, World!" after 50% of the page content function insert_hello_world_after_50_percent($content) { // Check if the content is a post or a page if (is_single() || is_page()) { // Calculate the midpoint position $midpoint_pos = floor(strlen($content) / 2); // Generate "Hello, World!" content or your ad code $hello_world_content = '<p>Hello, World!</p>'; // Replace the line above with your actual ad code // Insert "Hello, World!" after 50% of the page content $modified_content = substr_replace($content, $hello_world_content, $midpoint_pos, 0); return $modified_content; } // For other types of content, return the original content return $content; } // Add the filter to modify the content add_filter('the_content', 'insert_hello_world_after_50_percent'); |