{"id":69,"date":"2017-03-27T01:08:28","date_gmt":"2017-03-27T00:08:28","guid":{"rendered":"http:\/\/nerostark.com\/b\/?page_id=69"},"modified":"2020-03-27T15:02:54","modified_gmt":"2020-03-27T15:02:54","slug":"custom-text-widgets-for-localized-text-in-wordpress-posts-page","status":"publish","type":"page","link":"https:\/\/cantile.xyz\/b\/mi-light\/custom-text-widgets-for-localized-text-in-wordpress-posts-page\/","title":{"rendered":"Custom widgets for localized text in wordpress posts page"},"content":{"rendered":"<h3>Problem:<\/h3>\n<p>On a wordpress project, the posts page needed static localized text.<br \/>\nThe site is an already deployed wordpress installation with a custom template and the multilingual plugin Polylang ( https:\/\/wordpress.org\/plugins\/polylang\/ )<br \/>\nThe standard wordpress package isn&#8217;t very friendly towards multilanguage sites, and relies on external plugins and tweaks to support different localized content.<\/p>\n<h3>Solution: Register and place dedicated widget areas in the index page, and create a custom text widget.<\/h3>\n<p>This solution can be implemented in three quick easy steps.<\/p>\n<h3>Step 1:<\/h3>\n<p>Create the code to register two new widget areas, one that goes in the higher part of the page and one for the middle<\/p>\n<p>In the file functions.php of the custom theme, add:<\/p>\n<pre>function cascina_widgets_init() {\n\nregister_sidebar( array(\n             'name'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =&gt; 'Higher - Descrizione Eventi e Eventi Futuri',\n             'id'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =&gt; 'eventi_descrizione_01',\n             ) );\n\nregister_sidebar( array(\n             'name'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =&gt; 'Middle - Titolo Eventi Passati',\n             'id'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =&gt; 'eventi_descrizione_02',\n             ) );\n}\n\nadd_action( 'widgets_init', 'cascina_widgets_init' );\n<\/pre>\n<h3>Step 2:<\/h3>\n<p>Display the widget area in the post page, by editing index.php and adding in the desired spots the following code:<\/p>\n<pre>&lt;?php dynamic_sidebar('eventi_descrizione_01'); ?&gt;<\/pre>\n<p>and<\/p>\n<pre>&lt;?php dynamic_sidebar('eventi_descrizione_02'); ?&gt;<\/pre>\n<h3><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-71 size-full\" src=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/custom_text_widget_01.png\" alt=\"\" width=\"974\" height=\"471\" srcset=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/custom_text_widget_01.png 974w, https:\/\/cantile.xyz\/b\/wp-content\/uploads\/custom_text_widget_01-300x145.png 300w\" sizes=\"(max-width: 974px) 100vw, 974px\" \/><\/h3>\n<p>Here are in the admin panel the new two widget areas on top of the others.<\/p>\n<h3>Step 3:<\/h3>\n<p>Now the template has two more widget areas, and I need a very simple text widget.<br \/>\nThe default one in wordpress has the &#8220;textwidget&#8221; class that alters the CSS and the desired outcome.<\/p>\n<p>To avoid messing with the textwidget class, I chose to define a new text widget from scratch, adding the following code in the custom theme functions.php<\/p>\n<pre>class Cascina_Text_Widget extends WP_Widget {\n\n    public function __construct() {\n          $widget_ops = array('classname' =&gt; 'Cascina_Widget', 'description' =&gt; 'Widget Testo Eventi' );\n          $this-&gt;WP_Widget('Cascina_Text_Widget', 'Widget Testo Eventi', $widget_ops);\n    }\n\n    public function form( $instance ) {\n          $instance = wp_parse_args( (array) $instance, array( 'title' =&gt; '' ) );\n          $title = $instance['title'];\n          $text = $instance['text'];\n    ?&gt;\n\n    &lt;p&gt;&lt;label for=\"&lt;?php echo $this-&gt;get_field_id('text'); ?&gt;\"&gt;Text\/HTML Code:\n&lt;textarea class=\"widefat\" rows=\"16\" cols=\"20\" id=\"&lt;?php echo $this-&gt;get_field_id('text'); ?&gt;\" name=\"&lt;?php echo $this-&gt;get_field_name('text'); ?&gt;\"\/&gt; &lt;?php echo attribute_escape($text); ?&gt;&lt;\/textarea&gt;\n    &lt;\/label&gt;&lt;\/p&gt;\n\n   &lt;?php\n   }\n    function widget( $args, $instance ) {\n          extract($args);\n          $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' :           $instance['text'], $instance );\n          echo $text;\n          }\n }\n\nadd_action( 'widgets_init', function(){\n          register_widget( 'Cascina_Text_Widget' );\n          });<\/pre>\n<h3>In conclusion:<\/h3>\n<p>I can now add multiple copies of this custom widget and choose which one to show to the visitor according to the language the visitor is using.<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-72 size-large\" src=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/custom_text_widget_02-1024x589.png\" alt=\"\" width=\"840\" height=\"483\" srcset=\"https:\/\/cantile.xyz\/b\/wp-content\/uploads\/custom_text_widget_02-1024x589.png 1024w, https:\/\/cantile.xyz\/b\/wp-content\/uploads\/custom_text_widget_02-300x173.png 300w, https:\/\/cantile.xyz\/b\/wp-content\/uploads\/custom_text_widget_02-1200x690.png 1200w, https:\/\/cantile.xyz\/b\/wp-content\/uploads\/custom_text_widget_02.png 1288w\" sizes=\"(max-width: 840px) 100vw, 840px\" \/><br \/>\nThis custom text widget that will be placed in the newly registered widget areas is a quick solution that works well with the Polylang plugin, and serves the purpose.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: On a wordpress project, the posts page needed static localized text. The site is an already deployed wordpress installation with a custom template and the multilingual plugin Polylang ( https:\/\/wordpress.org\/plugins\/polylang\/ ) The standard wordpress package isn&#8217;t very friendly towards multilanguage sites, and relies on external plugins and tweaks to support different localized content. Solution: &hellip; <a href=\"https:\/\/cantile.xyz\/b\/mi-light\/custom-text-widgets-for-localized-text-in-wordpress-posts-page\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Custom widgets for localized text in wordpress posts page<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":46,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/pages\/69"}],"collection":[{"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":9,"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/pages\/69\/revisions"}],"predecessor-version":[{"id":115,"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/pages\/69\/revisions\/115"}],"up":[{"embeddable":true,"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/pages\/46"}],"wp:attachment":[{"href":"https:\/\/cantile.xyz\/b\/wp-json\/wp\/v2\/media?parent=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}