wordpress 在数组中的字符串之间放置换行符(但仅当字符串存在时)

mklgxw1f  于 2023-03-22  发布在  WordPress
关注(0)|答案(1)|浏览(140)

所以--我在我的网站上定制了一个表单,并将表单的结果以数组的形式发布给第三方。不幸的是,我试图将一堆不同的字段塞进数组中的“additional_information”条目。我希望在字段之间有一个换行符,如果(且仅当)字段中有一个条目。
它看起来像这样:

$post_url = 'API HOOK';
 $body = array(
 'lead[first_name]' => rgar( $entry, '1.3' ), 
 'lead[last_name]' => rgar( $entry, '1.6' ), 
 'lead[email_address]' => rgar ($entry, '2'),
 'lead[phone_number]' => rgar( $entry, '24' ),
 'lead[company]' => rgar( $entry, '22' ),
 'lead[event_description]' => rgar( $entry, '3' ),
 'lead[event_date]' => rgar( $entry, '6' ),
 'lead[start_time]' => rgar( $entry, '7' ),
 'lead[end_time]' => rgar( $entry, '23' ),
 'lead[guest_count]' => rgar( $entry, '16' ),
 'lead[additional_information]' => nl2br(rgar( $entry, '10' ) . "\n" .
 rgar( $entry, '13' ) . "\r\n" .
 rgar( $entry, '15' ) . "\r\n" .
 rgar( $entry, '17.1' ) . "\r\n" .
 rgar( $entry, '17.2' ) . "\r\n" .
 rgar( $entry, '17.3' ) . "\r\n" .
 rgar( $entry, '17.4' ) . "\r\n" .
 rgar( $entry, '18.1' ) . "\r\n" .
 rgar( $entry, '18.2' ) . "\r\n" .
 rgar( $entry, '18.3' ) . "\r\n" .
 rgar( $entry, '18.4' ) . "\r\n" .
 rgar( $entry, '18.5' ) . "\r\n" .
 rgar( $entry, '18.6' ) . "\r\n" .
 rgar( $entry, '18.7' ) . "\r\n" .
 rgar( $entry, '18.8' ) . "\r\n" .
 rgar( $entry, '18.9' ) . "\r\n" .
 rgar( $entry, '18.10' ) . "\r\n" .
 rgar( $entry, '18.11' ) . "\r\n" .
  rgar( $entry, '18.12' ) . "\r\n" .
  rgar( $entry, '18.13' ) . "\r\n" .
  rgar( $entry, '18.14' ) . "\r\n" .
  rgar( $entry, '18.15' ) . "\r\n" .
  rgar( $entry, '18.16' ) . "\r\n" .
  rgar( $entry, '18.17' ) . "\r\n" .
  rgar( $entry, '18.18' ) . "\r\n" .
  rgar( $entry, '19' ) . "\r\n" .
  rgar( $entry, '20' ) . "\r\n" .
  rgar( $entry, '21' ))
 );

以前,所有这些字符串都是用.连接在一起的,这会造成一个巨大的混乱的单词沙拉,所以我试图在每个字符串之间使用nl 2br(和“\r\n”。但是现在,即使这些可选字段中的一些是空的,也会有所有的空格。
如果$entry字段不为空,任何人都可以添加一个换行符,我怎么能有任何想法?
我在每个字符串之间添加了“.“\r\n”.,但如果没有$entry,我不希望所有的空格都是空的

gpnt7bae

gpnt7bae1#

好吧,我让它工作,把所有的字符串到一个新的数组,过滤数组,然后imploding它。如果有人要做类似的事情,这里的代码:

add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {
$array2 = array( rgar( $entry, '10' ) , rgar( $entry, '13' ) , rgar( $entry, '15' ) , rgar( $entry, '17.1' ) , rgar( $entry, '17.2' ) , rgar( $entry, '17.3' ) , rgar( $entry, '17.4' ) , rgar( $entry, '18.1' ) , rgar( $entry, '18.2' ) , rgar( $entry, '18.3' ) , rgar( $entry, '18.4' ) , rgar( $entry, '18.5' ) , rgar( $entry, '18.6' ) , rgar( $entry, '18.7' ) , rgar( $entry, '18.8' ) , rgar( $entry, '18.9' ) , rgar( $entry, '18.10' ) , rgar( $entry, '18.11' ) , rgar( $entry, '18.12' ) , rgar( $entry, '18.13' ) , rgar( $entry, '18.14' ) , rgar( $entry, '18.15' ) , rgar( $entry, '18.16' ) , rgar( $entry, '18.17' ) , rgar( $entry, '18.18' ) , rgar( $entry, '19' ) , rgar( $entry, '20' ) , rgar( $entry, '21' )
);
$filtered_array = array_filter($array2);
 $post_url = 'API KEY';
 $body = array(
 'lead[first_name]' => rgar( $entry, '1.3' ), 
 'lead[last_name]' => rgar( $entry, '1.6' ), 
 'lead[email_address]' => rgar ($entry, '2'),
 'lead[phone_number]' => rgar( $entry, '24' ),
 'lead[company]' => rgar( $entry, '22' ),
 'lead[event_description]' => rgar( $entry, '3' ),
 'lead[event_date]' => rgar( $entry, '6' ),
 'lead[start_time]' => rgar( $entry, '7' ),
 'lead[end_time]' => rgar( $entry, '23' ),
 'lead[guest_count]' => rgar( $entry, '16' ),
 'lead[additional_information]' => implode("\n", $filtered_array)
 );
 GFCommon::log_debug( 'gform_after_submission: body => ' . print_r( $body, true ) );
$request = new WP_Http();
 $response = $request->post( $post_url, array( 'body' => $body ) );
 GFCommon::log_debug( 'gform_after_submission: response => ' . print_r( $response, true ) );
}

相关问题