【问题标题】:Wordpress sanitize data before saving with update_post_metaWordpress 在使用 update_post_meta 保存之前清理数据
【发布时间】:2017-09-01 16:14:53
【问题描述】:

我正在尝试为我的新 WordPress 模板创建帖子选项,但我不知道如何在保存之前清理或验证我的自定义帖子元数据:

$data = $_POST['enablog_post_options'];

// Update the meta fields in the database.
update_post_meta( $post_id, 'enablog_post_options',$data ); 

所有选项(YouTube URL、文本、复选框和单选按钮)都使用我唯一的元键 enablog_post_options 保存。

更新:

sanitize_text_field() 损坏了我的所有代码(我保存帖子时会检查所有复选框),恕我直言,我需要的不仅仅是 sanitize_text_field(),因为$_POST['enablog_post_options'] 有复选框、文本字段等等。

【问题讨论】:

    标签: php wordpress checkbox sanitize


    【解决方案1】:

    首先。假设$_POST['enablog_post_options'] 是一个数组,它应该作为一个数组进行清理,在循环中迭代每个元素。不是作为字符串,一次是所有元素。

    因此,请查看您的 enablog_post_options 数组并为每个元素确定一种与数据类型相关的清理技术。 WP Codex 可以帮助开始

    现在。当您了解您真正要清理的数据类型时,我怀疑值得一提的是 update_post_meta() 内置清理操作和自定义过滤器,您可以挂接到该函数。

    因此,任何人都可以查找为update_post_meta() here in the Core Metadata API source code 完成繁重工作的update_metadata() 函数的代码。

    但与此同时,它会消毒:

    • 带有sanitize_key()wp_unslash() 的元键;
    • 带有wp_unslash()sanitize_meta() 的元值(后面有更多解释)。
    • update_metadata()调用wpdb::prepare()的数据库保存查询->wpdb::update();

    使用sanitize_meta()进行消毒。

    另外清理 [自定义] 帖子元数据的便捷方法是通过 sanitize_meta()update_metadata() 已经将您必须创建的潜在现有自定义清理过滤器连接到元字段清理过程中。这是通过sanitize_meta() 完成的。

    它是从update_metadata() 调用的,带有您所有的帖子元参数:

    $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
    

    因此,您可以像这样制作一个自定义清理过滤器来处理您的帖子元数据(来自 WP Codex sanitize_meta() 描述的示例,上面的链接):

    // --- sanitize_meta() call is commented out because it is called from update_metadata()
    // $clean_value = sanitize_meta( 'birth-year', $user_input, 'user' );
    function sanitize_birth_year_meta( $year ) {
        $now = date( 'Y' );
        $then = $now - 115; // No users older than 115.
        if ( $then > $year || $year > $now ) {
            wp_die( 'Invalid entry, go back and try again.' );
        }
        return $year;
    }
    add_filter( 'sanitize_user_meta_birth-year', 'sanitize_birth_year_meta' );
    

    继续使用一些虚构的代码,在 sanitize_birth_year_meta() 而不是 $year 中,您将在调用过滤器时获得您的 $data 内容进行清理。

    【讨论】:

      【解决方案2】:

      WordPress 标准建议对每个全局变量($_POST、$_GET 等)使用wp_unslash()。然后根据您的需要使用任何清理功能。

      $data = sanitize_text_field( wp_unslash( $_POST['enablog_post_options'] ) );
      
      // Update the meta fields in the database.
      update_post_meta( $post_id, 'enablog_post_options',$data ); 
      

      【讨论】:

        【解决方案3】:

        使用它来清理您的数据:

        $data = sanitize_text_field( $_POST['enablog_post_options'] );
        
        // Update the meta fields in the database.
        update_post_meta( $post_id, 'enablog_post_options',$data ); 
        

        查看Validating Sanitizing and Escaping User Data 上的 Codex

        【讨论】:

        • Sanitize_text_field 损坏了我的所有代码(我保存帖子时会检查所有复选框),恕我直言,我需要的不仅仅是 sanitize_text_field,因为 $_POST['enablog_post_options'] 有复选框、文本字段等等。跨度>
        猜你喜欢
        • 2013-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        相关资源
        最近更新 更多