This topic is: not resolved
-
Hi. A couple days ago I posted in the comments, and I was invited to continue this in this forum.
My question was if it’s possible to copy/paste VC shortcodes into a regular “text widget” (for a sidebar, footer, etc.) This can work, except shortcode padding, margins, borders, and backgrounds get ignored. In the comments I was told the following code put into functions.php will make it work:
add_filter('widget_text', 'dhvc_view_shortcode_parse_custom_css',0); function dhvc_view_shortcode_parse_custom_css2($content){ $css = ''; if ( preg_match( '/\s*(\.[^\{]+)\s*\{\s*([^\}]+)\s*\}\s*/', $content ) ) { WPBMap::addAllMappedShortcodes(); preg_match_all( '/' . get_shortcode_regex() . '/', $content, $shortcodes ); foreach ( $shortcodes[2] as $index => $tag ) { $shortcode = WPBMap::getShortCode( $tag ); $attr_array = shortcode_parse_atts( trim( $shortcodes[3][ $index ] ) ); if ( isset( $shortcode['params'] ) && ! empty( $shortcode['params'] ) ) { foreach ( $shortcode['params'] as $param ) { if ( 'css_editor' === $param['type'] && isset( $attr_array[ $param['param_name'] ] ) ) { $css .= $attr_array[ $param['param_name'] ]; } } } } foreach ( $shortcodes[5] as $shortcode_content ) { $css .= dhvc_view_shortcode_parse_custom_css2( $shortcode_content); } } return $css; } function dhvc_view_shortcode_parse_custom_css($content){ if(!class_exists('WPBMap')) return $content; $css = ''; if ( ! preg_match( '/\s*(\.[^\{]+)\s*\{\s*([^\}]+)\s*\}\s*/', $content ) ) { return $content; } WPBMap::addAllMappedShortcodes(); preg_match_all( '/' . get_shortcode_regex() . '/', $content, $shortcodes ); foreach ( $shortcodes[2] as $index => $tag ) { $shortcode = WPBMap::getShortCode( $tag ); $attr_array = shortcode_parse_atts( trim( $shortcodes[3][ $index ] ) ); if ( isset( $shortcode['params'] ) && ! empty( $shortcode['params'] ) ) { foreach ( $shortcode['params'] as $param ) { if ( 'css_editor' === $param['type'] && isset( $attr_array[ $param['param_name'] ] ) ) { $css .= $attr_array[ $param['param_name'] ]; } } } } foreach ( $shortcodes[5] as $shortcode_content ) { $css .= dhvc_view_shortcode_parse_custom_css2( $shortcode_content ); } return (!empty($css) ? '<style type="text/css">'.$css.'</style>' : '' ).$content; }
With this code, if one of my shortcodes contains any margins, borders, padding, or background, then the entire contents in the text widget will not display at all.
I was told in the comments that it should work, and if not then post in the forum. Thanks so much for any help with this!