The functionality of the "class" parameter in wordpress register_sidebar is not functioning as expected

Despite its initial appearance of simplicity, the task is proving to be quite complex.

I am attempting to register a class on the inner part of the widget output. My setup involves using Bootstrap 3, which requires me to include a panel-body class in the inner section, while the entire widget itself is enveloped by a panel with the class panel-default.

For example:

register_sidebar( 
        array( 
                'name' => __( 'Home Page (Beside Content)' , 'symbiostock' ),
                'id' => 'home-page-beside-content',
                'class'         => 'panel-body',
                'before_widget' => '<div class="panel panel-default home-beside-content"><aside id="%1$s" class="widget %2$s">',
                'after_widget' => '</aside></div>',
                'before_title' => '<div class="row panel-heading"><h3 class="featured-posts col-md-12">',
                'after_title' => '</h3></div>', ) );

Initially, one would expect that "class=>'something'" would apply a class to the inner part of the widget. However, this does not appear as intended. The result is as follows:

<div class="panel panel-default home-beside-content">
   <aside class="widget widget_text" id="text-8">
      <div class="row panel-heading">
         <h3 class="featured-posts col-md-12">This is a test</h3>
      </div>
      <div class="textwidget">This is a test</div>
   </aside>
</div>

The expected outcome is:

<div class="panel panel-default home-beside-content">
   <aside class="widget widget_text" id="text-8">
      <div class="row panel-heading">
         <h3 class="featured-posts col-md-12">This is a test</h3>
      </div>
      <div class="textwidget panel-body">This is a test</div>
   </aside>
</div>

Take note of the addition of "panel-body".

How can I resolve this issue? The nesting order of classes is crucial due to my use of bootstrap 3.

http://getbootstrap.com/components/#panels

Answer №1

I successfully addressed the issue using jQuery towards the bottom of my theme's footer.php file.

To accomplish this, I looped through each panel element, extracted all children except for panel-heading, and enclosed them within a panel-body container.

<script>
jQuery('.panel').each(function(index, value) {
 jQuery(value).children().not('.panel-heading').wrapAll('<div class="panel-body"></div>');
});
</script>

Note: I utilized the complete term JQuery instead of the shortcut $ due to its unavailability in this context.

Answer №2

Indeed, one might assume that to be the case, but Wordpress has a tendency to do the unexpected! :-)

I've encountered this issue in the past. It seems to operate in a rather unconventional manner. To resolve it, you'll have to cleverly enclose your widget within that specific class. Here's a potential workaround:

register_sidebar( 
    array( 
            'name' => __( 'Home Page (Beside Content)' , 'symbiostock' ),
            'id' => 'home-page-beside-content',
            'before_widget' => '<div class="panel panel-default home-beside-content"><aside id="%1$s" class="widget %2$s">',
            'after_widget' => '</div></aside></div>',
            'before_title' => '<div class="row panel-heading"><h3 class="featured-posts col-md-12">',
            'after_title' => '</h3></div><div class="panel-body">', ) );

Take note that the opening div is initiated in 'after_title' and closed in 'after_widget'.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Skip the "required" attribute in HTML forms for submission

Before submitting a form, I rely on the use of required for initial validation. <form action = "myform.php"> Foo: <input type = "text" name = "someField" required = "required"> <input type = "submit" value = "submit"> <input typ ...

Utilizing Python and Selenium with PhantomJS to convert multiple files into PDFs

I have been working on implementing the code provided in Python + Selenium + PhantomJS render to PDF Instead of saving a single web page as a PDF file, I am trying to iterate through a list of URLs and save each one with a specific name taken from ano ...

Getting the value of dynamically generated buttons when they are clicked in PHP - a simple guide

In my PHP code, I have successfully created dynamic buttons. However, I am facing an issue where I need to retrieve the value of a specific button when it is clicked, and populate all the information in a form. Below is the code snippet for handling the b ...

Is WordPress version 4.9.6 or 4.9 compatible with MySQL version 5.20?

Is it possible to test if WordPress version 4.9.6 (or 4.9) is compatible with MySQL 5.20? How can this compatibility be verified? ...

Dragging elements with jQuery to move them outside of a side panel

For a hidden panel, I am utilizing the script mentioned below. I have listed some boxes in the panel and attempted to drag them and drop them onto a workspace. This is a demonstration that works with HTML5. <a id="simple-menu" href="#sidr">Jobs< ...

The inability of Chrome and Firefox browsers to open Windows folders directly from a hyperlink is a common

My link is directing to a Windows folder using its directory path. Surprisingly, it works perfectly fine in Internet Explorer but fails to work in Chrome and Firefox. How can I resolve this issue? The structure of my link looks like this: <a href="&bs ...

Styling and Div Container management with Bootstrap in ASP.NET Code Behind

I am currently in the process of developing an ASP.NET application that requires the dynamic creation of "TextBox" and "DropdownList" controls. Here is the method I have been using to create "TextBox" controls: private void CreateTextBox(string id, string ...

Tips for creating distinct hover descriptions for each element in an array

My dilemma may not be fully captured by the title I've chosen, but essentially, I am working with a list of names and I want each one to display a unique description when hovered over with a mouse. On the surface, this seems like a simple task, right ...

Arranging items in a semi-circular formation around the color Blue in a precise manner

I have a character named Blue (who is blue) and I would like to place some elements in a half-circle around him. Each element will consist of an image and a text span positioned below the image. My current attempt results in the elements rotating incorre ...

Start by prioritizing items with a higher click count when looping through the

I am creating a component to display recent search results. The API provides these results, including a value called "click_count". Each time a user clicks on a positive search result from the recent searches, this click_count value will increment by one. ...

Button in Bootstrap input group moves down when jQuery validation is activated

Check out my bootstrap styled form: <div class="form-group"> <label for="formEmail">User Email</label> <div class="input-group"> <select class="form-control" data-rule-emailRequired="true" ...

What is the best way to position a Button on the far right of an MUI AppBar?

I'm struggling to grasp the concept of aligning items in MUI. Here is my code snippet: class SignUpForm extends React.Component { render() { return ( <Button sx={{ justifyContent: "flex-end" }} colo ...

Tips for changing the color of shapes when hovering over an image state

I have arranged three images in a column and applied column-count: 3; along with setting border-width for those images. Now I am curious about how to create the hover state for these images. Here is the HTML code snippet: <div class="wrap"> < ...

Merge JSON object information into tables, including column headings

I am currently facing a challenge in loading multiple JSON files into tables within different divs. Here is the structure of my JSON data: JSON file 1: [ { "id": 12345, "code": "final", "error": "", "data": [ ...

The alignment of the content within a div is mismatched compared to a div that doesn't have any

Can you figure out why the element with content is out of order compared to the other elements without content in the code below? This issue seems to disappear when all elements have content. Any insights on this peculiar behavior? (Thank you in advance fo ...

creating nested columns within a parent column using flexbox techniques

Struggling with column height issues in Bootstrap 3? Consider using flexboxes for a simpler solution. Check out the design image I created using Bootstrap 3: https://i.sstatic.net/PVCKm.png Can this design be replicated with flexboxes? I have successfull ...

How to design an HTML table with columns of a set width using CSS

I need a table that has a fixed width but dynamic height to accommodate varying content lengths. Take a look at this example: Despite setting a fixed width, my content is overlapping. What am I doing incorrectly? Here is my current code: <table width= ...

Challenges with Div Height and Border

Hello there! I've run into a height and border issue with a div. Here's the CSS I'm currently working with: .content { background-image: url(../../images/logo-04.png); background-position: left bottom; ...

resizing a big image to perfectly fit the width of the screen with the help of

Is there a way to use AngularJS to automatically adjust the size of a large image to fit the screen on various devices (responsive), similar to how it is done on this website? Are there any Angular directives available for this purpose, or would I need to ...

Ways to identify when the scroll bar reaches the end of the modal dialog box

I have been working on a modal that should display an alert when the scrollbar reaches the bottom. Despite my efforts to research a solution, I am struggling to detect this specific event within the modal. The desired outcome is for an alert to pop up once ...