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条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教