【问题标题】:How save input fields value created dynamically in Wordpress save_post hook action?如何保存在 Wordpress save_post 挂钩操作中动态创建的输入字段值?
【发布时间】:2014-05-24 10:00:48
【问题描述】:

我正在开发一个创建自定义帖子类型的插件。 我创建了一个带有自定义元输入字段的自定义元框。 使用 AJAX 函数,我复制了输入字段。它们是相同的名称和相同的类。 没关系,但是如何使用save_post 钩子操作保存这些数据?

如果我使用经典函数,update_post_meta,它只保存最后一个值。 我需要创建一个数组并将其传递给保存函数。 如何?

如果我使用 admin-ajax.php,我如何在回调函数中为 update_post_meta 传递 post_id? 我不想使用 wpalchemy。

【问题讨论】:

标签: wordpress custom-post-type


【解决方案1】:

谢谢!这是完美的 ! 我用

$new[$i]['name'] = stripslashes( strip_tags( htmlentities($names[$i]) ) );

对于像 'à,è,ì,ò,ù' 这样的特殊字符,它可以正常工作!

谢谢!

【讨论】:

    【解决方案2】:

    输入字段 name 必须是数组格式:field_name[]

    <form>

    <td><input type="text" class="widefat" name="name[]" /></td>
    <td><input type="text" class="widefat" name="url[]" value="http://" /></td>
    

    然后在save_post 操作:

    $names = $_POST['name'];
    $urls = $_POST['url'];
    $count = count( $names );
    
    for ( $i = 0; $i < $count; $i++ ) 
    {
        if ( $names[$i] != '' ) 
        {
            $new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) ); // Sanitization
    
            if ( $urls[$i] == 'http://' )
                $new[$i]['url'] = '';
            else
                $new[$i]['url'] = stripslashes( $urls[$i] );
        }
    }
    
    if ( !empty( $new ) && $new != $old )
        update_post_meta( $post_id, 'repeatable_fields', $new );
    

    示例改编from here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多