Tagged: Shortcode
- This topic has 2 replies, 1 voice, and was last updated 6 years, 1 month ago by
mcmaster.
-
AuthorPosts
-
-
December 14, 2015 at 1:12 am #5318
mcmasterMemberTrying to do a simple 2-column section with shortcodes, and one column worked fine but the other kept coming back with an error:
Notice: Undefined variable: col_left in /Users/donna/Sites/lcso/wp-content/plugins/mcw-piklist-custom/add-ons/mcw-post-custom/parts/shortcodes/2column.php on line 6Finally figured out that the left column has some embedded links with double quote marks, e.g.,
<a href="tel:911">9-1-1</a>When I removed the quoted text, the shortcode saved and displayed fine.
Form file:
/* Name: Two-Column Section Description: Create a 2-column section inside a page. Shortcode: mcw-2column Icon: dashicons-grid-view */ piklist('field', array( 'type' => 'editor', 'field' => 'col_left', 'label' => 'Left Column', 'template' => 'field', 'columns' => '12', )); piklist('field', array( 'type' => 'editor', 'field' => 'col_right', 'label' => 'Right Column', 'template' => 'field', 'columns' => '12', ));Display file:
/* Shortcode: mcw-2column */ $left = apply_filters( 'the_content', $col_left ); $right = apply_filters( 'the_content', $col_right ); ?> <div class="two-column row"> <div class="col-sm-6"> <?php echo $left; ?> </div> <div class="col-sm-6"> <?php echo $right; ?> </div> </div>Donna
-
December 22, 2015 at 2:36 am #5405
mcmasterMemberThe problem also occurs with square brackets.
-
December 23, 2015 at 9:20 pm #5422
mcmasterMemberI tried to fix this using Piklist sanitization. The function named in the piklist_sanitization_rules filter (mcw_sanitize_shortcode) was called, but the callback function (mcw_sanitize_shortcode_callback) never got executed.
Did I do something wrong? Or does the shortcode interface not support sanitization rules?
I created these filters and functions:
/** keep shortcodes from barfing on " [ ] chars */ add_filter( 'piklist_sanitization_rules', 'mcw_sanitize_shortcode', 11 ); function mcw_sanitize_shortcode ( $sanitization_rules ) { $sanitization_rules['mcw_shortcode'] = array( 'callback' => 'mcw_sanitize_shortcode_callback' ); return $sanitization_rules; } function mcw_sanitize_shortcode_callback ( $value, $field, $options ) { $reserved_chars = array( '"', '[', ']' ); $encoded_chars = array( '"', '[', ']' ); $return = str_replace( $reserved_chars, $encoded_chars, $value ); return $return; } function mcw_restore_shortcode_chars ( $encoded_value ) { $encoded_chars = array( '"', '[', ']' ); $reserved_chars = array( '"', '[', ']' ); return str_replace( $encoded_chars, $reserved_chars, $encoded_value ); }And here’s my shortcode form:
<?php /* Name: Two-Column Section Description: Create a 2-column section inside a page. Shortcode: mcw-2column Icon: dashicons-controls-pause */ piklist('field', array( 'type' => 'editor', 'field' => 'col_left', 'label' => 'Content for left column', 'columns' => 12, 'options' => array ( 'media_buttons' => false, 'teeny' => true, ), 'sanitize' => array( array( 'type' => 'mcw_shortcode', ), ), )); piklist('field', array( 'type' => 'editor', 'field' => 'col_right', 'label' => 'Content for left column', 'columns' => 12, 'options' => array ( 'media_buttons' => false, 'teeny' => true, ), 'sanitize' => array( array( 'type' => 'mcw_shortcode', ), ), ));and the shortcode output:
<?php /* Shortcode: mcw-2column */ $left = apply_filters( 'the_content', mcw_restore_shortcode_chars( $col_left ) ); $right = apply_filters( 'the_content', mcw_restore_shortcode_chars( $col_right ) ); ?> <div class="two-column row"> <div class="col-sm-6"> <?php echo $left; ?> </div> <div class="col-sm-6"> <?php echo $right; ?> </div> </div>Thanks,
Donna
-
-
AuthorPosts
- You must be logged in to reply to this topic.