• Resolved ayandebnath

    (@ayandebnath)


    Hello,

    I have added a SELECT field for Term Field whose option are dynamically generated from a custom SQL query.

    I am using –
    add_filter( 'wpt_field_options', 'colleges_list', 10, 3);

    But in colleges_list function –
    function colleges_list( $old_options, $title, $type ) {}

    $title is coming blank/empty.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Anonymous User 14808221

    (@anonymized-14808221)

    We do not have official Documents and information about the filter wpt_field_options but we work on this.

    The filter wpt_field_options can be used to manipulate the options of select and radio field.

    Arguments:
    $current_options
    $title (the title of the field)

    Output:
    Array

    Required format:

    array(
        array(
            '#title' => 'First Title',
            '#value' => 'first-value'
        ),
        array(
            '#title' => 'Second Title',
            '#value' => 'second-value'
        ),
    )

    Example of usage:
    Let’s assume your theme has a colorset of dark, light and accent color, which are editable in the theme options. The theme stores these values as a wp_option theme_name_colorset in this format:

    array(
        'Dark'         => '#333333',
        'Light'        => '#eeeeee',
        'Accent Color' => '#ff3300'
    )  

    You want to use these colors on a custom select field to define the headline color of the post. So we create a post field group with a select field:
    https://drive.google.com/file/d/0B-e4FFHtzndlU1dfaXZtMFJlV2c/view?usp=sharing

    Now we adding this function to our themes functions.php:

    add_filter( 'wpt_field_options', function ( $current_options, $title_of_field ) {
        if ( $title_of_field != 'Headline Color' ) {
            // not our "Headline Color" field
            return $current_options;
        }
    
        $theme_colorset = get_option( 'theme_name_colorset' );
    
        if ( ! $theme_colorset ) {
            // no theme colors are set yet
            return array(
                array(
                    '#title' => 'No colors available',
                    '#value' => 0
                )
            );
        }
    
        $new_options = array();
        foreach ( $theme_colorset as $color_title => $color_value ) {
            $new_options[] = array(
                '#title' => $color_title . ' (' . $color_value . ')',
                '#value' => $color_value
            );
        }
    
        return $new_options;
    }, 10, 2 );

    This is the result:

    https://drive.google.com/open?id=0B-e4FFHtzndlenRNVVowNW44RFk
    https://drive.google.com/open?id=0B-e4FFHtzndlaE83blNKeVRKSWc

    I cannot assist Custom Code further, but this should give you enough informations to work with it.
    Soon we will publish a DOC about it as well.

    Thread Starter ayandebnath

    (@ayandebnath)

    Just an update for anyone who is facing similar problem where $title variable may be coming as null/empty, add this code block in the beginning of the function –

    
    add_filter( 'wpt_field_options', 'custom_select_options', 10, 3);
    
    function custom_select_options( $old_options, $title, $type ) {
    	if (empty($title) || $title=='') {
    		$wptoolset_field = $GLOBALS['wptoolset_field'];
    		$title = str_ireplace('wpcf-','', $wptoolset_field->getId());
    	}
    	
    	// more code
    }
    

    @ayandebnath Thank you, sir. You saved my day.

    @ayandebnath Thank you!!! You saved my day also.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘BUG Report: wpt_field_options $title is empty when SELECT field for Term Field’ is closed to new replies.