3

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>',
));

itens'before_widget'、'after_widget'、'before_title'、および'after_title'とは異なり、'description'はHTMLを受け入れません。もしそうなら、私がHTMLに変換したくないHTMLをエスケープするだけで簡単に解決できます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")

それで、何かアイデアはありますか?

4

4 に答える 4

2

From your question it sounds like you might have widgets and sidebars slightly confused -- or maybe it just sounds that way from the way you're using the two terms. So my apologies if I'm restating the obvious to you.

Sidebars are dynamic sections on your site that can display widgets. Widgets are the individual items that are displayed in one or more sidebars. The function you're referencing, register_sidebar(), is meant for creating a sidebar area -- one of those blocks in the Settings --> Appearance menu that you can drag widgets to. The description field is meant just to indicate basic info. about where that particular sidebar is used. For example, if you register a sidebar that you plan to use in your header the description might be something like "Appears in header above navigation menu."

Widgets also have descriptions that give info. on how to fill out their various options. That sounds like what you're trying to do. So the question would be what widget are you trying to use? It's in the widget code that you would set up that description you're talking about.

As far as sidebar descriptions, Wordpress uses the function wp_sidebar_description($id) to output the description for each sidebar. That function runs the description text through another Wordpress function, esc_html(), which runs it through a couple of checks and removes anything that might be a problem.

There is an opportunity for you to change the default output though. The last thing it does is apply a filter, esc_html, to the text. So if you want to make <br> actually function as a newline, you could simply add a filter in your theme's functions.php file to restore that functionality, like so:

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

That will basically undo what Wordpress is doing and output any HTML code (bold, italics, etc.) just as you'd expect.

Should have noted in the original response, the above filter will affect the esc_html() function everywhere it is used on your site -- you probably do not want that!

There aren't, unfortunately, any hooks or filters that would let you target a specific sidebar but you could limit the filter above to only happening on the admin page by using the following:

function newLines($desc){
global $pagenow;
global $wp_registered_sidebars;
if($pagenow == 'widgets.php'){
return htmlspecialchars_decode($desc);
}
}
add_filter('esc_html', 'newLines');
于 2012-12-16T03:18:24.687 に答える
0

このファイルを 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'] );

}

于 2013-12-08T12:01:08.213 に答える
-1

答えはわかりません。ただし、可能性のある解決策は、これを代わりにウィジェット form() に、フィールドのすぐ上または下に追加することです。より多くのテキストを収容する必要がある場合は、フォームを広くすることができます (ウィジェット コンストラクターの 4 番目のパラメーターは、「幅」を受け入れる配列です)。

于 2012-12-16T02:59:10.130 に答える