Создать массив из ввода области сохраненного текста - PullRequest
0 голосов
/ 30 мая 2019

Я разрабатываю плагин для Wordpress. Я сохраняю данные из многострочного текстового ввода. Мне нужно извлечь сохраненные данные и создать массив, используя в качестве разделителя линейные тормоза в текстовой области.

// 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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...