WP编辑器去除输入占位符属性

时间:2012-06-11 作者:premtech11

为什么WP编辑器还会剥离输入文本元素的“占位符”属性?当然,我使用的是HTML模式。以下是输入:

<input type="text" value="" name="s" style="width: 550px;" placeholder="Search this website..">
更新帖子后(删除后):

<input type="text" value="" name="s" style="width: 550px;">
我不希望WP编辑器剥离这些属性。

有什么帮助吗?

2 个回复
SO网友:fuxia

允许的元素和属性列表存储在全局变量中$allowedposttags 设置为wp-includes/kses.php.

要覆盖它,请创建一个简单mu plugin 包括以下内容:

<?php # -*- coding: utf-8 -*-
/**
 * Plugin Name: Enable placeholder attribute for input elements in post tags.
 * Version: 2012.07.18
 */

add_action( \'init\', \'wpse_54829_register_placeholder\' );


function wpse_54829_register_placeholder()
{
    global $allowedposttags;

    $default = empty ( $allowedposttags[\'input\'] ) ? array () : $allowedposttags[\'input\'];
    $custom  = array (
        \'placeholder\' => TRUE,
        \'name\'        => TRUE,
        \'value\'       => TRUE,
        \'size\'        => TRUE,
        \'maxlength\'   => TRUE,
        \'type\'        => TRUE,
        \'required\'    => TRUE
    );

    $allowedposttags[\'input\'] = array_merge( $default, $custom );
}
此帖子包含内容<input placeholder="pass" required /> 使用作者帐户创建:

enter image description here

SO网友:Evan Mattson

您可以使用短代码!;)

<?php

// desired output: <input type="text" value="" name="s" style="width: 550px;" placeholder="Search this website..">
// sc: [text_input name="s" style="width: 550px;" placeholder="Search this website.."]

add_shortcode(\'text_input\',\'text_input_sc\');
function text_input_sc($atts) {

    // modify defaults as you wish
    $defaults = array(
        \'id\' => null,
        \'class\' => null,
        \'value\' => null,
        \'name\' => null,
        \'size\' => null,
        \'style\' => null,
        \'placeholder\' => null
    );

    $args = shortcode_atts($defaults, $atts);

    $out = array();

    foreach ($args as $attr => $value) {

        if ( null !== $value )
            $out[] = $attr.\'="\'.$value.\'"\';

    }

    $out = trim(implode(\' \', $out));

    if( !empty($out) )
        $out = \' \'.$out;

    return vsprintf(\'<input type="text"%s>\', $out);

}
未经测试,但绝对有效!

结束

相关推荐