簡體   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