Changeset 2641357
- Timestamp:
- 12/08/2021 04:13:46 PM (4 years ago)
- Location:
- thinker-sidebar-shortcode/tags/1.0.0
- Files:
-
- 2 edited
-
readme.txt (modified) (4 diffs)
-
thinker-sidebar-shortcode.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
thinker-sidebar-shortcode/tags/1.0.0/readme.txt
r1833032 r2641357 1 === Thinker Sidebar Shortcode === 1 === Sidebar Shortcode === 2 Contributors: thinkerwebdesign 2 3 Tags: sidebar shortcode, sidebar, shortcode, widget area, widget area shortcode, shortcode for sidebar, sidebar in page, sidebar in post, sidebar via shortcode, sidebar by shortcode, sidebar from shortcode, sidebar per shortcode 3 4 Donate link: http://www.thinkerwebdesign.com/thinker-sidebar-shortcode-plugin/ 4 5 Requires at least: 3.4+ 5 Tested up to: 4.96 Tested up to: 5.8 6 7 Stable tag: trunk 7 8 License: GPLv3 … … 20 21 21 22 **Method 1 - (Recommended Use: Add a sidebar using a sidebar Name.)** 23 22 24 A sidebar Name can be found in the `Appearance > Widgets` section of your WordPress Admin Area. 23 Example uses:24 [sidebar name="your-sidebar-name"]25 [sidebar name="your-sidebar-name" class="custom-class"]26 [sidebar name="your-sidebar-name" class="custom-class-1 custom-class-2 custom-class-3"]27 25 28 **Method 2 (Advanced WP Users: Add a sidebar using a sidebar ID.)** 29 A sidebar ID can be found in your theme`s register_sidebar functions, usually in the theme`s functions.php file. 30 Example uses: 31 [sidebar id="your-sidebar-id"] 32 [sidebar id="your-sidebar-id" class="custom-class"] 33 [sidebar id="your-sidebar-id" class="custom-class-1 custom-class-2 custom-class-3"] 26 *Example uses:* 27 28 * `[sidebar name="your-sidebar-name"]` 29 * `[sidebar name="your-sidebar-name" class="custom-class"]` 30 * `[sidebar name="your-sidebar-name" class="custom-class-1 custom-class-2 custom-class-3"]` 31 32 **Method 2 - (Advanced WP Users: Add a sidebar using a sidebar ID.)** 33 34 A sidebar ID can be found in your theme's `register_sidebar` functions, usually in the theme's `functions.php` file. 35 36 *Example uses:* 37 38 * `[sidebar id="your-sidebar-id"]` 39 * `[sidebar id="your-sidebar-id" class="custom-class"]` 40 * `[sidebar id="your-sidebar-id" class="custom-class-1 custom-class-2 custom-class-3"]` 34 41 35 42 **General Notes** 43 36 44 * The spelling and capitalization of a shortcode Name or ID must exactly match that of the desired sidebar. 37 45 * Definition of an active sidebar: An active sidebar is a sidebar that contains widgets. 38 * A [sidebar]shortcode without an active sidebar Name or active sidebar ID displays nothing.46 * A `[sidebar]` shortcode without an active sidebar Name or active sidebar ID displays nothing. 39 47 * An active sidebar ID overrides a sidebar Name if both are present in the same shortcode. 40 48 * There is no limit to the number of shortcodes that can be used on the same page or post. … … 42 50 43 51 **HTML Class Notes** 52 44 53 * Separate multiple custom classes using a space character. Examples shown in `Method` sections above. 45 54 * There is no limit to the number of custom classes that can be used. … … 69 78 = How can a sidebar ID be found? = 70 79 71 Advanced WP users can find sidebar IDs in the active theme `s register_sidebar functions, usually in the theme`s functions.phpfile.80 Advanced WP users can find sidebar IDs in the active theme's `register_sidebar` functions, usually in the theme's `functions.php` file. 72 81 See the `Method` sections and `Screenshots` section for details. 73 See also: https://codex.wordpress.org/Function_Reference/register_sidebar82 See also: [https://codex.wordpress.org/Function_Reference/register_sidebar](https://codex.wordpress.org/Function_Reference/register_sidebar) 74 83 75 84 = How many custom classes can be used? = -
thinker-sidebar-shortcode/tags/1.0.0/thinker-sidebar-shortcode.php
r1833032 r2641357 1 1 <?php 2 3 2 /** 4 3 * Thinker Sidebar Shortcode … … 11 10 * 12 11 * @wordpress-plugin 13 * Plugin Name: ThinkerSidebar Shortcode12 * Plugin Name: Sidebar Shortcode 14 13 * Plugin URI: http://www.thinkerwebdesign.com/thinker-sidebar-shortcode-plugin/ 15 14 * Description: Add sidebars to WP posts and pages via shortcodes. … … 45 44 * Contains the shortcode function and the action hook. 46 45 * 47 * @since 1.0.0 48 * @package Thinker_Sidebar_Shortcode 46 * @since 1.0.0 49 47 */ 50 48 class Thinker_Sidebar_Shortcode { … … 55 53 * Adds shortcode after WordPress has been initialized. 56 54 * 57 * @since 1.0.055 * @since 1.0.0 58 56 */ 59 57 public function run() { 60 61 58 add_action( 'init', array( $this, 'register_shortcode' ) ); 62 63 59 } 64 60 … … 68 64 * Adds a hook for the [sidebar] shortcode tag. 69 65 * 70 * @since 1.0.066 * @since 1.0.0 71 67 */ 72 68 public function register_shortcode() { 73 74 69 add_shortcode( 'sidebar', array( $this, 'shortcode' ) ); 75 76 70 } 77 71 … … 83 77 * If the shortcode matches a sidebar then verifies that the sidebar has widgets. 84 78 * Sanitizes the HTML classes and removes unneeded spaces before returning HTML. 85 * If neither ID nor Name exactly matches an active sidebar then returns n othing.79 * If neither ID nor Name exactly matches an active sidebar then returns null. 86 80 * 87 * @since 1.0.081 * @since 1.0.0 88 82 * 89 * @global array$wp_registered_sidebars83 * @global array $wp_registered_sidebars 90 84 * 91 * @param array $attsShortcode attributes.92 * @param string $content93 * @return string $htmlDisplays sidebar.85 * @param array $atts Shortcode attributes. 86 * @param array $content Shortcode content. Default empty. 87 * @return string Displays sidebar. 94 88 */ 95 public function shortcode( $atts, $content = null ) { 96 89 public function shortcode( $atts, $content = '' ) { 97 90 global $wp_registered_sidebars; 98 91 99 if ( isset ( $wp_registered_sidebars ) ) { 92 // Returns if no sidebars are registered or shortcode attributes are empty. 93 if ( ! is_dynamic_sidebar() || empty( $wp_registered_sidebars ) || empty( $atts ) ) { 94 return; 95 } 100 96 101 if ( is_array ( $atts ) ) { 97 // Sets default shortcode attributes if missing from shortcode. 98 $atts = shortcode_atts( 99 array( 100 'name' => '', 101 'id' => '', 102 'class' => '', 103 ), 104 $atts, 105 'sidebar' 106 ); 102 107 103 // Sets default shortcode attributes if missing from shortcode. 104 $atts = shortcode_atts( array( 105 'name' => '', 106 'id' => '', 107 'class' => '', 108 ), $atts, 'sidebar' ); 108 // Sets variables from shortcode attributes. 109 $name = $atts['name']; 110 $id = $atts['id']; 111 $class = $atts['class']; 109 112 110 // Sets name, id, and class variables from shortcode attributes. 111 $name = $atts['name']; 112 $id = $atts['id']; 113 $class = $atts['class']; 113 // Gets Sidebar ID. Gives precedent to ID over Name. 114 $sidebar_id = ''; 115 if ( $id && array_key_exists( $id, $wp_registered_sidebars ) ) { 116 $sidebar_id = $id; 117 } elseif ( $name ) { 118 foreach ( $wp_registered_sidebars as $sidebar ) { 114 119 115 $sidebar_id = ''; 116 117 // Checks if any registered sidebar has active widgets. 118 if ( is_dynamic_sidebar() ) { 119 120 // Gives precedent to ID over Name if shortcode ID exactly matches a sidebar. 121 if ( $id !='' && array_key_exists( $id, $wp_registered_sidebars ) ) { 122 123 $sidebar_id = $id; 124 125 } elseif ( $name !='' ) { 126 127 foreach ( $wp_registered_sidebars as $sidebar ) { 128 129 // If shortcode Name matches a sidebar then sets sidebar ID. 130 if ( $sidebar['name'] == $name ) { 131 132 $sidebar_id = $sidebar['id']; 133 134 } 135 136 } 137 138 } else { 139 140 return; 141 142 } 143 } 144 145 // Verifies that the shortcode matched a sidebar and the sidebar is active. 146 if ( $sidebar_id !='' && is_active_sidebar( $sidebar_id ) ) { 147 148 $class_out = 'sidebar-shortcode'; 149 150 // Sanitizes class by permitting only (letter,number,space,-,_) characters. 151 if ( !preg_match ( '/[^-_a-zA-Z0-9\pL]/u', $sidebar_id ) ) { 152 $class_out = $class_out . ' ' . $sidebar_id; 153 } 154 if ( $class !='' && !preg_match ( '/[^-_ a-zA-Z0-9\pL]/u', $class ) ) { 155 $class_out = $class_out . ' ' . $class; 156 } 157 158 // Removes leading, trailing, and extra spaces from HTML class attribute. 159 $class_out = trim( $class_out ); 160 $class_out = preg_replace( '/\s+/', ' ', $class_out ); 161 162 // Displays the Sidebar. 163 ob_start(); 164 dynamic_sidebar( $sidebar_id ); 165 $sidebar_out = ob_get_clean(); 166 $html = ' 167 <div class="' . $class_out . '" role="complementary"> 168 <div class="sidebar-shortcode-content"> 169 ' . $sidebar_out . ' 170 </div> 171 </div> 172 '; 173 return $html; 174 175 } else { 176 177 return; 178 120 // If shortcode Name matches a sidebar then sets sidebar ID. 121 if ( $sidebar['name'] === $name ) { 122 $sidebar_id = $sidebar['id']; 179 123 } 180 124 } 125 } 126 127 // Verifies that the shortcode matched a sidebar and the sidebar is active. 128 if ( $sidebar_id && is_active_sidebar( $sidebar_id ) ) { 129 130 // Sanitizes class by permitting only (letter,number,space,-,_) characters. 131 $class_out = 'sidebar-shortcode'; 132 if ( ! preg_match( '/[^-_a-zA-Z0-9\pL]/u', $sidebar_id ) ) { 133 $class_out = $class_out . ' ' . $sidebar_id; 134 } 135 if ( $class && ! preg_match( '/[^-_ a-zA-Z0-9\pL]/u', $class ) ) { 136 $class_out = $class_out . ' ' . $class; 137 } 138 139 // Removes leading, trailing, and extra spaces from HTML class attribute. 140 $class_out = trim( $class_out ); 141 $class_out = preg_replace( '/\s+/', ' ', $class_out ); 142 143 // Displays the Sidebar. 144 ob_start(); 145 dynamic_sidebar( $sidebar_id ); 146 $sidebar_out = ob_get_clean(); 147 $html = ' 148 <div class="' . $class_out . '" role="complementary"> 149 <div class="sidebar-shortcode-content"> 150 ' . $sidebar_out . ' 151 </div> 152 </div> 153 '; 154 return $html; 181 155 } 182 156 } … … 189 163 * registering the shortcode via the init action hook. 190 164 * 191 * @since 1.0.0165 * @since 1.0.0 192 166 */ 193 167 function run_thinker_sidebar_shortcode() { 194 195 168 $plugin = new Thinker_Sidebar_Shortcode(); 196 169 $plugin->run(); 197 198 170 } 199 171 run_thinker_sidebar_shortcode(); 200
Note: See TracChangeset
for help on using the changeset viewer.