Producing Unique Widget IDs for CSS
-
My ultimate goal is to make a unique widget that differs from the norm of my theme. However, my theme does not produce ids for the widget divs.
I was delighted to come across the following thread that shows how to edit functions.php so that widgets’ divs get a unique ID:
https://www.ads-software.com/support/topic/153242
However, the conditional if (function_exists(‘register_sidebars’)) in my functions.php uses a string that is replaced with the html in a later function:
if (function_exists('register_sidebars')) { register_sidebars(3, array( 'before_widget' => '<!--- BEGIN Widget --->', 'before_title' => '<!--- BEGIN WidgetTitle --->', 'after_title' => '<!--- END WidgetTitle --->', 'after_widget' => '<!--- END Widget --->' )); }
There are two functions that setup the manipulation of the strings in the condition above. The second function art_sidebar sets up the array with the string and its corresponding html code for the replace. Anyways, here are the two functions:
function art_normalize_widget_style_tokens($content) { $bw = '<!--- BEGIN Widget --->'; $bwt = '<!--- BEGIN WidgetTitle --->'; $ewt = '<!--- END WidgetTitle --->'; $bwc = '<!--- BEGIN WidgetContent --->'; $ewc = '<!--- END WidgetContent --->'; $ew = '<!--- END Widget --->'; $result = ''; $startBlock = 0; $endBlock = 0; while (true) { $startBlock = strpos($content, $bw, $endBlock); if (false === $startBlock) { $result .= substr($content, $endBlock); break; } $result .= substr($content, $endBlock, $startBlock - $endBlock); $endBlock = strpos($content, $ew, $startBlock); if (false === $endBlock) { $result .= substr($content, $endBlock); break; } $endBlock += strlen($ew); $widgetContent = substr($content, $startBlock, $endBlock - $startBlock); $beginTitlePos = strpos($widgetContent, $bwt); $endTitlePos = strpos($widgetContent, $ewt); if ((false == $beginTitlePos) xor (false == $endTitlePos)) { $widgetContent = str_replace($bwt, '', $widgetContent); $widgetContent = str_replace($ewt, '', $widgetContent); } else { $beginTitleText = $beginTitlePos + strlen($bwt); $titleContent = substr($widgetContent, $beginTitleText, $endTitlePos - $beginTitleText); if (' ' == $titleContent) { $widgetContent = substr($widgetContent, 0, $beginTitlePos) . substr($widgetContent, $endTitlePos + strlen($ewt)); } } if (false === strpos($widgetContent, $bwt)) { $widgetContent = str_replace($bw, $bw . $bwc, $widgetContent); } else { $widgetContent = str_replace($ewt, $ewt . $bwc, $widgetContent); } $result .= str_replace($ew, $ewc . $ew, $widgetContent); } return $result; } function art_sidebar($index = 1) { if (!function_exists('dynamic_sidebar')) return false; ob_start(); $success = dynamic_sidebar($index); $content = ob_get_clean(); if (!$success) return false; $content = art_normalize_widget_style_tokens($content); $replaces = array( '<!--- BEGIN Widget --->' => "<div class=\"Block\">\r\n <div class=\"Block-body\">\r\n", '<!--- BEGIN WidgetTitle --->' => "<div class=\"BlockHeader\">\r\n <div class=\"header-tag-icon\">\r\n <div class=\"BlockHeader-text\">\r\n", '<!--- END WidgetTitle --->' => "\r\n </div>\r\n </div>\r\n <div class=\"l\"></div>\r\n <div class=\"r\"><div></div></div>\r\n</div>\r\n", '<!--- BEGIN WidgetContent --->' => "<div class=\"BlockContent\">\r\n <div class=\"BlockContent-tl\"></div>\r\n <div class=\"BlockContent-tr\"><div></div></div>\r\n <div class=\"BlockContent-bl\"><div></div></div>\r\n <div class=\"BlockContent-br\"><div></div></div>\r\n <div class=\"BlockContent-tc\"><div></div></div>\r\n <div class=\"BlockContent-bc\"><div></div></div>\r\n <div class=\"BlockContent-cl\"><div></div></div>\r\n <div class=\"BlockContent-cr\"><div></div></div>\r\n <div class=\"BlockContent-cc\"></div>\r\n <div class=\"BlockContent-body\" id=\"".$idGen."\">\r\n", '<!--- END WidgetContent --->' => "\r\n </div>\r\n</div>\r\n", '<!--- END Widget --->' => "\r\n </div>\r\n</div>\r\n" ); $bwt = '<!--- BEGIN WidgetTitle --->'; $ewt = '<!--- END WidgetTitle --->'; if ('' == $replaces[$bwt] && '' == $replaces[$ewt]) { $startTitle = 0; $endTitle = 0; $result = ''; while (true) { $startTitle = strpos($content, $bwt, $endTitle); if (false == $startTitle) { $result .= substr($content, $endTitle); break; } $result .= substr($content, $endTitle, $startTitle - $endTitle); $endTitle = strpos($content, $ewt, $startTitle); if (false == $endTitle) { $result .= substr($content, $startTitle); break; } $endTitle += strlen($ewt); } $content = $result; } $content = str_replace(array_keys($replaces), array_values($replaces), $content); echo $content; return true; }
As you can probably already tell I know enough about programming concepts to kind of know what is going on, but I don’t understand enough about how WordPress operates (yet!)
Thank you for taking the time to read this and let me know if I need to post additional code or need to clarify!
- The topic ‘Producing Unique Widget IDs for CSS’ is closed to new replies.