繁体   English   中英

如何在 Wordpress 的小部件描述中添加换行符?

[英]How to add line breaks in a widget description in Wordpress?

我想在 Wordpress 的小部件描述中添加换行符,以在管理面板中显示使用说明,如下所示:

To format the text use the tags:
<h3>Subtitle</h3>
<strong>Bold</strong>
[:pt]Text in Portuguese
[:en]Text in English
[:es]Text in Spanish

到目前为止的想法是使用 ISO 8859-1 符号 ( &nbsp; ) 添加空格,这不是最佳解决方案(描述为葡萄牙语):

register_sidebar( array(
    'name' => 'Widget',
    'id' => 'widget-user',
    'description' => "Para formatar o texto, utilize as tags: <h3>Subtítulo laranja</h3> &nbsp; <strong>Negrito</strong> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#91;:pt]Texto em português nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#91;:en]Texto em inglês &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#91;:es]Texto em espanhol &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (o que estiver escrito fora das tags de idioma irão aparecer para todos",
    'before_widget' => '<div class="widget-user"><div class="box">',
    'after_widget' => '</div></div>',
    'before_title' => '<h3 class="widget-title">',
    'after_title' => '</h3>',
));

与iten 'before_widget'、'after_widget'、'before_title'和'after_title'不同,'description'不接受HTML。 If it did, it would be easy to solve by simply escaping HTML I didn't want to be converted in HTML with a php function like htmlspecialchars() .

我拼命尝试了这些但没有成功:

"description" => "This is a line <br> break test"    
"description" => "This is a line \n break test"    
"description" => "This is a line \r break test"
"description" => "This is a line \r\n break test"
"description" => htmlspecialchars("This is a line <br>break test")
htmlspecialchars("description") => "This is a line <br>break test"
"description" => htmlentities("This is a line <br>break test")
"description" => html_entity_decode("This is a line <br>break test")
"description" => nl2br("This is a line <br> break test")
nl2br("description") => nl2br("This is a line <br> break test")

那么,有什么想法吗?

从您的问题来看,您的小部件和侧边栏听起来可能有些混乱 - 或者从您使用这两个术语的方式来看,这听起来可能如此。 因此,如果我向您重申显而易见的事情,我深表歉意。

侧边栏是您网站上可以显示小部件的动态部分。 小部件是显示在一个或多个侧栏中的单个项目。 您引用的函数register_sidebar()用于创建侧边栏区域——设置 --> 外观菜单中的块之一,您可以将小部件拖动到该区域。 描述字段仅用于指示基本信息。 关于该特定侧边栏的使用位置。 例如,如果您注册了一个计划在标题中使用的侧边栏,则说明可能类似于“出现在导航菜单上方的标题中”。

小部件也有提供信息的描述。 关于如何填写他们的各种选项。 这听起来像是你想要做的。 所以问题是你想使用什么小部件? 您可以在小部件代码中设置您正在谈论的描述。

至于侧边栏描述,Wordpress 使用函数wp_sidebar_description($id)来输出每个侧边栏的描述。 该函数通过另一个 Wordpress 函数esc_html()运行描述文本,该函数通过几次检查运行它并删除任何可能有问题的内容。

不过,您有机会更改默认输出。 它所做的最后一件事是对文本应用过滤器esc_html 因此,如果您想让<br>实际上用作换行符,您只需在主题的 functions.php 文件中添加一个过滤器即可恢复该功能,如下所示:

add_filter('esc_html', 'newLines');
function newLines($desc){
return htmlspecialchars_decode($desc);
}

这将基本上撤消 Wordpress 正在执行的操作并按照您的预期输出任何 HTML 代码(粗体、斜体等)。

应该在原始回复中指出,上述过滤器将影响您网站上使用的任何地方的 esc_html() 函数——您可能不希望那样!

不幸的是,没有任何挂钩或过滤器可以让您定位特定的侧边栏,但您可以使用以下方法将上述过滤器限制为仅在管理页面上发生:

function newLines($desc){
global $pagenow;
global $wp_registered_sidebars;
if($pagenow == 'widgets.php'){
return htmlspecialchars_decode($desc);
}
}
add_filter('esc_html', 'newLines');

在 wp-includes/widgets.php 中打开这个文件

找到名为的函数: wp_widget_description(); 在线664

用这个替换它

function wp_widget_description( $id ) { if ( !is_scalar($id) ) return;

global $wp_registered_widgets;

if ( isset($wp_registered_widgets[$id]['description']) )
    return ( $wp_registered_widgets[$id]['description'] );

}

我找到了一种使用短代码的方法。 首先,我们需要在functions.php文件中添加一个函数

function line_break_shortcode() {
    return '<br />';
}

add_shortcode( 'br', 'line_break_shortcode' );

然后我们可以在任何需要休息的地方使用 [br]

例如:

 Anti-counterfeiting [br] & [br] Online

解决方案 1

描述字段支持以下标签: a abbr acronym b blockquote cite code del em iqs strike strong 然后您可以添加样式display: block; 将文本移动到新行。

步骤1

register_sidebar( array(
  ...
  'description' => 'Sidebar <strong>description text</strong>',
  ...
));

第2步

function admin_style() {
  wp_enqueue_style( 'admin-style', 'admin-style.css', array(), 1.0.0, true );
}
add_action( 'admin_enqueue_scripts', 'admin_style' );

第 3 步

然后将以下内容添加到 admin-style.css

body.widgets-php .widgets-holder-wrap .description strong {
  display: block;
}

该解决方案不整洁且不理想。 第二种解决方案更好。


解决方案 2

可以使用pre_kses过滤器(代码参考)操作默认允许的标签并添加您自己的标签,方法如下。

function sidebar_description(  $string, $allowed_html, $allowed_protocols ) {
  if ($allowed_html == 'sidebar_description' ) {
    global $allowedtags;
    $allowedtags['br'] = array();
  }
  return $string;
}
add_filter( 'pre_kses', 'sidebar_description', 10, 3 );

然后将<br>添加到描述中。

register_sidebar( array(
  ...
  'description' => 'Sidebar <br>description text',
  ...
));

我不知道答案。 但是,一个可能的解决方案是将其添加到小部件 form() 中,就在字段的上方或下方。 如果您还需要容纳更多文本,则可以使表单更宽(小部件构造函数中的第四个参数是一个接受“宽度”的数组)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM