如何在REGISTER_SETTING回调中使用筛选器验证输入

时间:2013-03-08 作者:egdavid

我现在有了这个功能,它工作得很好:

function wpPartValidate_settings( $input ) {
   if ( check_admin_referer( \'wpPart_nonce_field\', \'wpPart_nonce_verify_adm\' ) ) {

    // Create our array for storing the validated options
    $output = array();

    foreach( $input as $key => $value ) {

      // Check to see if the current option has a value. If so, process it.
      if( isset( $input[$key] ) ) {

        // Strip all HTML and PHP tags and properly handle quoted strings
        $output[$key] = strip_tags( stripslashes( $input[ $key ] ) );

      } // end if
    } // end foreach
  } // end if
  // Return the array processing any additional functions filtered by this action
  return apply_filters( \'wpPartValidate_settings\', $output, $input );
}
我想知道如何在该函数中实现过滤器。

目标是通知管理员一个输入没有正确填写(使用正确的电子邮件、url、数字、日期…)

1 个回复
最合适的回答,由SO网友:Eugene Manuilov 整理而成

首先,我建议您将函数重命名为wpPartSanitize_settings, 因为这个函数不验证任何东西。因为它不会验证任何内容,所以您无法“通知”管理员一个输入无效。

尽管如此,为了wpPartValidate_settings 过滤器,按如下方式操作:

add_filter( \'wpPartValidate_settings\', \'wpse8170_sanitize_settings\', 10, 2 );
function wpse8170_sanitize_settings( $output, $input ) {
    // ...
    return $output;
}

结束

相关推荐

Run shortcode before filters

我的用户在注释中发布代码片段。我为此创建了一个快捷码:function post_codigo($atts,$content=\"\"){ return \'<code>\'.$content.\'</code>\'; } add_shortcode(\'codigo\',\'post_codigo\'); 问题是html在打包到代码标记之前会被过滤掉。我想如果我能在过滤器之前运行短代码,那么我可以使用fun