Shortcode in Menus - Version 3.0

Version Description

  • Removed the error trigger on the FULL HTML OUTPUT usage
  • Added the feature to use shortcodes in titles of menu items as well(works with all types of menu items)
  • Resolved the PHP Notice, popping up in the error log while adding new shortcodes
Download this release

Release Info

Developer gagan0123
Plugin Icon 128x128 Shortcode in Menus
Version 3.0
Comparing to
See all releases

Code changes from version 2.1 to 3.0

Files changed (3) hide show
  1. index.php +348 -290
  2. js/admin.js +71 -75
  3. readme.txt +8 -3
index.php CHANGED
@@ -3,297 +3,355 @@
3
  Plugin Name: Shortcodes in Menus
4
  Description: Allows you to add shortcodes in WordPress Navigation Menus
5
  Plugin URI: http://wordpress.org/plugins/shortcode-in-menus/
6
- Version: 2.1
7
- Author URI: http://gagan.pro
8
- Author: Gagan Deep Singh
9
  */
10
 
11
- if (!defined('ABSPATH'))
12
- exit; // Exit if accessed directly
13
-
14
- if (!class_exists('gsShortCodeInMenu')) {
15
-
16
- class gsShortCodeInMenu {
17
-
18
- /**
19
- * Hooks, filters and registers everything appropriately
20
- */
21
- public function init() {
22
-
23
- // register a test shortcode for testing
24
- add_shortcode('gs_test_shortcode', array($this, 'shortcode'));
25
-
26
- // setup the meta box
27
- add_action('admin_init', array($this, 'setup_meta_box'));
28
-
29
- // filter the menu item output on frontend
30
- add_filter('walker_nav_menu_start_el', array($this, 'start_el'), 10, 2);
31
-
32
- // filter the menu item before display in admin
33
- add_filter('wp_setup_nav_menu_item', array($this, 'setup_item'), 10, 1);
34
-
35
- // enqueue custom js
36
- add_action('admin_enqueue_scripts', array($this, 'enqueue'));
37
-
38
- // add an ajax hack to save the html content
39
- add_action('wp_ajax_gs_sim_description_hack', array($this, 'description_hack'));
40
-
41
- // hook to allow saving of shortcode in custom link metabox for legacy support
42
- add_action('wp_loaded', array($this, 'security_check'));
43
-
44
- // filter the output when shortcode is saved using custom links, for legacy support
45
- add_filter('clean_url', array($this, 'display_shortcode'), 1, 3);
46
- }
47
-
48
- /**
49
- * Test shortcode. Output's the developer's url
50
- *
51
- * @return string
52
- */
53
- public function shortcode() {
54
- return "http://gagan.pro";
55
- }
56
-
57
- /**
58
- * Gets a new object id,given the current one
59
- *
60
- * @param int $last_object_id The current/last object id
61
- * @return int
62
- */
63
- public function new_object_id($last_object_id) {
64
-
65
- // make sure it's an integer
66
- $object_id = (int) $last_object_id;
67
-
68
- // increment it
69
- $object_id++;
70
-
71
- // if object_id was 0 to start off with, make it 1
72
- $object_id = ($object_id < 1) ? 1 : $object_id;
73
-
74
- // save into the options table
75
- update_option('gs_sim_last_object_id', $object_id);
76
-
77
- return $object_id;
78
- }
79
-
80
- /**
81
- * Register our custom meta box
82
- */
83
- public function setup_meta_box() {
84
- add_meta_box('add-shortcode-section', __('Shortcode'), array($this, 'meta_box'), 'nav-menus', 'side', 'default');
85
- }
86
-
87
- /**
88
- * Display our custom meta box
89
- * @global int $_nav_menu_placeholder A placeholder index for the menu item
90
- * @global int|string $nav_menu_selected_id (id, name or slug) of the currently-selected menu
91
- */
92
- public function meta_box() {
93
- global $_nav_menu_placeholder, $nav_menu_selected_id;
94
-
95
- $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
96
-
97
- $last_object_id = get_option('gs_sim_last_object_id', 0);
98
- $object_id = $this->new_object_id($last_object_id);
99
- ?>
100
- <div class="gs-sim-div" id="gs-sim-div">
101
- <input type="hidden" class="menu-item-db-id" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-db-id]" value="0" />
102
- <input type="hidden" class="menu-item-object-id" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-object-id]" value="<?php echo $object_id; ?>" />
103
- <input type="hidden" class="menu-item-object" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-object]" value="gs_sim" />
104
- <input type="hidden" class="menu-item-type" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-type]" value="gs_sim" />
105
- <input type="hidden" id="gs-sim-description-nonce" value="<?php echo wp_create_nonce('gs-sim-description-nonce') ?>" />
106
- <p id="menu-item-title-wrap">
107
- <label for="gs-sim-title"><?php _e('Title'); ?></label>
108
- <input id="gs-sim-title" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-title]" type="text" class="regular-text menu-item-textbox" title="<?php esc_attr_e('Title'); ?>" style="width:100%" />
109
- </p>
110
-
111
- <p id="menu-item-html-wrap">
112
- <textarea style="width:100%;" rows="9" id="gs-sim-html" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-description]" class="code menu-item-textbox" title="<?php esc_attr_e('Text/html/shortcode here!'); ?>"></textarea>
113
- </p>
114
-
115
- <p class="button-controls">
116
- <span class="add-to-menu">
117
- <input type="submit"<?php wp_nav_menu_disabled_check($nav_menu_selected_id); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e('Add to Menu'); ?>" name="add-gs-sim-menu-item" id="submit-gs-sim" />
118
- <span class="spinner"></span>
119
- </span>
120
- </p>
121
-
122
- </div>
123
- <?php
124
- }
125
-
126
- /**
127
- * Check if the passed content has any shortcode. Inspired from the core's has_shortcode
128
- *
129
- * @param string $content The content to check for shortcode
130
- * @return boolean
131
- */
132
- function has_shortcode($content) {
133
-
134
- if (false != strpos($content, '[')) {
135
-
136
- preg_match_all('/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER);
137
-
138
- if (!empty($matches)) {
139
- return true;
140
- }
141
- }
142
- return false;
143
- }
144
-
145
- /**
146
- * Modifies the menu item display on frontend
147
- *
148
- * @param string $item_output The original html.
149
- * @param object $item The menu item being displayed.
150
- * @return object
151
- */
152
- public function start_el($item_output, $item) {
153
- // if it isn't our custom object
154
- if ($item->object != 'gs_sim') {
155
-
156
- // check the legacy hack
157
- if ($item->post_title == 'FULL HTML OUTPUT') {
158
-
159
- // trigger notice for deprecation
160
- trigger_error('Using Custom Links is deprecated.'
161
- . ' Use the new Shortcode box on the Menu Editor.');
162
-
163
- // then just process as we used to
164
- $item_output = do_shortcode($item->url);
165
- }
166
-
167
- // if it is our object
168
- } else {
169
- // just process it
170
- $item_output = stripslashes(do_shortcode($item->description));
171
-
172
- }
173
-
174
- return $item_output;
175
- }
176
-
177
- /**
178
- * Modify the menu item before display on Menu editor
179
- *
180
- * @param object $item The menu item
181
- * @return object
182
- */
183
- public function setup_item($item) {
184
-
185
- // only if it is our object
186
- if ($item->object == 'gs_sim') {
187
-
188
- // setup our label
189
- $item->type_label = __('Shortcode', 'gs_sim');
190
-
191
- if($item->post_content!= ''){
192
- $item->description = $item->post_content;
193
- }else{
194
-
195
- // set up the description from the transient
196
- $item->description = get_transient('gs_sim_description_hack_' . $item->object_id);
197
-
198
- // discard the transient
199
- delete_transient('gs_sim_description_hack_' . $item->object_id);
200
- }
201
- }
202
- return $item;
203
- }
204
-
205
- /**
206
- * Enqueue our custom js
207
- *
208
- * @param string $hook The current screen
209
- * @return null
210
- */
211
- public function enqueue($hook) {
212
-
213
- // don't enqueue if it isn't the menu editor
214
- if ('nav-menus.php' != $hook)
215
- return;
216
-
217
- // otherwise enqueue with nav-menu.js as a dependency so that our script is loaded after it
218
- wp_enqueue_script(
219
- 'gs-sim-admin', plugins_url('/js/admin.min.js', __FILE__), array('nav-menu')
220
- );
221
- }
222
-
223
- /**
224
- * An ajax based workaround to save descriptions without using the custom object type
225
- */
226
- public function description_hack() {
227
- // verify the nonce
228
- $nonce = $_POST['description-nonce'];
229
- if (!wp_verify_nonce($nonce, 'gs-sim-description-nonce')) {
230
- die();
231
- }
232
-
233
- // get the menu item
234
- $item = $_POST['menu-item'];
235
-
236
- // save the description in a transient. This is what we'll use in setup_item()
237
- set_transient('gs_sim_description_hack_' . $item['menu-item-object-id'], $item['menu-item-description']);
238
-
239
- // increment the object id, so it can be used by js
240
- $object_id = $this->new_object_id($item['menu-item-object-id']);
241
-
242
- echo $object_id;
243
-
244
- die();
245
- }
246
-
247
- /**
248
- * Legacy method to allow saving of shortcodes in custom_link url
249
- *
250
- * @deprecated since 2.0
251
- *
252
- * @param string $url The processed url for displaying/saving
253
- * @param string $orig_url The url that was submitted, retreived
254
- * @param string $context Whether saving or displaying
255
- * @return string
256
- */
257
- public function save_shortcode($url, $orig_url, $context) {
258
-
259
- if ($context == 'db') {
260
- return $orig_url;
261
- }
262
- return $url;
263
- }
264
-
265
- /**
266
- * Allows shortcodes into the custom link url field
267
- *
268
- * @deprecated since 2.0
269
- */
270
- public function security_check() {
271
- if (current_user_can('activate_plugins')) {
272
- //Conditionally adding the function for database context for
273
- add_filter('clean_url', array($this, 'save_shortcode'), 99, 3);
274
- }
275
- }
276
-
277
- /**
278
- * Allows shortcode to be processed and displayed
279
- *
280
- * @deprecated since 2.0
281
- *
282
- * @param string $url The processed url for displaying/saving
283
- * @param string $orig_url The url that was submitted, retreived
284
- * @param string $context Whether saving or displaying
285
- * @return string
286
- */
287
- public function display_shortcode($url, $orig_url, $context) {
288
- if ($context == 'display') {
289
- return do_shortcode($orig_url);
290
- }
291
- return $url;
292
- }
293
-
294
- }
295
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
  }
297
 
298
- $gs_sim = new gsShortCodeInMenu();
299
- $gs_sim_init = $gs_sim->init();
3
  Plugin Name: Shortcodes in Menus
4
  Description: Allows you to add shortcodes in WordPress Navigation Menus
5
  Plugin URI: http://wordpress.org/plugins/shortcode-in-menus/
6
+ Version: 3.0
7
+ Author: <a href="http://gagan.pro">Gagan Deep Singh</a> and <a href="http://hookrefineandtinker.com">Saurabh Shukla</a>
 
8
  */
9
 
10
+ if ( !defined( 'ABSPATH' ) )
11
+ exit; // Exit if accessed directly
12
+
13
+ if ( !class_exists( 'gsShortCodeInMenu' ) ) {
14
+
15
+ class gsShortCodeInMenu {
16
+
17
+ /**
18
+ * Hooks, filters and registers everything appropriately
19
+ */
20
+ public function init() {
21
+
22
+ // register a test shortcode for testing
23
+ add_shortcode( 'gs_test_shortcode', array( $this, 'shortcode' ) );
24
+
25
+ // setup the meta box
26
+ add_action( 'admin_init', array( $this, 'setup_meta_box' ) );
27
+
28
+ // filter the menu item output on frontend
29
+ add_filter( 'walker_nav_menu_start_el', array( $this, 'start_el' ), 10, 2 );
30
+
31
+ // filter the menu item before display in admin
32
+ add_filter( 'wp_setup_nav_menu_item', array( $this, 'setup_item' ), 10, 1 );
33
+
34
+ // enqueue custom js
35
+ add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
36
+
37
+ // add an ajax hack to save the html content
38
+ add_action( 'wp_ajax_gs_sim_description_hack', array( $this, 'description_hack' ) );
39
+
40
+ // hook to allow saving of shortcode in custom link metabox for legacy support
41
+ add_action( 'wp_loaded', array( $this, 'security_check' ) );
42
+
43
+ // filter the output when shortcode is saved using custom links, for legacy support
44
+ add_filter( 'clean_url', array( $this, 'display_shortcode' ), 1, 3 );
45
+
46
+ add_action( 'wp_ajax_add-menu-item', array( $this, 'ajax_add_menu_item' ), 0 );
47
+ }
48
+
49
+ /**
50
+ * Test shortcode. Output's the developer's url
51
+ *
52
+ * @return string
53
+ */
54
+ public function shortcode() {
55
+ return "http://gagan.pro";
56
+ }
57
+
58
+ /**
59
+ * Gets a new object id,given the current one
60
+ *
61
+ * @param int $last_object_id The current/last object id
62
+ * @return int
63
+ */
64
+ public function new_object_id( $last_object_id ) {
65
+
66
+ // make sure it's an integer
67
+ $object_id = (int) $last_object_id;
68
+
69
+ // increment it
70
+ $object_id++;
71
+
72
+ // if object_id was 0 to start off with, make it 1
73
+ $object_id = ($object_id < 1) ? 1 : $object_id;
74
+
75
+ // save into the options table
76
+ update_option( 'gs_sim_last_object_id', $object_id );
77
+
78
+ return $object_id;
79
+ }
80
+
81
+ /**
82
+ * Register our custom meta box
83
+ */
84
+ public function setup_meta_box() {
85
+ add_meta_box( 'add-shortcode-section', __( 'Shortcode' ), array( $this, 'meta_box' ), 'nav-menus', 'side', 'default' );
86
+ }
87
+
88
+ /**
89
+ * Display our custom meta box
90
+ * @global int $_nav_menu_placeholder A placeholder index for the menu item
91
+ * @global int|string $nav_menu_selected_id (id, name or slug) of the currently-selected menu
92
+ */
93
+ public function meta_box() {
94
+ global $_nav_menu_placeholder, $nav_menu_selected_id;
95
+
96
+ $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
97
+
98
+ $last_object_id = get_option( 'gs_sim_last_object_id', 0 );
99
+ $object_id = $this->new_object_id( $last_object_id );
100
+ ?>
101
+ <div class="gs-sim-div" id="gs-sim-div">
102
+ <input type="hidden" class="menu-item-db-id" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-db-id]" value="0" />
103
+ <input type="hidden" class="menu-item-object-id" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-object-id]" value="<?php echo $object_id; ?>" />
104
+ <input type="hidden" class="menu-item-object" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-object]" value="gs_sim" />
105
+ <input type="hidden" class="menu-item-type" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-type]" value="gs_sim" />
106
+ <input type="hidden" id="gs-sim-description-nonce" value="<?php echo wp_create_nonce( 'gs-sim-description-nonce' ) ?>" />
107
+ <p id="menu-item-title-wrap">
108
+ <label for="gs-sim-title"><?php _e( 'Title' ); ?></label>
109
+ <input id="gs-sim-title" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-title]" type="text" class="regular-text menu-item-textbox" title="<?php esc_attr_e( 'Title' ); ?>" style="width:100%" />
110
+ </p>
111
+
112
+ <p id="menu-item-html-wrap">
113
+ <textarea style="width:100%;" rows="9" id="gs-sim-html" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-description]" class="code menu-item-textbox" title="<?php esc_attr_e( 'Text/html/shortcode here!' ); ?>"></textarea>
114
+ </p>
115
+
116
+ <p class="button-controls">
117
+ <span class="add-to-menu">
118
+ <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-gs-sim-menu-item" id="submit-gs-sim" />
119
+ <span class="spinner"></span>
120
+ </span>
121
+ </p>
122
+
123
+ </div>
124
+ <?php
125
+ }
126
+
127
+ /**
128
+ * Modifies the menu item display on frontend
129
+ *
130
+ * @param string $item_output The original html.
131
+ * @param object $item The menu item being displayed.
132
+ * @return object
133
+ */
134
+ public function start_el( $item_output, $item ) {
135
+ // if it isn't our custom object
136
+ if ( $item->object != 'gs_sim' ) {
137
+
138
+ // check the legacy hack
139
+ if ( $item->post_title == 'FULL HTML OUTPUT' ) {
140
+
141
+ // then just process as we used to
142
+ $item_output = do_shortcode( $item->url );
143
+ } else {
144
+ $item_output = do_shortcode( $item_output );
145
+ }
146
+
147
+ // if it is our object
148
+ } else {
149
+ // just process it
150
+ $item_output = do_shortcode( $item->description );
151
+ }
152
+
153
+ return $item_output;
154
+ }
155
+
156
+ /**
157
+ * Modify the menu item before display on Menu editor
158
+ *
159
+ * @param object $item The menu item
160
+ * @return object
161
+ */
162
+ public function setup_item( $item ) {
163
+ if ( !is_object( $item ) )
164
+ return $item;
165
+
166
+ // only if it is our object
167
+ if ( $item->object == 'gs_sim' ) {
168
+
169
+ // setup our label
170
+ $item->type_label = __( 'Shortcode', 'gs_sim' );
171
+
172
+ if ( $item->post_content != '' ) {
173
+ $item->description = $item->post_content;
174
+ } else {
175
+
176
+ // set up the description from the transient
177
+ $item->description = get_transient( 'gs_sim_description_hack_' . $item->object_id );
178
+
179
+ // discard the transient
180
+ delete_transient( 'gs_sim_description_hack_' . $item->object_id );
181
+ }
182
+ }
183
+ return $item;
184
+ }
185
+
186
+ /**
187
+ * Enqueue our custom js
188
+ *
189
+ * @param string $hook The current screen
190
+ * @return null
191
+ */
192
+ public function enqueue( $hook ) {
193
+
194
+ // don't enqueue if it isn't the menu editor
195
+ if ( 'nav-menus.php' != $hook )
196
+ return;
197
+
198
+ // otherwise enqueue with nav-menu.js as a dependency so that our script is loaded after it
199
+ wp_enqueue_script(
200
+ 'gs-sim-admin', plugins_url( '/js/admin.js', __FILE__ ), array( 'nav-menu' )
201
+ );
202
+ }
203
+
204
+ /**
205
+ * An ajax based workaround to save descriptions without using the custom object type
206
+ */
207
+ public function description_hack() {
208
+ // verify the nonce
209
+ $nonce = $_POST[ 'description-nonce' ];
210
+ if ( !wp_verify_nonce( $nonce, 'gs-sim-description-nonce' ) ) {
211
+ die();
212
+ }
213
+
214
+ // get the menu item
215
+ $item = $_POST[ 'menu-item' ];
216
+
217
+ // save the description in a transient. This is what we'll use in setup_item()
218
+ set_transient( 'gs_sim_description_hack_' . $item[ 'menu-item-object-id' ], $item[ 'menu-item-description' ] );
219
+
220
+ // increment the object id, so it can be used by js
221
+ $object_id = $this->new_object_id( $item[ 'menu-item-object-id' ] );
222
+
223
+ echo $object_id;
224
+
225
+ die();
226
+ }
227
+
228
+ /**
229
+ * Legacy method to allow saving of shortcodes in custom_link url
230
+ *
231
+ * @deprecated since 2.0
232
+ *
233
+ * @param string $url The processed url for displaying/saving
234
+ * @param string $orig_url The url that was submitted, retreived
235
+ * @param string $context Whether saving or displaying
236
+ * @return string
237
+ */
238
+ public function save_shortcode( $url, $orig_url, $context ) {
239
+
240
+ if ( $context == 'db' ) {
241
+ return $orig_url;
242
+ }
243
+ return $url;
244
+ }
245
+
246
+ /**
247
+ * Allows shortcodes into the custom link url field
248
+ *
249
+ * @deprecated since 2.0
250
+ */
251
+ public function security_check() {
252
+ if ( current_user_can( 'activate_plugins' ) ) {
253
+ //Conditionally adding the function for database context for
254
+ add_filter( 'clean_url', array( $this, 'save_shortcode' ), 99, 3 );
255
+ }
256
+ }
257
+
258
+ /**
259
+ * Allows shortcode to be processed and displayed
260
+ *
261
+ * @deprecated since 2.0
262
+ *
263
+ * @param string $url The processed url for displaying/saving
264
+ * @param string $orig_url The url that was submitted, retreived
265
+ * @param string $context Whether saving or displaying
266
+ * @return string
267
+ */
268
+ public function display_shortcode( $url, $orig_url, $context ) {
269
+ if ( $context == 'display' ) {
270
+ return do_shortcode( $orig_url );
271
+ }
272
+ return $url;
273
+ }
274
+
275
+ /**
276
+ * Ajax handler for add menu item request
277
+ */
278
+ public function ajax_add_menu_item() {
279
+
280
+ check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' );
281
+
282
+ if ( !current_user_can( 'edit_theme_options' ) )
283
+ wp_die( -1 );
284
+
285
+ require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
286
+
287
+ // For performance reasons, we omit some object properties from the checklist.
288
+ // The following is a hacky way to restore them when adding non-custom items.
289
+
290
+ $menu_items_data = array();
291
+ foreach ( (array) $_POST[ 'menu-item' ] as $menu_item_data ) {
292
+ if (
293
+ !empty( $menu_item_data[ 'menu-item-type' ] ) &&
294
+ 'custom' != $menu_item_data[ 'menu-item-type' ] &&
295
+ 'gs_sim' != $menu_item_data[ 'menu-item-type' ] &&
296
+ !empty( $menu_item_data[ 'menu-item-object-id' ] )
297
+ ) {
298
+ switch ( $menu_item_data[ 'menu-item-type' ] ) {
299
+ case 'post_type' :
300
+ $_object = get_post( $menu_item_data[ 'menu-item-object-id' ] );
301
+ break;
302
+
303
+ case 'taxonomy' :
304
+ $_object = get_term( $menu_item_data[ 'menu-item-object-id' ], $menu_item_data[ 'menu-item-object' ] );
305
+ break;
306
+ }
307
+
308
+ $_menu_items = array_map( 'wp_setup_nav_menu_item', array( $_object ) );
309
+ $_menu_item = reset( $_menu_items );
310
+
311
+ // Restore the missing menu item properties
312
+ $menu_item_data[ 'menu-item-description' ] = $_menu_item->description;
313
+ }
314
+
315
+ $menu_items_data[] = $menu_item_data;
316
+ }
317
+
318
+ $item_ids = wp_save_nav_menu_items( 0, $menu_items_data );
319
+ if ( is_wp_error( $item_ids ) )
320
+ wp_die( 0 );
321
+
322
+ $menu_items = array();
323
+
324
+ foreach ( (array) $item_ids as $menu_item_id ) {
325
+ $menu_obj = get_post( $menu_item_id );
326
+ if ( !empty( $menu_obj->ID ) ) {
327
+ $menu_obj = wp_setup_nav_menu_item( $menu_obj );
328
+ $menu_obj->label = $menu_obj->title; // don't show "(pending)" in ajax-added items
329
+ $menu_items[] = $menu_obj;
330
+ }
331
+ }
332
+
333
+ /** This filter is documented in wp-admin/includes/nav-menu.php */
334
+ $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $_POST[ 'menu' ] );
335
+
336
+ if ( !class_exists( $walker_class_name ) )
337
+ wp_die( 0 );
338
+
339
+ if ( !empty( $menu_items ) ) {
340
+ $args = array(
341
+ 'after' => '',
342
+ 'before' => '',
343
+ 'link_after' => '',
344
+ 'link_before' => '',
345
+ 'walker' => new $walker_class_name,
346
+ );
347
+ echo walk_nav_menu_tree( $menu_items, 0, (object) $args );
348
+ }
349
+ wp_die();
350
+ }
351
+
352
+ }
353
+
354
+ $gs_sim = new gsShortCodeInMenu();
355
+ $gs_sim_init = $gs_sim->init();
356
  }
357
 
 
 
js/admin.js CHANGED
@@ -1,79 +1,75 @@
1
- jQuery('document').ready(function() {
2
-
3
- jQuery('#submit-gs-sim').on('click', function(e) {
4
- // call registerChange like any add
5
- wpNavMenu.registerChange();
6
-
7
- // call our custom function
8
- gsSimAddWidgettoMenu();
9
- });
10
-
11
- /**
12
- * Add our custom Shortcode object to Menu
13
- *
14
- * @returns {Boolean}
15
- */
16
- function gsSimAddWidgettoMenu( ) {
17
-
18
- // get the description
19
- description = jQuery('#gs-sim-html').val();
20
-
21
- // initialise object
22
- menuItems = {};
23
-
24
- // the usual method for ading menu Item
25
- processMethod = wpNavMenu.addMenuItemToBottom;
26
-
27
- var t = jQuery('.gs-sim-div');
28
-
29
- // Show the ajax spinner
30
- t.find('.spinner').show();
31
-
32
- // regex to get the index
33
- re = /menu-item\[([^\]]*)/;
34
-
35
- m = t.find('.menu-item-db-id');
36
- // match and get the index
37
- listItemDBIDMatch = re.exec(m.attr('name')),
38
- listItemDBID = 'undefined' == typeof listItemDBIDMatch[1] ? 0 : parseInt(listItemDBIDMatch[1], 10);
39
-
40
- // assign data
41
- menuItems[listItemDBID] = t.getItemData('add-menu-item', listItemDBID);
42
- menuItems[listItemDBID]['menu-item-description'] = description;
43
-
44
- if(menuItems[listItemDBID]['menu-item-title'] === ''){
45
- menuItems[listItemDBID]['menu-item-title'] = '(Untitled)';
46
- }
47
-
48
- // get our custom nonce
49
- nonce = jQuery('#gs-sim-description-nonce').val();
50
-
51
- // set up params for our ajax hack
52
- params = {
53
- 'action': 'gs_sim_description_hack',
54
- 'description-nonce': nonce,
55
- 'menu-item': menuItems[listItemDBID]
56
- };
57
-
58
- // call it
59
- jQuery.post(ajaxurl, params, function(objectId) {
60
-
61
- // returns the incremented object id, add to ui
62
- jQuery('#gs-sim-div .menu-item-object-id').val(objectId);
63
-
64
- // now call the ususl addItemToMenu
65
- wpNavMenu.addItemToMenu(menuItems, processMethod, function() {
66
- // Deselect the items and hide the ajax spinner
67
- t.find('.spinner').hide();
68
- // Set form back to defaults
69
- jQuery('#gs-sim-title').val('').blur();
70
- jQuery('#gs-sim-html').val('');
71
-
72
- });
73
-
74
- });
75
 
 
 
 
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  }
78
 
79
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ jQuery( 'document' ).ready( function () {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ jQuery( '#submit-gs-sim' ).on( 'click', function ( e ) {
4
+ // call registerChange like any add
5
+ wpNavMenu.registerChange();
6
 
7
+ // call our custom function
8
+ gsSimAddWidgettoMenu();
9
+ } );
10
+
11
+ /**
12
+ * Add our custom Shortcode object to Menu
13
+ *
14
+ * @returns {Boolean}
15
+ */
16
+ function gsSimAddWidgettoMenu( ) {
17
+
18
+ // get the description
19
+ description = jQuery( '#gs-sim-html' ).val();
20
+
21
+ // initialise object
22
+ menuItems = { };
23
+
24
+ // the usual method for ading menu Item
25
+ processMethod = wpNavMenu.addMenuItemToBottom;
26
+
27
+ var t = jQuery( '.gs-sim-div' );
28
+
29
+ // Show the ajax spinner
30
+ t.find( '.spinner' ).show();
31
+
32
+ // regex to get the index
33
+ re = /menu-item\[([^\]]*)/;
34
+
35
+ m = t.find( '.menu-item-db-id' );
36
+ // match and get the index
37
+ listItemDBIDMatch = re.exec( m.attr( 'name' ) ),
38
+ listItemDBID = 'undefined' == typeof listItemDBIDMatch[1] ? 0 : parseInt( listItemDBIDMatch[1], 10 );
39
+
40
+ // assign data
41
+ menuItems[listItemDBID] = t.getItemData( 'add-menu-item', listItemDBID );
42
+ menuItems[listItemDBID]['menu-item-description'] = description;
43
+
44
+ if ( menuItems[listItemDBID]['menu-item-title'] === '' ) {
45
+ menuItems[listItemDBID]['menu-item-title'] = '(Untitled)';
46
  }
47
 
48
+ // get our custom nonce
49
+ nonce = jQuery( '#gs-sim-description-nonce' ).val();
50
+
51
+ // set up params for our ajax hack
52
+ params = {
53
+ 'action': 'gs_sim_description_hack',
54
+ 'description-nonce': nonce,
55
+ 'menu-item': menuItems[listItemDBID]
56
+ };
57
+
58
+ // call it
59
+ jQuery.post( ajaxurl, params, function ( objectId ) {
60
+
61
+ // returns the incremented object id, add to ui
62
+ jQuery( '#gs-sim-div .menu-item-object-id' ).val( objectId );
63
+
64
+ // now call the ususl addItemToMenu
65
+ wpNavMenu.addItemToMenu( menuItems, processMethod, function () {
66
+ // Deselect the items and hide the ajax spinner
67
+ t.find( '.spinner' ).hide();
68
+ // Set form back to defaults
69
+ jQuery( '#gs-sim-title' ).val( '' ).blur();
70
+ jQuery( '#gs-sim-html' ).val( '' );
71
+
72
+ } );
73
+ } );
74
+ }
75
+ } );
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: gagan0123, saurabhshukla
3
  Tags: Shortcode, Menus, Custom Link
4
  Requires at least: 3.5
5
- Tested up to: 4.0
6
- Stable tag: 2.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -27,7 +27,7 @@ Also, see a [great tutorial](https://wordpress.org/support/topic/how-does-it-wor
27
 
28
  1. Check the screen options, if you don't see the *Shortcode* box.
29
  1. Check the Shortcode option to see the new Shortcode box.
30
- 1. Add your shortcode/html to the text area (not a link, in the screenshot). Optionally, add a title.
31
  1. The menu item is saved.
32
  1. The html is displayed.
33
  1. Old Method: In the *Links* box, add your shortcode in the URL field.
@@ -65,3 +65,8 @@ Also, see a [great tutorial](https://wordpress.org/support/topic/how-does-it-wor
65
 
66
  = 2.1 =
67
  * Bug fix for custom links with ShortCode like structure not being displayed in the nav menus.
 
 
 
 
 
2
  Contributors: gagan0123, saurabhshukla
3
  Tags: Shortcode, Menus, Custom Link
4
  Requires at least: 3.5
5
+ Tested up to: 4.4.2
6
+ Stable tag: 3.0
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
27
 
28
  1. Check the screen options, if you don't see the *Shortcode* box.
29
  1. Check the Shortcode option to see the new Shortcode box.
30
+ 1. Add your shortcode/HTML to the text area (not a link, in the screenshot). Optionally, add a title.
31
  1. The menu item is saved.
32
  1. The html is displayed.
33
  1. Old Method: In the *Links* box, add your shortcode in the URL field.
65
 
66
  = 2.1 =
67
  * Bug fix for custom links with ShortCode like structure not being displayed in the nav menus.
68
+
69
+ = 3.0 =
70
+ * Removed the error trigger on the FULL HTML OUTPUT usage
71
+ * Added the feature to use shortcodes in titles of menu items as well(works with all types of menu items)
72
+ * Resolved the PHP Notice, popping up in the error log while adding new shortcodes