从保存的文本区域输入创建数组

时间:2019-05-29 21:49:06

标签: php html wordpress

我正在开发Wordpress插件。我正在从多行textarea输入中保存数据。我需要检索保存的数据并使用文本区域中的行制动器作为分隔符来创建一个数组。

// register setting
add_settings_field(
  'api_pv_exclude_from_brand',
  'Exclude Words in Brand',
  'api_pv_text_area_callback',
  'api-pv-settings',
  'api_pv_general_options_section',
  [ 'id' => 'api_pv_exclude_from_brand', 'label' => 'Please enter words one per line. With the above option, Require Brand in Title, some times brands will have words in them that are harder to match in a YouTube title, for example, the brand <u>Cooler Master Co. Ltd</u>. We would want to ignore Co. and Ltd and just accept videos with <u>Cooler Master</u> in the title.' ]
);  

// validate setting
if ( isset( $input['api_pv_exclude_from_brand'] ) ) {
    $input['api_pv_exclude_from_brand'] = wp_kses_post( $input['api_pv_exclude_from_brand'] );
}

// callback

function api_pv_text_area_callback( $args ) {
    $options = get_option( 'api_pv_options', api_pv_default_options() );
    $id    = isset( $args['id'] )    ? $args['id']    : '';
    $label = isset( $args['label'] ) ? $args['label'] : '';
    $allowed_tags = wp_kses_allowed_html( 'post' );
    $value = isset( $options[$id] ) ? wp_kses( stripslashes_deep( $options[$id] ), $allowed_tags ) : '';
    echo '<textarea id="api_pv_options_'. $id .'" name="api_pv_options['. $id .']" rows="5" cols="50">'. $value .'</textarea><br />';
    echo '<label for="api_pv_options_'. $id .'">'. $label .'</label>';
}

// get option and create array
$plugin_options = wp_parse_args(get_option('api_pv_options'), api_pv_default_options());
$test = sanitize_text_field($plugin_options['api_pv_exclude_from_brand']);
$lines = explode("\n", $test);
foreach( $lines as $line ){
  echo $line;
}

0 个答案:

没有答案