WordPress中的PHP DOM-在输出缓冲区HTML中添加属性 [英] PHP DOM in WordPress - add attribute in output buffer HTML

查看:61
本文介绍了WordPress中的PHP DOM-在输出缓冲区HTML中添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在产品和购物车页面上将所有 input 类别为 qty 的标签设为 type = number 和 readonly = readonly 属性。

I want to make all input tags on product and cart page with class qty have type="number" and readonly="readonly" attributes through modifying DOM with PHP.

当我将其限制在购物车和产品页面时,它仅使延迟加载插件和Autoptimize插件停止对这两个页面起作用,而其他页面则可以。在管理页面上也找不到任何控制台错误。

When I limit it to cart and product page, it "only" makes lazy load plugin and Autoptimize plugin stop working for these 2 pages, while other pages are fine. I can't spot any console errors, on admin pages as well.

当我允许它在每个页面上运行时,会发生以下情况:

When I allow it to run for every page, the following happens:


  1. 产品和购物车页面上的控制台中没有错误。

  2. Ajax请求Ajax添加到购物车插件中断。 / li>
  3. 我收到多个控制台错误,这些错误会破坏许多管理页面的布局,从而移动和隐藏部分内容。

这些输出缓冲区的修改就像被黑客入侵,从而导致错误/不兼容。可能是因为 libxml_use_internal_errors(true); 隐藏了警告并且实际上没有解决任何问题吗?

These output buffer modifications feel like hacking with resulting errors / incompatibilities. Could it be because of libxml_use_internal_errors(true); which hides warnings and doesn't actually fix anything?

< a href = https://stackoverflow.com/questions/11819603/dom-loadhtml-doesnt-work-properly-on-a-server> Dom loadHTML在服务器上无法正常工作

这就是我在functions.php中拥有的内容:

This is what I have in my functions.php:

add_action( 'template_redirect', 'acau_activate_buffer', 99999 );
function acau_activate_buffer() {
    // cart and product page only, WooCommerce required for below line to work, remove to reproduce issues without WooCommerce      
    if ( ! is_cart() && ! is_product() ) return;
    ob_start();
}

add_action('shutdown', function() {
    // cart and product page only, WooCommerce required for below line to work, remove to reproduce issues without WooCommerce          
    if ( ! is_cart() && ! is_product() ) return;
    $final = '';

    // Collect output from all previous buffers.
    $levels = ob_get_level();

    for ($i = 0; $i < $levels; $i++) {
        $final .= ob_get_clean();
    }

    echo apply_filters('acau_output', $final);

}, -99999);

// Filter final output.
add_filter('acau_output', function($output) {

    $dom = new DOMDocument();
    libxml_use_internal_errors(true);
    $dom->loadHTML(mb_convert_encoding($output, 'HTML-ENTITIES', 'UTF-8'));

    foreach ($dom->getElementsByTagName('input') as $node) {
        $classes = explode (' ', $node->getAttribute('class') );
        if ( in_array ( 'qty', $classes ) ) {
            $node->setAttribute('type', 'number');
            $node->setAttribute('readonly', 'readonly');
        }
    }
    $newHtml = $dom->saveHtml();
    return $newHtml;

});


推荐答案

我找到了解决我问题的方法关于nextlandweb的Roland在WordPress支持论坛上发布的内容。他提交了一个错误报告:

I've found a solution to my issue, based on what Roland from nextendweb posted on WordPress support forums. He filed a bug report:

https://bugs.php.net/bug.php?id=76563

在我看来,他的建议是

$final .= ob_get_contents();
ob_clean();

而不是:

$final .= ob_get_clean();

因为它无法正常工作。

这篇关于WordPress中的PHP DOM-在输出缓冲区HTML中添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆