douyu2817 2012-05-06 06:38
浏览 43
已采纳

如何使用WordPress中的过滤器将类添加到html元素?

I am changing the current theme of my blog and I wish to add a class to all my input fields.

I am not well versed with regex codes, so I end up in a lot of trouble whenever I need to make things like these happen.

type="submit"

I want to add class btn needs to be added to <input type="submit">

Old Code

<input type="submit" id="some_id" name="something" class="some_class" value="some_value" />

New Code

<input type="submit" id="some_id" name="something" class="btn some_class" value="some_value" />

and

Similarly, I want the class input to be added to <input type="text"> and <input type="textarea">

Old Code

<input type="text" id="some_id" name="something" class="some_class" value="some_value" />
<textarea id="some_id" name="something" class="some_class" value="some_value" />

New Code

<input type="text" id="some_id" name="something" class="input some_class" value="some_value" />
<textarea id="some_id" name="something" class="input some_class" value="some_value" />
  • 写回答

4条回答 默认 最新

  • dsklfsdlkf1232 2012-05-09 02:14
    关注

    Given your constraints, the cleanest way you can do what you want with PHP and stay within the Wordpress framework is to use DOMDocument. While it's POSSIBLE to rely on regular expressions, it's very sloppy and you can run into more problems than what you started with.

    Place this in your functions.php file, and it should do everything you need:

    add_filter('the_content', 'add_text_input_classes', 20);
    function add_text_input_classes($content)
    {
        $doc = new DOMDocument(); //Instantiate DOMDocument
        $doc->loadHTML($content); //Load the Post/Page Content as HTML
        $textareas = $doc->getElementsByTagName('textarea'); //Find all Textareas
        $inputs = $doc->getElementsByTagName('input'); //Find all Inputs
        foreach($textareas as $textarea)
        {
            append_attr_to_element($textarea, 'class', 'input');
        }
        foreach($inputs as $input)
        {
            $setClass = false;
            if($input->getAttribute('type') === 'submit') //Is the input of type submit?
                $setClass = 'btn';
            else if($input->getAttribute('type') === 'text') //Is the input of type text?
                $setClass = 'input';
    
            if($setClass)
                append_attr_to_element($input, 'class', $setClass);
        }
        return $doc->saveHTML(); //Return modified content as string
    }
    function append_attr_to_element(&$element, $attr, $value)
    {
        if($element->hasAttribute($attr)) //If the element has the specified attribute
        {
            $attrs = explode(' ', $element->getAttribute($attr)); //Explode existing values
            if(!in_array($value, $attrs))
                $attrs[] = $value; //Append the new value
            $attrs = array_map('trim', array_filter($attrs)); //Clean existing values
            $element->setAttribute($attr, implode(' ', $attrs)); //Set cleaned attribute
        }
        else
            $element->setAttribute($attr, $value); //Set attribute
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路