【问题标题】:Multiple Content Filters Gravity View Plugin For Gravity Forms Plugin For WordPress多个内容过滤器 Gravity View Plugin For Gravity Forms Plugin For WordPress
【发布时间】:2015-03-04 22:16:54
【问题描述】:

我们正在使用 Gravity View 插件为 WordPress 的 Gravity Forms 处理过滤器。还不是很擅长过滤器,所以如果可能的话,希望能得到一些帮助。添加时我们有一个过滤器,它根据重力表单字段 ID 过滤掉某些内容。在下面的示例中,我们将过滤掉 ID 为 18 的 Gravity Form 字段。

例子:

add_filter( 'gravityview/fields/custom/content_before', 'my_gv_custom_content_before', 10, 1 );
function my_gv_custom_content_before( $content ) {
$id = '18';
global $gravityview_view;
extract( $gravityview_view->field_data );
if( empty( $entry[ (string)$id ] ) ) {
return '';
}
return $content;
} 

这行得通。然后更进一步,我们被告知我们可以在这个过滤器中添加一些东西来过滤掉多个 Gravity Form 字段 id,就像添加到我们上面的示例中一样。在此示例中,过滤字段 18 和 26:

if( ( false !== strpos( $content, '{MY_FIELD_NAME:18}') && empty( $entry['18'] ) ) || ( ( false !== strpos( $content, '{MY_FIELD_NAME:26}') && empty( $entry['26'] )  ) ) {
return '';
}

两者结合起来会是什么样子?例如,如果我们想过滤字段 id 18、26 或者第三个字段 34?使用代码示例的第一部分和第二个示例,它们组合起来会是什么样子?谁能举个例子?

一直在玩这个并且没有太多运气将它们全部组合到一个过滤器中。不断收到错误,我相信我们没有完全正确的东西。提前谢谢你。

【问题讨论】:

    标签: wordpress forms


    【解决方案1】:

    搞定了,下面是代码,以防有人需要。

    /** Modify the content returned from the Custom Content field */
    add_filter( 'gravityview/fields/custom/content_before', 'my_gv_custom_content_before', 10, 1 );
    /**
    * Removes the custom content field's content in case a certain entry field is empty
    *
    * @param string $content Custom Content field content
    * @return string
    */
    function my_gv_custom_content_before( $content ) {
    global $gravityview_view;
    extract( $gravityview_view->field_data );
    // field_id => merge_tag
    $validation = array(
    '18' => '{Custom Background:18}',
    '26' => '{New YouTube Video Test:26}',
    '28' => '{New Vimeo:28}'
    );
    foreach( $validation as $id => $merge ) {
    if( false !== strpos( $content, $merge ) && empty( $entry[ (string)$id ] ) ) {
    return '';
    }
    }
    return $content;
    } 
    

    【讨论】:

      猜你喜欢
      • 2014-11-27
      • 2015-08-01
      • 2020-06-17
      • 1970-01-01
      • 2018-07-23
      • 2015-07-02
      • 1970-01-01
      • 2014-12-06
      相关资源
      最近更新 更多