• Resolved fb-creations

    (@fb-creations)


    Hi, i am codding a filter widget witch sends paramters with $_GET in order to customize The Loop whith WP_Query.

    In order to filter width several taxonomies terms i have this :
    mytax[]=term1, mytax[]=term2 …

    i did a var_dump of WP_Query objetc and all is ok, but results are not displayed since i have mytax[] and i get 2 errors :

    Warning: preg_match() expects parameter 2 to be string, array given in C:\xampp\htdocs\wordpress-4.3\wp-includes\formatting.php on line 1067
    
    Warning: strip_tags() expects parameter 1 to be string, array given in C:\xampp\htdocs\wordpress-4.3\wp-includes\formatting.php on line 1505

    When i have only mytax=term1 there is ne problem.
    How can i do it ? Is there another way to do it ?
    Thank you for help.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz

    (@bcworkz)

    Without showing the code that is using the taxonomy parms nor the code causing the errors, you’re not giving us much to go on! ??

    Generally speaking, to process an array of data by functions that accept single values, we run a foreach loop that takes each array element in turn, does something with it before proceeding to the next element in the array. This loop is independent of the WP loop.

    If that’s too vague, you’ll need to show us some code ??

    Thread Starter fb-creations

    (@fb-creations)

    Hi
    this is my widget function for the form

    public function widget( $args, $instance ) {
            $cache = array();
            if ( ! $this->is_preview() ) {
                $cache = wp_cache_get( 'widget_alliance_build_posts', 'widget' );
            }
    
            if ( ! is_array( $cache ) ) {
                $cache = array();
            }
    
            if ( ! isset( $args['widget_id'] ) ) {
                $args['widget_id'] = $this->id;
            }
    
            if ( isset( $cache[ $args['widget_id'] ] ) ) {
                echo $cache[ $args['widget_id'] ];
                return;
            }
    
            ob_start();
    
            $title          = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'A new section', 'alliance' );
            /** This filter is documented in wp-includes/default-widgets.php */
            $title          = apply_filters( 'widget_title', $title, $instance, $this->id_base );
            $sectionID      = ( ! empty( $instance['sectionID'] ) ) ? $instance['sectionID'] : __( 'A new section', 'alliance' );
            $category       = $instance['category'];
            $display       = $instance['display'];
    
            ?><section id="<?php echo $sectionID; ?>"><?php
    
            echo $args['before_widget'];
            if ( $title ) {
                echo $args['before_title'] . $title . $args['after_title'];
            }
            if ( !empty( $category ) ) {
                echo '<form id="'. $this->get_field_id('category').'" method="get" action="">';
                $i = 1;
                foreach ($category as $tax) {
                    $terms = get_terms($tax);
                    switch ($display[$i]) {
                        case 'select':
                            ?>
                            <p>
                            <label for="<?php echo $this->get_field_id('category'); ?>"><?php echo $tax; ?></label>
                        <select id="<?php echo $tax; ?>" name="<?php echo $tax; ?>">
                            <option name="all" value=""><?php echo __('All'); ?></option>
                            <?php
                            foreach ($terms as $taxo => $values) {
                                //echo '<pre>' . var_dump($values->slug);
                                $selected = esc_attr($values->slug) ? ' selected = "selected" ' : '';
                                $option = '<option ' . $selected . 'value="' . $values->slug;
                                $option = $option . '">';
                                $option = $option . $values->slug;
                                $option = $option . '</option>';
                                echo $option;
                            }
                            echo '</select>';
                            break;
                        case 'checkbox':
                            echo '<p>';
                            $t = 0;
                            foreach ($terms as $taxo => $values) {
                                    echo '<label for="'. $this->get_field_id('category').'">'. $values->slug.'</label>';
                                echo '<input id="'. $this->get_field_id('category') . $values->slug.'"
                                       name="'. $tax.'[]" type="checkbox"
                                       value="'. $values->slug.'" '. checked('1', $values->slug).' />';
                                $t++;
                                    }
                            break;
                        case 'radio':
                            echo '<p>';
                            $t = 0;
                            foreach ($terms as $taxo => $values) {
                                echo '<label for="'. $this->get_field_id('category').'">'. $values->slug.'</label>';
                                echo '<input id="'. $this->get_field_id('category') . $values->slug.'"
                                       type="radio" name="'. $tax.'"
                                       value="'. $values->slug.'" '. checked('1', $values->slug).' />';
                                $t++;
                            }
                            break;
                        case 'range':
                            echo '<p>';
                            echo '<label for="'. $this->get_field_id('category').'">'. $tax.'</label>';
                            $input = '<input id="'. $this->get_field_id('category') . $tax.'" type="range" name="'. $tax.'"';
                            $a = 0;
                            $all_terms = array();
                            foreach ($terms as $taxo) {
                                //echo '<pre>' . var_dump($taxo->name);
                                array_push($all_terms, $taxo->name);
                                $min = min($all_terms);
                                $max = max($all_terms);
                                $a++;
                            }
                            $input .= 'min="'.$min.'" max="'.$max.'" step="1" />';
                            echo $input;
                            break;
                    } $i++;
                }
                    echo '</p><p><input type="submit" name="submit"/></p></form>';
            }
            //echo '<pre>'.var_dump($instance);
    
            echo $args['after_widget'];
    
            ?></section><?php
    
            if ( ! $this->is_preview() ) {
                $cache[ $args['widget_id'] ] = ob_get_flush();
                wp_cache_set( 'widget_alliance_build_posts', $cache, 'widget' );
            } else {
                ob_end_flush();
            }
        }

    this is my shortcode function for displaying results

    public static function sc_result_filter($atts) {
        if (!isset( $_GET['submit'] )) {
            $post_type = sc_list_post_type();
            extract(shortcode_atts(array(
                'post_type' => $post_type,
                'posts' => 1,
            ), $atts));
    
            $return_string = '<ul><pre>'.var_dump($post_type).'</pre>';
            query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts, 'post_type' => $post_type));
            if (have_posts()) :
                while (have_posts()) : the_post();
                    $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
                endwhile;
            endif;
            $return_string .= '</ul>';
            wp_reset_query();
        }
        else {
            $post_type = sc_list_post_type();
            $tax_query = array();
            unset($_GET['submit']);
            foreach ($_GET as $k => $v ) {
                $tax_name = $k;
                $tax_value = $v;
                $tax_request = array(
                        'taxonomy'=>$tax_name,
                        'field'=>'slug',
                        'terms'=>array($tax_value),
                        'operator'=>'IN'
                    );
    
                $tax_query[] = $tax_request;
            }
            extract( shortcode_atts(
                    $args = array(
                        'post_type' => $post_type,
                        'tax_query' => $tax_query
                    ), $atts )
            );
            $output = '';
            $query = new WP_Query($args);
            echo '<pre>';
            echo var_dump($query).'<pre>';
            if ($query->have_posts()) :
                $output .= '<div class="results"><ul><pre>'.var_dump($tax_query).'</pre>';
                while ($query->have_posts()) : $query->the_post();
                    $output .= '<li><div class="image">'.get_the_post_thumbnail().'</div>';
                    $output .= '<a href="'.get_permalink().'">'.get_the_title().'</a></li>';
                endwhile;
                $output .= '</div>';
            endif;
            wp_reset_postdata();
            return $output;
        }
        return $return_string;
        }

    It is not really finished but checkbox works fine when i don’t use[]. How can i perform this ?

    Moderator bcworkz

    (@bcworkz)

    Thanks for posting your code. Unfortunately, the solution is not as obvious as I had hoped ??

    The errors are coming from core formatting functions, which are often called when sanitizing data from forms for output or storage. I’m unsure why your taxonomy term arrays are being passed through such functions. Unless this alone is a good enough clue for you, in order to narrow down where the problem lies, we’ll need a backtrace from the error back to where the taxonomy terms are being used inappropriately.

    Some versions of PHP display a backtrace by default. If you have that, please share the output. If you don’t see a backtrace, you’ll need to set an error handler to output one for you. This involves debug_backtrace(), but the function’s returned data is hard to decipher by itself. You should be able to find a backtrace error handler somewhere that produces more useful output.

    I could probably dig one up for you, but I don’t have time right now.

    Thread Starter fb-creations

    (@fb-creations)

    Thank you very much for help, i did a var dump of backtrace in my code but it don’t seems usefull where i output this :

    array(15) {
      [0]=>
      array(4) {
        ["function"]=>
        string(16) "sc_result_filter"
        ["class"]=>
        string(12) "sc_TaxFilter"
        ["type"]=>
        string(2) "::"
        ["args"]=>
        array(3) {
          [0]=>
          &array(1) {
            ["posts"]=>
            string(2) "10"
          }
          [1]=>
          &string(0) ""
          [2]=>
          &string(7) "results"
        }
      }
      [1]=>
      array(4) {
        ["file"]=>
        string(56) "C:\xampp\htdocs\wordpress-4.3\wp-includes\shortcodes.php"
        ["line"]=>
        int(308)
        ["function"]=>
        string(14) "call_user_func"
        ["args"]=>
        array(4) {
          [0]=>
          &array(2) {
            [0]=>
            string(12) "sc_TaxFilter"
            [1]=>
            string(16) "sc_result_filter"
          }
          [1]=>
          &array(1) {
            ["posts"]=>
            string(2) "10"
          }
          [2]=>
          &string(0) ""
          [3]=>
          &string(7) "results"
        }
      }
      [2]=>
      array(2) {
        ["function"]=>
        string(16) "do_shortcode_tag"
        ["args"]=>
        array(1) {
          [0]=>
          &array(7) {
            [0]=>
            string(18) "[results posts=10]"
            [1]=>
            string(0) ""
            [2]=>
            string(7) "results"
            [3]=>
            string(9) " posts=10"
            [4]=>
            string(0) ""
            [5]=>
            string(0) ""
            [6]=>
            string(0) ""
          }
        }
      }
      [3]=>
      array(4) {
        ["file"]=>
        string(56) "C:\xampp\htdocs\wordpress-4.3\wp-includes\shortcodes.php"
        ["line"]=>
        int(210)
        ["function"]=>
        string(21) "preg_replace_callback"
        ["args"]=>
        array(3) {
          [0]=>
          &string(186) "/\[(\[?)(embed|wp_caption|caption|gallery|playlist|audio|video|sc_map|results)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)/s"
          [1]=>
          &string(16) "do_shortcode_tag"
          [2]=>
          &string(19) "[results posts=10]
    "
        }
      }
      [4]=>
      array(2) {
        ["function"]=>
        string(12) "do_shortcode"
        ["args"]=>
        array(1) {
          [0]=>
          &string(19) "[results posts=10]
    "
        }
      }
      [5]=>
      array(4) {
        ["file"]=>
        string(52) "C:\xampp\htdocs\wordpress-4.3\wp-includes\plugin.php"
        ["line"]=>
        int(213)
        ["function"]=>
        string(20) "call_user_func_array"
        ["args"]=>
        array(2) {
          [0]=>
          &string(12) "do_shortcode"
          [1]=>
          &array(1) {
            [0]=>
            string(19) "[results posts=10]
    "
          }
        }
      }
      [6]=>
      array(4) {
        ["file"]=>
        string(59) "C:\xampp\htdocs\wordpress-4.3\wp-includes\post-template.php"
        ["line"]=>
        int(230)
        ["function"]=>
        string(13) "apply_filters"
        ["args"]=>
        array(2) {
          [0]=>
          &string(11) "the_content"
          [1]=>
          &string(18) "[results posts=10]"
        }
      }
      [7]=>
      array(4) {
        ["file"]=>
        string(78) "C:\xampp\htdocs\wordpress-4.3\wp-content\themes\twentyfifteen\content-page.php"
        ["line"]=>
        int(22)
        ["function"]=>
        string(11) "the_content"
        ["args"]=>
        array(0) {
        }
      }
      [8]=>
      array(4) {
        ["file"]=>
        string(54) "C:\xampp\htdocs\wordpress-4.3\wp-includes\template.php"
        ["line"]=>
        int(557)
        ["args"]=>
        array(1) {
          [0]=>
          string(78) "C:\xampp\htdocs\wordpress-4.3\wp-content\themes\twentyfifteen\content-page.php"
        }
        ["function"]=>
        string(7) "require"
      }
      [9]=>
      array(4) {
        ["file"]=>
        string(54) "C:\xampp\htdocs\wordpress-4.3\wp-includes\template.php"
        ["line"]=>
        int(514)
        ["function"]=>
        string(13) "load_template"
        ["args"]=>
        array(2) {
          [0]=>
          &string(78) "C:\xampp\htdocs\wordpress-4.3/wp-content/themes/twentyfifteen/content-page.php"
          [1]=>
          &bool(false)
        }
      }
      [10]=>
      array(4) {
        ["file"]=>
        string(62) "C:\xampp\htdocs\wordpress-4.3\wp-includes\general-template.php"
        ["line"]=>
        int(171)
        ["function"]=>
        string(15) "locate_template"
        ["args"]=>
        array(3) {
          [0]=>
          &array(2) {
            [0]=>
            string(16) "content-page.php"
            [1]=>
            string(11) "content.php"
          }
          [1]=>
          &bool(true)
          [2]=>
          &bool(false)
        }
      }
      [11]=>
      array(4) {
        ["file"]=>
        string(70) "C:\xampp\htdocs\wordpress-4.3\wp-content\themes\twentyfifteen\page.php"
        ["line"]=>
        int(24)
        ["function"]=>
        string(17) "get_template_part"
        ["args"]=>
        array(2) {
          [0]=>
          &string(7) "content"
          [1]=>
          &string(4) "page"
        }
      }
      [12]=>
      array(4) {
        ["file"]=>
        string(61) "C:\xampp\htdocs\wordpress-4.3\wp-includes\template-loader.php"
        ["line"]=>
        int(75)
        ["args"]=>
        array(1) {
          [0]=>
          string(70) "C:\xampp\htdocs\wordpress-4.3\wp-content\themes\twentyfifteen\page.php"
        }
        ["function"]=>
        string(7) "include"
      }
      [13]=>
      array(4) {
        ["file"]=>
        string(48) "C:\xampp\htdocs\wordpress-4.3\wp-blog-header.php"
        ["line"]=>
        int(16)
        ["args"]=>
        array(1) {
          [0]=>
          string(61) "C:\xampp\htdocs\wordpress-4.3\wp-includes\template-loader.php"
        }
        ["function"]=>
        string(12) "require_once"
      }
      [14]=>
      array(4) {
        ["file"]=>
        string(39) "C:\xampp\htdocs\wordpress-4.3\index.php"
        ["line"]=>
        int(17)
        ["args"]=>
        array(1) {
          [0]=>
          string(48) "C:\xampp\htdocs\wordpress-4.3\wp-blog-header.php"
        }
        ["function"]=>
        string(7) "require"
      }
    }

    Moderator bcworkz

    (@bcworkz)

    Yes, the raw backtrace output is a little hard to decipher, but I think I get the picture now, thanks for doing that, it actually helped!

    I’m not fully sure what’s going on, but I think it is because you’re trying to use $_GET taxonomy array data within a shortcode handler that is causing the problem. Shortcode attributes need to be sanitized, but the functions to do that can’t handle arrays.

    Perhaps there is a way around this using the track you’re on, but I’m not sure what it is. I would suggest a different track simply to get around the issue. You can still use a shortcode to display the data, but the widget form data would be better off being processed outside the shortcode handler. Your options are to use AJAX or to submit a GET request through /wp-admin/admin-post.php. Despite the filename, it will accept GET requests.

    Let the AJAX or admin-post handler deal with the array data, then place the result in a transient or something where the shortcode handler can get at it. With AJAX, the jQuery script can request the shortcode page once the data has been processed. With admin-post the PHP callback should be able to redirect to the shortcode page.

    One final option is to process the data on a page template for the page where the shortcode is used. Sorry to make this even more complicated for you, but it appears the array needs to be processed outside the shortcode handler in some way.

    Thread Starter fb-creations

    (@fb-creations)

    Thank you very much for those explanations !
    I am actually learning JQuery so i think i would be abble to do this in some time…

    Do you mean i should use a template page instead of my shortcode ?

    Moderator bcworkz

    (@bcworkz)

    Either way would work — template page with shortcode or without. Only the array processing needs to be outside of the shortcode handler, but you could make a custom template page and forgo the shortcode entirely. I suppose it would depend on how much flexibility you need for the shortcode output in relation to other content.

    Thread Starter fb-creations

    (@fb-creations)

    Hi i finally found how to use the Loop with several taxonomies : by using query_posts.

    I have to get a string of the searched terms ans pass it to query post args like that :

    ‘my-tax’ => ‘term1, term2’

    With this way, i get no errors, it works like a charm !

    Thank you very much foll all tips.

    Moderator bcworkz

    (@bcworkz)

    You’re most welcome!

    Great news that you worked it all out.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Probleme with formatting file’ is closed to new replies.