FAQ. Author: anmari Version: 3.7 Author URI: http://webdesign.anmari.com */ add_action('in_widget_form', 'amr_spice_get_widget_id'); function amr_spice_get_widget_id($widget_instance) { echo "
To use as shortcode with id: "; if ($widget_instance->number=="__i__"){ echo "Save the widget first!
" ; } else { echo "[do_widget id=".$widget_instance->id. "]"; } } function amr_remove_widget_class($params) { // remove the widget classes if (!empty($params[0]['before_widget'])) { $params[0]['before_widget'] = str_replace ('"widget ','"',$params[0]['before_widget']); } if (!empty($params[0]['before_title'])) { $params[0]['before_title'] = $params[0]['before_title'] = str_replace ('widget-title','',$params[0]['before_title']); } return ($params); } function amr_do_widget_area($atts) { global $wp_registered_widgets, $_wp_sidebars_widgets, $wp_registered_sidebars; extract(shortcode_atts(array( 'widget_area' => 'widgets_for_shortcodes', 'class' => 'amr-widget-area', /* the widget class is picked up automatically. If we want to add an additional class at the wrap level to try to match a theme, use this */ 'widget_area_class' => '', /* option to disassociate from themes widget styling use =none*/ 'widget_classes' => '' /* option to disassociate from themes widget styling */ ), $atts)); if (!empty($atts)) { if (($widget_area == 'widgets_for_shortcodes' ) and !empty($atts[0])) $widget_area = $atts[0]; } if (empty ($wp_registered_sidebars[$widget_area])) { echo 'Error: Sidebar "'.$sidebar.'" with sidebarid "'.$sidebarid.'" is not defined.
'; // shouldnt happen - maybe someone running content filters on save else $output = 'Error: Sidebar "'.$sidebar.'" with sidebarid "'.$sidebarid.'" is empty (no widgets)
'; } } $output = ''; if (empty ($wid) or (!is_array($wid)) or (count($wid) < 1)) { $output = 'Error: Your requested Widget "'.$widget.'" is not in the "'.$sidebar.'" sidebar
'; $output .= amr_show_widget_debug('empty', $name, $id, $sidebar); unset($sidebar); unset($sidebarid); } else { /* There may only be one but if we have two in our chosen widget then it will do both */ $output = ''; foreach ($wid as $i=>$widget_instance) { ob_start(); /* catch the echo output, so we can control where it appears in the text */ amr_shortcode_sidebar($widget_instance, $sidebar, $title, $class, $wrap, $widget_classes); $output .= ob_get_clean(); } } return ($output); } function amr_shortcode_sidebar( $widget_id, $name="widgets_for_shortcode", $title=true, $class='', $wrap='', $widget_classes='') { /* This is basically the wordpress code, slightly modified */ global $wp_registered_sidebars, $wp_registered_widgets; $debug = amr_check_if_widget_debug(); $sidebarid = amr_get_sidebar_id ($name); $amr_sidebars_widgets = wp_get_sidebars_widgets(); //201711 do we need? $sidebar = $wp_registered_sidebars[$sidebarid]; // has the params etc $did_one = false; /* lifted from wordpress code, keep as similar as possible for now */ if ( !isset($wp_registered_widgets[$widget_id]) ) return; // wp had c o n t i n u e $params = array_merge( array( array_merge( $sidebar, array('widget_id' => $widget_id, 'widget_name' => $wp_registered_widgets[$widget_id]['name']) ) ), (array) $wp_registered_widgets[$widget_id]['params'] ); $validtitletags = array ('h1','h2','h3','h4','h5','header','strong','em'); $validwraptags = array ('div','p','main','aside','section'); if (!empty($wrap)) { /* then folks want to 'wrap' with their own html tag, or wrap = yes */ if ((!in_array( $wrap, $validwraptags))) $wrap = ''; /* To match a variety of themes, allow for a variety of html tags. */ /* May not need if our sidebar match attempt has worked */ } if (!empty ($wrap)) { $params[0]['before_widget'] = '<'.$wrap.' id="%1$s" class="%2$s">'; $params[0]['after_widget'] = ''.$wrap.'>'; } // wp code to get classname $classname_ = ''; //foreach ( (array) $wp_registered_widgets[$widget_id]['classname'] as $cn ) { $cn = $wp_registered_widgets[$widget_id]['classname']; if ( is_string($cn) ) $classname_ .= '_' . $cn; elseif ( is_object($cn) ) $classname_ .= '_' . get_class($cn); //} $classname_ = ltrim($classname_, '_'); // add MKM and others requested class in to the wp classname string // if no class specfied, then class will = amrwidget. These classes are so can reverse out unwanted widget styling. // $classname_ .= ' widget '; // wordpress seems to almost always adds the widget class $classname_ .= ' '.$class; // we are picking up the defaults from the thems sidebar ad they have registered heir sidebar to issue widget classes? // Substitute HTML id and class attributes into before_widget if (!empty($params[0]['before_widget'])) $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $widget_id, $classname_); else $params[0]['before_widget'] = ''; if (empty($params[0]['before_widget'])) $params[0]['after_widget'] = ''; $params = apply_filters( 'dynamic_sidebar_params', $params ); // allow, any pne usingmust ensure they apply to the correct sidebars if (!empty($title)) { if ($title=='false') { /* amr switch off the title html, still need to get rid of title separately */ $params[0]['before_title'] = ''; } else { if (in_array( $title, $validtitletags)) { $class = ' class="widget-title" '; $params[0]['before_title'] = '<'.$title.' '.$class.' >'; $params[0]['after_title'] = ''.$title.'>'; } } } if (!empty($widget_classes) and ($widget_classes == 'none') ) { $params = amr_remove_widget_class($params); // also called in widget area shortcode } $callback = $wp_registered_widgets[$widget_id]['callback']; if ( is_callable($callback) ) { call_user_func_array($callback, $params); $did_one = true; } // } return $did_one; } function amr_saw_setup_sidebar ($sidebars_widgets) { global $sidebars_widgets; //need? // now theme mods has record of widgets and sidebar, but not for our new one. if ( is_array( $sidebars_widgets ) ) { if (empty($sidebars_widgets['widgets_for_shortcodes'])) { $sidebars_widgets['widgets_for_shortcodes'] = array(); } } return $sidebars_widgets; } function amr_reg_sidebar() { // this is fired late, so hopefully any theme sidebars will have been registered already. global $wp_registered_widgets, $_wp_sidebars_widgets, $sidebars_widgets, $wp_registered_sidebars; if ( function_exists('register_sidebar') ) { // maybe later, get the first main sidebar and copy it's before/after etc $args = array( 'name' =>'Widgets for Shortcodes', 'id' => 'widgets_for_shortcodes', // hope to avoid losing widgets 'description' => __('Sidebar to hold widgets and their settings. These widgets will be used in a shortcode. This sidebars widgets should be saved with your theme settings now.','amr-shortcode-any-widget'), 'before_widget' => '', 'before_title' => '