Insert ads after a specific paragraph in WordPress

To insert adsens ad units after paragraph 2, you need to write the following PHP code in the functions.php file.

Inserting ads after paragraph 2

// Function to insert "Hello, World!" after the second paragraph
function insert_hello_world_after_second_paragraph($content) {
    // Check if the content is a post or a page
    if (is_single() || is_page()) {
        // Find the position of the closing tag of the second paragraph
        $second_paragraph_pos = strpos($content, '</p>', strpos($content, '<p>') + 1) + 4;

        // Generate "Hello, World!" content
        $hello_world_content = '<p>Hello, World!</p>';

        // Insert "Hello, World!" after the second paragraph
        $modified_content = substr_replace($content, $hello_world_content, $second_paragraph_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_second_paragraph');

Inserting ads after paragraph 5

// Function to insert "Hello, World!" after the fifth paragraph
function insert_hello_world_after_fifth_paragraph($content) {
    // Check if the content is a post or a page
    if (is_single() || is_page()) {
        // Find the position of the closing tag of the fifth paragraph
        $fifth_paragraph_pos = strpos($content, '</p>', strpos($content, '</p>', strpos($content, '</p>', strpos($content, '</p>', strpos($content, '<p>') + 1) + 1) + 1) + 1) + 4;

        // Generate "Hello, World!" content
        $hello_world_content = '<p>Hello, World!</p>';

        // Insert "Hello, World!" after the fifth paragraph
        $modified_content = substr_replace($content, $hello_world_content, $fifth_paragraph_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_fifth_paragraph');

Insert ads after 10%, 20%, 30%, 40%, 50%, 60%, 70%, 80%, 90%, 100% of the content

Insert ads after 10%, 20%, 30%, 40%, 50%, 60%, 70%, 80%, 90%, 100% of the content.

To insert adsens ad units after paragraph 2, you need to write the following PHP code in the functions.php file.

// 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);
});



To insert adsens ad units after paragraph 2, you need to write the following PHP code in the functions.php file.

 

Contents Copyrights Reserved By T4Tutorials