Tagged: 

Viewing 2 reply threads
  • Author
    Posts
    • #5318
      mcmaster
      Member

      Trying 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 6
      

      Finally 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

    • #5405
      mcmaster
      Member

      The problem also occurs with square brackets.

    • #5422
      mcmaster
      Member

      I 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( '&quot;', '&lsqb;', '&rsqb;' );
         $return = str_replace( $reserved_chars, $encoded_chars, $value );
         return $return;
      }
      
      function mcw_restore_shortcode_chars ( $encoded_value ) {   
         $encoded_chars = array( '&quot;', '&lsqb;', '&rsqb;' );
         $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

Viewing 2 reply threads
  • You must be logged in to reply to this topic.