0

I try to change type attribute of input fields to number (they have hidden type by default) and add readonly="readonly", but it has no effect on output HTML. Attributes are as they were.

Function is triggered properly, because before I added encoding, it showed incorrect characters on page. I have proper CSS to format readonly inputs, so visuals are not a problem, I will also use more conditions to find only specific input tags, but for now I would like to get this code to work properly:

add_filter('the_content', 'acau_lock_input');
function acau_lock_input($content) {
    $dom = new DOMDocument();       
    @$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));

    foreach ($dom->getElementsByTagName('input') as $node) {            
        $node->setAttribute('type', 'number');
        $node->setAttribute('readonly', 'readonly');
    }
    $newHtml = $dom->saveHtml();
    return $newHtml;
}
Ryszard Jędraszyk
  • 2,296
  • 4
  • 23
  • 52

1 Answers1

0

Nigel Ren helped me to find the problem. The solution is ridiculous, the_content filter of WordPress is simply whatever is placed in the text area of the current page as visible in the editor.

It has nothing to do with full HTML structure of output page, so while wrong encoding can break text of the whole page, $content returns only one shortcode because this is in the page editor.

To learn how to edit DOM for WordPress, including the output of all templates, check this question:

PHP DOM in WordPress - add attribute in output buffer HTML

Ryszard Jędraszyk
  • 2,296
  • 4
  • 23
  • 52