Har*_*mar 6 php wordpress strip-tags visual-composer
我正在开发一个网络服务(API),我正在获取结果 WP_query() 函数并以 JSON 格式解析它。这将进一步用于 android 应用程序。问题是我通过查询获得的 post_content 是由视觉作曲家组成的,整个内容都是这样的标签形式
[VC_ROW][/VC_ROW][VC_COLUMN]some text[/VC_COLUMN] etc.
Run Code Online (Sandbox Code Playgroud)
我想从内容中删除/剥离所有这些短代码,并只从中检索纯文本。是否有任何视觉作曲家功能可以通过它来实现这件事
<?php
require('../../../wp-load.php');
require_once(ABSPATH . 'wp-includes/functions.php');
require_once(ABSPATH . 'wp-includes/shortcodes.php');
header('Content-Type: application/json');
$post_name = $_REQUEST['page'];
if($post_name!=''){
if($post_name=='services') {
$args = array(
'post_parent' => $page['services']['id'],
'post_type' => 'page',
'post_status' => 'published'
);
$posts = get_children($args);
foreach($posts as $po){
$services_array[] = array('id'=>$po->ID,'title'=>$po->post_title,'image'=>get_post_meta($po->ID, 'webservice_page_image',true),'description'=>preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $po->post_content));
}
$post = array(
'status'=>'ok',
'services'=>$services_array
);
echo json_encode($post);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
I want to remove/strip all these shortcode from the content and retrieve only plain text from it.
对我有用的解决方案:
$content = strip_tags( do_shortcode( $post->post_content ) );
Run Code Online (Sandbox Code Playgroud)
do_shortcode触发所有视觉作曲家短代码,从而返回 html+text;
strip_tags删除所有 html 标签并返回纯文本。
在这里,您可以尝试轻松地在数组中添加一些您需要的短代码,也可以通过下面的代码删除所有短代码。
$the_content = '[VC_ROW][VC_COLUMN]some text1[/VC_COLUMN] etc.[/VC_ROW][VC_COLUMN_INNTER width="1/3"][/VC_COLUMN_INNTER]';
$shortcode_tags = array('VC_COLUMN_INNTER');
$values = array_values( $shortcode_tags );
$exclude_codes = implode( '|', $values );
// strip all shortcodes but keep content
// $the_content = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $the_content);
// strip all shortcodes except $exclude_codes and keep all content
$the_content = preg_replace( "~(?:\[/?)(?!(?:$exclude_codes))[^/\]]+/?\]~s", '', $the_content );
echo $the_content;
Run Code Online (Sandbox Code Playgroud)
您想保留一些不能用于strip_shortcodes()此目的的短代码。