• stefan22

    (@stefan22)


    I am trying to fetch data from a DB and create checkboxes based on the rows from the database. I am having an issue, when calling

    wpcf7_form_tag_data_option

    I have used this code which should put ID as a Value and topic_name as a Label but I am getting an error about WCF7_Pipes not beeing able to convert to string.

    add_filter('wpcf7_form_tag_data_option', function($data, $options, $args) {
        if (str_contains($options[0],"av_topics") === false) {
            return $data;
        }
        global $wpdb;
        $av_topics = $wpdb->prefix . 'av_topics';
    	foreach ($options as $option) {
    		if ($option === 'av_topics_ms') {
                $topics = $wpdb->get_results("SELECT ID,topic_name FROM " . $av_topics . " WHERE FIND_IN_SET('m?',school_type)>0 ORDER BY topic_name", ARRAY_A);
    		}
            if ($option === 'av_topics_zs') {
                $topics = $wpdb->get_results("SELECT ID,topic_name FROM " . $av_topics . " WHERE FIND_IN_SET('z?',school_type)>0 ORDER BY topic_name", ARRAY_A);
    		}
            if ($option === 'av_topics_ss') {
                $topics = $wpdb->get_results("SELECT ID,topic_name FROM " . $av_topics . " WHERE FIND_IN_SET('s?',school_type)>0 ORDER BY topic_name", ARRAY_A);
    		}
    	}
        foreach ($topics as $topic) {
    	    $data['raw_values'][] = $topic["topic_name"] . '|' . $topic["ID"]; //using pipes to put the value before the pipe and the label after by picking up the ID and LabelName fields from the database
        }
        $pipes = new \WPCF7_Pipes($data['raw_values']); //use the WPCF7_Pipes class
        $data['values'] = $pipes->collect_befores(); //get value vefore pipe
        $data['pipes'] = $pipes;
        $data['labels'] = $pipes->collect_afters(); //get label after pipe
    
        return $data;
    }, 10, 3);

    And this is the error

    function wp_check_invalid_utf8( $text, $strip = false ) {
    	$text = (string) $text; //HERE
    Error: Object of class WPCF7_Pipes could not be converted to string

    Strange thing is, that this hook wpcf7_form_tag works

  • The topic ‘Error: Object of class WPCF7_Pipes could not be converted to string’ is closed to new replies.