Community Forums › Forums › Archived Forums › Design Tips and Tricks › Adding Attributes to Widgets
Tagged: attribute sidebar
- This topic has 6 replies, 2 voices, and was last updated 8 years, 6 months ago by David Chu.
-
AuthorPosts
-
September 9, 2015 at 8:20 am #165028emmtreParticipant
I'm trying to add some schema.org microdata to a widgetized page for SEO purpose.
According to this article you should be able to use the genesis_attr filter as along as it is a valid HTML element with a class.
https://wpbeaches.com/adding-attribute-html-section-genesis/
Is it possible to apply genesis_attr to the class widget-area or do you have to add a context to the widget to be able to use?
//* Custom attributes for widget area add_filter( 'genesis_attr_widget-area', 'custom_widget_markup' ); function custom_widget_markup( $attributes ) { if ( is_home() ) { $attributes['itemscope'] = true; $attributes['itemtype'] = 'http://schema.org/CreativeWork'; } return $attributes; }
September 9, 2015 at 10:33 am #165036David ChuParticipantHi,
That's an interesting question. I tried your code and a couple variants, but didn't get it to work. As you may have seen, there is a spot in the code that uses a wildcard; it may work with further investigation.In the meantime, I was able to get something similar to what you're asking for, I think, applying it to the Primary Sidebar, and the secondary can also be done this way, as well as the footer widgets area.
I didn't set itemscope, as it seems to default to true, but it wouldn't hurt to have that. I also added a class, taking care to concatenate it, not just assign it.
Hope this helps,
Dave, aka Mr. AttributePS: great job on finding the funky schema parts in the Genesis update!
Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers
September 9, 2015 at 1:32 pm #165048David ChuParticipantHowdy,
I got curious about this, and cracked the code for quite a few "wildcard" attribute items.I had a hunch and tested it a bunch. [got to work that into some poem]. Since many types of markup items do seem to respond to a command like yours above, it occurred to me that if there's more than one on a page, the command throws up its hands, essentially, and doesn't work. widget-area may actually work if you just have one on a page!
There are some items that work great, such as site-inner, content, site-container, etc. Even nav-primary works because there's only one (unless you code your theme to have 2 of those, I've done that for submenu coding to have them both styled the same).
But wait, that's not all! It also works on some unique HTML elements, too, such as BODY and HEAD. Some things did not work even though unique, such as HTML and FOOTER. ARTICLE worked on a single page, but not on an archive page, which does make sense, as there are many ARTICLES on an archive page.
It also possible that regular WordPress filters may help, too. I know that stuff on the HTML tag can be dealt with that way.
Dave
Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers
September 10, 2015 at 11:59 am #165135emmtreParticipantThanx Dave for your comments. Where do you see a wildcard in the code? I think you only can add attributes via the genesis_attr filter if the element/section have a valid html or a context declared. So the list of context/html in the articles are the only elements where you can use the filter. But you should be able to declare the widget with the required html markup and context to be able to use the genesis_attr filter. Maybe like the primary sidebar structure is done in Genesis? Please see the second article. So the question is how to combine these to make the genesis_attr filter to work for the widget?
https://wpbeaches.com/adding-attribute-html-section-genesis/
http://www.rfmeier.net/using-genesis_markup-with-html5-in-genesis-2-0/
//* Register my-custom-widget area genesis_register_sidebar( array( 'id' => 'my-custom-widget', 'name' => __( 'My Custom Widget', 'child-theme' ), 'description' => __( 'This is the my custom widget area.', 'child-theme' ), ) );
//* Hook my-custom-widget widget area before content add_action( 'genesis_before_loop', 'hook_my_custom_widget' ); function hook_my_custom_widget() { if ( is_front_page() && ! is_paged() ) { genesis_widget_area( 'my-custom-widget', array( 'before' => '<div class="my-custom-widget widget-area">', 'after' => '</div>', ) ); } }
genesis_markup( array( 'html5' => '<section id="%1$s" class="widget %2$s"><div class="widget-wrap">', 'xhtml' => '<div id="%1$s" class="widget %2$s"><div class="widget-wrap">', 'context' => 'my-custom-widget', 'echo' => false, ) );
//* Output primary sidebar structure genesis_markup( array( 'html5' => '<aside %s>' . genesis_sidebar_title( 'sidebar' ), 'xhtml' => '<div id="sidebar" class="sidebar widget-area">', 'context' => 'sidebar-primary', ) ); do_action( 'genesis_before_sidebar_widget_area' ); do_action( 'genesis_sidebar' ); do_action( 'genesis_after_sidebar_widget_area' ); genesis_markup( array( 'html5' => '</aside>', 'xhtml' => '</div>', ) );
September 10, 2015 at 1:33 pm #165149David ChuParticipantHi,
Please keep in mind that I can't see your HTML code or site, so I'm only guessing exactly what you're looking for. ๐I'm afraid the ultimate answer is to study the code carefully in markup.php. I found what I call the "wildcard" in this function: genesis_parse_attr
Investigating and testing that will be instructive. The articles you cite do look legit (and similar). Because I used the wildcard bit I was able to stick attributes on things that aren't in their lists! ๐ But as I said, I could not get any attributes on anything with a non-unique class, or on repeating structures such as 10 ARTICLEs in an archive. I don't know right now, but I suspect this has something to do with array processing, and I don't have time to dig further right now.
I did just do a test with a widget that I coded myself, somewhat like what you have. I tested, but could not alter its attributes, either. So that might be a dead end.
The bottom line: markup.php, while very helpful, is limited.
If I were faced with a dead end in markup.php, and couldn't get the attribute I wanted, I'd "go native" and hand-code a template to make the code bend to my will. ๐
Dave
Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers
September 11, 2015 at 6:07 pm #165260emmtreParticipantI took the easy way out...
//* Hook welcome widget area before content add_action( 'genesis_before_loop', 'child_theme_welcome' ); function child_theme_welcome() { if ( is_front_page() && ! is_paged() ) { genesis_widget_area( 'welcome', array( 'before' => '<div class="welcome widget-area" itemscope itemtype="http://schema.org/CreativeWork">', 'after' => '</div>', ) ); } }
//* Register widget areas genesis_register_sidebar( array( 'id' => 'welcome', 'name' => __( 'Welcome', 'child_theme' ), 'description' => __( 'This is the welcome widget area.', 'child_theme' ), 'before_title' => '<h4 class="widget-title widgettitle" itemprop="headline">', ) );
<span itemprop="text">Content in text widget...</span>
September 12, 2015 at 8:43 am #165282David ChuParticipantEmmtre,
A very clever way around it. Nice job! It's a great example of how there are nearly always multiple solutions for such problems.Dave
Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.