I am developing a plugin for my WordPress site. I want to select all non-empty paragraph elements.
Here is my code :
function my_php_custom_function($content){
// Create a new DOMDocument instance
$dom = new DOMDocument();
// Load the HTML content into the DOMDocument
$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
// Create a DOMXPath object to query the DOM
$xpath = new DOMXPath($dom);
// Find all non-empty p elements in the content
$p_elements = $xpath->query('//p[string-length(normalize-space()) > 0]');
}
add_filter('the_content','my_php_custom_function')
$p_elements in this variable I am getting those paragraphs also which I have just created by pressing enter. When I check on DOM, it is showing as <p> </p>
is not the result of a carriage return and evaluates correctly as non-empty. Where does your content of the$contentvariable come from?