Support » Plugin: WP Editor Widget » No ‘widget-content’ div when excluding title output

  • Resolved rarcher30

    (@rarcher30)


    Hi David,

    Not sure if this is an intended feature or bug/oversight but when I decided to NOT output the title of the Rich Text widget, I notice that it does not print:

    <div class="widget-content">[insert content here]</div>
    BTW, works great for not printing the h2 tag

    I came across it as the site I am working on has CSS directly applied to .text-widget .widget-content – which disappeared when the tags disappeared.

    Cheers,
    Ryan

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Author feedmeastraycat

    (@feedmeastraycat)

    It doesnt output the widget at all?

    I did a test now on WP 4.6.1 and Twenty Fourteen. I created two WP Editor Widgets, added “Title 1” and “Content 1” on the first and “Title 2” and “Content 2” on the second one and unmarked the checkbox to output the title on the second one. And this is the result HTML:

    <div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary">
      <aside id="wp_editor_widget-7" class="widget WP_Editor_Widget">
        <h1 class="widget-title">Titel 1</h1>
        <p>Content 1</p>
      </aside>
      <aside id="wp_editor_widget-8" class="widget WP_Editor_Widget">
        <p>Content 2</p>
      </aside>	
    </div>

    So to me it looks correct, no? Or am I just misunderstanding something?

    /D

    I got something more to share. The issue arose because of the way the sidebar widget was declared in widgets.php:

        register_sidebar(array(
            'name' => __('Sidebar', 'understrap'),
            'id' => 'sidebar-1',
            'description' => '',
            'before_widget' => '<aside id="%1$s" class="widget text-widget %2$s">',
            'after_widget' => '</div></aside>',
            'before_title' => '<h2>',
            'after_title' => '</h2><div class="widget-content">',
        ));

    Because the code is appended to options ‘before_title’ and ‘after_title’ – when choosing not to output the title of the Rich Text Widget, it strips out these values (which include the additional HTML that was in use). I’m going to look at a workaround for this.

    My current workaround for this is to take out the ‘widget-content’ div from the sidebar registration and then insert it within the plugins’ “class-widget.php” file, surrounding “echo $content; like thus:

    echo $content;
    
    echo '</div>';
    
    echo $after_widget;

    Not really ideal I guess, but just need to find an easy way to wrap the text area content within the div tag.

    Oops! Shame I can’t edit the post above. I left out this code before the start of echo $content:

    echo '<div class="widget-content">';

    I haven’t been able to figure out how to get a <div> to surround the text content, stopping short of editing the plugin, which is totally not ideal. When choosing to “not output the title”, both the content in before_title and after_title are completely stripped out which is where my div surrounding the text has been put.

    Is there any possible scope for you to add some extra parameters of before_text and after_text like Black Studio TinyMce plugin have done (with some custom hooks)? Here is a link https://en-au.wordpress.org/plugins/black-studio-tinymce-widget/faq/

    Otherwise, is there a hook option to simply disable option for choosing whether to output title or not?

    Plugin Author feedmeastraycat

    (@feedmeastraycat)

    Sorry for not getting back to you earlier. Been a couple of crazy weeks here for me.

    The function that outputs the widget you can see on github here:
    https://github.com/feedmeastraycat/wp-editor-widget/blob/v0.5.5/classes/class-widget.php#L43

    It is pretty much a standard widget setup.

    It outputs $before_widget always.
    It outputs $before_title and $after_title only if there is a title.
    It outputs $content.
    It outputs $after_widget.

    The variables $before_widget, $before_title, $after_title and $after_widget is populated from how you register your sidebars with register_sidebar().

    You have three filters to play around with:
    – wp_editor_widget_title
    – wp_editor_widget_output_title
    – wp_editor_widget_content

    So I guess you could do something like this … maybe? (Just thinking out loud here).

    function my_wp_editor_widget_content_filter( $content ) {
      return '<div class="widget-content">' . $content . '</div>';
    }
    add_filter( 'wp_editor_widget_content', 'my_wp_editor_widget_content_filter', 10, 1 );

    It should add a filter which would wrap the content of the widget in a <div class=”widget-content”> regardless if title is used or not.

    Then remove this div from your register_sidebar() so it only registers the <aside> and the <h2>.

    Absolute gem man!
    Put that filter in my inc/widgets.php file and it worked a treat.

    Thanks again.

    Plugin Author feedmeastraycat

    (@feedmeastraycat)

    Great to hear! 🙂

    rarcher30

    (@rarcher30)

    Yeah, I have a small problem that I did not see before. I have other widgets that depend on the ‘widget-content’ div being registered in the ‘register-sidebar’ function. Now only the WP Editor widgets have that div applied.

    I decided to try and write in a conditional ternary expression into the register sidebar function like so:

        register_sidebar(array(
            'name' => __('Sidebar', 'understrap'),
            'id' => 'sidebar-1',
            'description' => '',
            'before_widget' => '<aside id="%1$s" class="widget text-widget %2$s">',
            //'after_widget' => '</aside>',
            'after_widget' => (wp_editor_widget_content) ? '</aside>' : '</div></aside>',
            'before_title' => '<h2>',
            //'after_title' => '</h2><div class="widget-content">',
            'after_title' => (wp_editor_widget_content) ? '</h2>' : '<div class="widget-content">',
        ));

    I’m trying to check for the prescence of the WP Editor widget in the sidebar and apply the appropriate code for ‘after title’ and ‘after widget’. I’ve tried using other expressions like ‘wp_editor_widget’ and ‘WP_Widget’ to no avail. I keep getting error message saying that I am using an undefined constant. Get the feeling that I can’t use conditional ternary statements in the register_sidebar function but would appear most obvious fix.

    rarcher30

    (@rarcher30)

    Nah, I’ve even tried to run some logic outside the array:

    function understrap_widgets_init()
    {
    $afterWidget = (wp_editor_widget_content) ? '</aside>' : '</div></aside>';
    $afterTitle = (wp_editor_widget_content) ? '</h2>' : '</h2><div class="widget-content">';
    
        register_sidebar(array(
            'name' => __('Sidebar', 'understrap'),
            'id' => 'sidebar-1',
            'description' => '',
            'before_widget' => '<aside id="%1$s" class="widget text-widget %2$s">',
            'after_widget' => $afterWidget,
            'before_title' => '<h2>',
            'after_title' => $afterTitle,
        ));
    
    }
    add_action('widgets_init', 'understrap_widgets_init');

    and I’ve got errors. Is there a global in your plugin code that I can check for so this conditional will work? I’ve tried a number of things but none of them are working.

    rarcher30

    (@rarcher30)

    Finally removed the errors by revising code:

    ....
    function understrap_widgets_init($wp_editor_widget)
    {
    $afterWidget = ($wp_editor_widget) ? '</aside>' : '</div></aside>';
    $afterTitle = ($wp_editor_widget) ? '</h2>' : '</h2><div class="widget-content">';
    ...

    But it can obviously never see the global as it always goes to the fallback for every widget (inc WP Editor widgets).

    Plugin Author feedmeastraycat

    (@feedmeastraycat)

    Yeah thats a tricky one. Unfortunately I dont really have a good idea right now on how you could solve that.

    You could look up those specific widgets and try to alter them if they have filters.

    Or you could add the HTML you want in the register_sidebar() function and move it for WP Editor Widget using its filter.

    However, having <div class="widget-content"> in after_title means it will only be printed if title is used. And since you have </div> in after_widget it will always be printed. Meaning you might end up with a </div> without its opening <div> and you might get really weird HTML errors.

    rarcher30

    (@rarcher30)

    @feedmestraycat, your last comment is very true. After coming back to this again, I think the best solution is removing the option for the user to choose whether or not they will output the title (due to dependencies for other widgets that have already been set within the dynamic sidebar declaration).

    Is there a filter available to simply remove the checkbox option to “Output title” and leave it to output title by default?

    Plugin Author feedmeastraycat

    (@feedmeastraycat)

    There is no filter to output the checkbox in admin.

    But there is an action before the data is saved:
    https://github.com/feedmeastraycat/wp-editor-widget/blob/v0.5.5/classes/class-widget.php#L123

    And there is a filter on the data being saved:
    https://github.com/feedmeastraycat/wp-editor-widget/blob/v0.5.5/classes/class-widget.php#L125

    So you could use either to overwrite the data so title option always will be checked regardless of what the user chooses.

    I would suggest also use this filter to make sure title output option is turned on regardless of what is saved as well:
    https://github.com/feedmeastraycat/wp-editor-widget/blob/v0.5.5/classes/class-widget.php#L48

    However. If a user does not add a title you will obviously get an empty title tag. So it will be, for example, <h2></h2><div class="widget-content">.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘No ‘widget-content’ div when excluding title output’ is closed to new replies.