Php 函数从Wordpress帖子中获取嵌入内容和链接(从1到全部)
我正在做一个定制设计,我将使用Wordpress(不是主题)。 我创建了一个函数来获取帖子中的一个、多个或所有嵌入项。 它与Php 函数从Wordpress帖子中获取嵌入内容和链接(从1到全部),php,html,wordpress,iframe,preg-match-all,Php,Html,Wordpress,Iframe,Preg Match All,我正在做一个定制设计,我将使用Wordpress(不是主题)。 我创建了一个函数来获取帖子中的一个、多个或所有嵌入项。 它与 所以我想获得iframe源代码。我尝试了许多不同的preg\u match方法来获得它,但没有成功。 奇怪的是,它与“视频”一起工作时,要么是先写的,要么是在if部分。。。但是没有iframes 由于我既不是wordpress也不是php开发人员,因此任何其他评论-无论是出于安全考虑还是我做错了什么-如果有人告诉我,我将非常感激。 我还想知道我是否正确使用ob_start
所以我想获得iframe源代码。我尝试了许多不同的preg\u match方法来获得它,但没有成功。
奇怪的是,它与“视频”一起工作时,要么是先写的,要么是在if部分。。。但是没有iframes
由于我既不是wordpress也不是php开发人员,因此任何其他评论-无论是出于安全考虑还是我做错了什么-如果有人告诉我,我将非常感激。
我还想知道我是否正确使用ob_start();如果同时有许多访问者,这是否有助于使服务器更容易使用此功能。。。
另外,如果有更好的方法来生成函数的参数
稍后,我将为每个单独的项目添加一个包装器,这非常有用(例如,从帖子链接创建菜单),我希望有人会发现它很有用,特别是当那些iFrame得到修复时
这就是功能:
if (have_posts()) : while (have_posts()) : the_post();
/$universal\u用于img大小(“缩略图”)或链接目标“\u blank”的修饰符
//示例:获取“定制的”发布内容(“链接”、“全部”、“链接类”、“空白”);
//示例:获取自定义的发布内容(“图像”,1,“图像类”,“自定义缩略图”);
函数获取自定义的发布内容($item\u type=null,$items\u num=null,$item\u classes=null,$universal\u modifier=null)
{
//PHP在到达脚本末尾时自动刷新打开的输出缓冲区
ob_start();
全球$员额;
$single_img=假;
如果($items\u num){
如果($item_type==='image'){
如果($items\u num==1&&$universal\u修饰符){
$single_img=真;
//这将获得特征图像,允许
//“完整”img及其源集
_post_缩略图($universal_修饰符,数组)(
“类”=>esc\U属性($item\U类),
'alt'=>esc_html(获取标题())
));
}否则{
preg_match_all('//i',$post->post_content,$item_src);
$additional_attr='';
$the_html_tag='/i',$post->content,$item_src);
$additional_attr='preload=none loading=lazy controls';
$the_html_标记='post_content,$anchor_text);
if($universal\u修饰符){
$additional_attr='target='。$universal_修饰符;
}否则{
$additional_attr='';
}
$the_html_tag=''。$item_content[1][$i]。$close_tag;
echo$the_项目;
};
};
};
}
}
我不得不更改函数,我认为现在更好了。它工作完全,易于使用。功能相当长
我使用了var\u dump($post->post\u content)
,发现post内容中没有iframe。它们只在页面上的输出中得到响应
因此,我使用带有适当媒体类型的get\u media\u embedded\u in\u content()
来获取所需内容,然后使用preg\u match()
来获取所需内容
当每个帖子只需要一个图像时,该函数会获取帖子缩略图“full”(帖子必须有特色图像–有一个自动创建的插件),该插件会返回带有源集的图像,从而使其响应。对于每篇文章的更多图像,它将返回完整的图像大小
如果需要的话,我为每个单独的项目添加了一个包装。可以向其中添加类。
它对于在中包装图像很有用;
如果($item\u wrap){
回声';
};
};
}elseif($item_type==='video'){
对于($i=0;$i<$items_总计;$i++){
preg_match(“//i”,$the_item_media[$i],$item_src);
如果($item\u wrap){
回声';
};
如果($item_src[1]){
回声';
}否则{
preg_match(“//i”,$the_item_media[$i],$item_src);
如果($item_src[1]){
回声';
}否则{
echo'”.esc_html('Media not found.')。
”;
};
};
如果($item\u wrap){
回声';
};
};
}elseif($item_type==='audio'){
对于($i=0;$i<$items_总计;$i++){
preg_match(“//i”,$the_item_media[$i],$item_src);
如果($item\u wrap){
回声';
};
如果($item_src[1]){
回声';
}否则{
echo'”.esc_html('Media not found.')。
”;
};
如果($item\u wrap){
回声';
};
};
}否则{
echo'”.esc_html('Nothing found')。
”;
};
}否则{
echo'”.esc_html('Media not found.')。
”;
};
};
// $universal_modifier for img size ('thumbnail') or link target '_blank'
// example: get_the_customized_post_content('link', 'all', 'link-class', '_blank');
// example: get_the_customized_post_content('image', 1, 'image-class', 'custom-thumbnail');
function get_the_customized_post_content($item_type = null, $items_num = null, $item_classes = null, $universal_modifier = null)
{
// PHP automatically flushes open output buffers when it reaches the end of a script
ob_start();
global $post;
$single_img = false;
if ($items_num) {
if ($item_type === 'image') {
if ($items_num === 1 && $universal_modifier) {
$single_img = true;
// this will get the featured image, which allows for getting
// the 'full' img with its sourceset
the_post_thumbnail($universal_modifier, array(
'class' => esc_attr($item_classes),
'alt' => esc_html(get_the_title())
));
} else {
preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $item_src);
$additional_attr = '';
$the_html_tag = '<img';
$close_tag = '';
}
} elseif ($item_type === 'audio') {
preg_match_all('/<audio.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->content, $item_src);
$additional_attr = 'preload=none loading=lazy controls';
$the_html_tag = '<audio';
$close_tag = '</audio>';
} elseif ($item_type === 'video') {
preg_match_all('/<iframe.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $item_src);
$additional_attr = 'loading=lazy frameborder=0 allowfullscreen';
$the_html_tag = '<iframe';
$close_tag = '</iframe>';
if (count($item_src[1]) === 0) {
echo 'Getting videos';
preg_match_all('/<video.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $item_src);
$additional_attr = 'preload=metadata loading=lazy controls';
$the_html_tag = '<video';
$close_tag = '</video>';
}
} elseif ($item_type === 'link') {
preg_match_all('/<a.+href=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $item_src);
preg_match_all('/<a.+>([^\'"]+)<\/a>/i', $post->post_content, $anchor_text);
if ($universal_modifier) {
$additional_attr = 'target=' . $universal_modifier;
} else {
$additional_attr = '';
}
$the_html_tag = '<a';
$close_tag = '</a>';
} else {
echo '<p class="' . esc_attr('not-found-info') . '">' . esc_html('Media not found...') . '</p>';
}
}
if ($single_img) {
$display_item = ob_get_clean();
echo $display_item;
} else {
if (count($item_src[1]) === 0) {
echo '<p class="' . esc_attr('not-found-info') . '">' . esc_html('Media not found...') . '</p>';
} else {
$num_of_items = count($item_src[0]);
if ($items_num === 'all') {
$items_total = $num_of_items;
} else {
$items_total = min($num_of_items, $items_num);
// get the smaller number of the two
}
if ($items_total > 0) {
for ($i = 0; $i < $items_total; $i++) {
if ($item_type === 'link') {
$source_type = 'href';
$item_content = $anchor_text;
} else {
$source_type = 'src';
$item_content = '';
}
$the_item = $the_html_tag . ' class="' . esc_attr($item_classes) . '" ' . $source_type . '="' . esc_url($item_src[1][$i]) . '" ' . esc_attr($additional_attr) . '>' . $item_content[1][$i] . $close_tag;
echo $the_item;
};
};
};
}
}
if (have_posts()) : while (have_posts()) : the_post();
get_the_customized_post_content(
$item_type = 'image',
$items_num = 'all',
$item_classes = 'item-image-class class-two',
$item_wrap = 'a',
$item_wrap_classes = 'item-wrap-class one-more-class',
$the_link_target = '_blank'
);
endwhile;
wp_reset_postdata();
endif;
query_posts(array(
'category_name' => 'your-cat-name',
'posts_per_page' => 1
));
if (have_posts()) : while (have_posts()) : the_post();
get_the_customized_post_content(
$item_type = 'audio',
$items_num = 1,
$item_classes = 'item-audio-class',
$item_wrap = 'div',
$item_wrap_classes = 'item-wrap-class classs-two',
$link_target = false
);
endwhile;
wp_reset_postdata();
wp_reset_query();
endif;
// remove Featured Image width/height inline attributes
function remove_img_size_attr($html)
{
$html = preg_replace('/(width|height)="\d+"\s/', "", $html);
return $html;
}
add_filter('post_thumbnail_html', 'remove_img_size_attr', 10);
add_filter('image_send_to_editor', 'remove_img_size_attr', 10);
// remove Featured Image classes
remove_action('begin_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_add');
function get_the_customized_post_content(
$item_type = null,
$items_num = null,
$item_classes = null,
$item_wrap = null,
$item_wrap_classes = null,
$link_target = null
) {
// PHP automatically flushes open output buffers when it reaches the end of a script
ob_start();
global $post;
$not_found_class = 'media-not-found'; // class for the p tag container for "Nothing found" info
if ($item_type && $items_num) {
if ($item_type === 'video') {
$the_item_media = get_media_embedded_in_content(apply_filters('the_content', get_the_content(), -1), array('video', 'iframe'));
$num_of_items = count($the_item_media);
} elseif ($item_type === 'audio') {
$the_item_media = get_media_embedded_in_content(apply_filters('the_content', get_the_content(), -1), array('audio'));
$num_of_items = count($the_item_media);
} elseif ($item_type === 'image') {
if ($items_num === 1) {
// this will get the featured image, which allows for getting
// the 'full' img with its sourceset
// it's buffered, otherwise it echos automatically
the_post_thumbnail('full', array(
'class' => esc_attr($item_classes),
'alt' => esc_attr(get_the_title())
));
$featured_img = ob_get_clean();
$the_item_url = get_the_post_thumbnail_url(get_the_ID(), 'full');
$num_of_items = 1;
} else {
preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $item_src);
$the_item_media = $item_src[1];
$the_item_url = $item_src[1];
$the_title_attr = $item_src[1];
$num_of_items = count($the_item_media);
};
} elseif ($item_type === 'link') {
preg_match_all('/<a.+href=[\'"]([^\'"]+)[\'"].*>([^\'"]+)<\/a>/i', $post->post_content, $link_parts);
$num_of_items = count($link_parts[0]);
} else {
echo '<p class="' . esc_attr($not_found_class) . '">' . esc_html('Please enter the item type.') . '</p>';
};
} else {
echo '<p class="' . esc_attr($not_found_class) . '">' . esc_html('Please enter the item type and the number of items to return...') . '</p>';
};
if ($num_of_items > 0) {
if ($link_target) {
$link_target_window = 'target=' . esc_attr($link_target);
} else {
$link_target_window = '';
};
if ($items_num === 'all') {
$items_total = $num_of_items;
} else {
$items_total = min($num_of_items, $items_num);
// get the smaller number of the two
};
if ($item_type === 'image') {
if ($items_num === 1) {
if ($item_wrap) {
if ($item_wrap === 'a') {
echo '<a class="' . esc_attr($item_wrap_classes) . '" href="' . esc_url($the_item_url) . '" ' . $link_target_window . '>';
} else {
echo '<' . $item_wrap . ' class="' . esc_attr($item_wrap_classes) . '">';
}
};
echo $featured_img;
if ($item_wrap) {
echo '</' . $item_wrap . '>';
}
} else {
for ($i = 0; $i < $items_total; $i++) {
if ($item_wrap) {
if ($item_wrap === 'a') {
echo '<a class="' . esc_attr($item_wrap_classes) . '" href="' . esc_url($the_item_url[$i]) . '" ' . $link_target_window . '>';
} else {
echo '<' . $item_wrap . ' class="' . esc_attr($item_wrap_classes) . '">';
}
};
$img_src = pathinfo($item_src[1][$i]);
$img_alt_tag = $img_src['filename'];
$fix_filename = array();
$fix_filename[0] = '/-/';
$fix_filename[1] = '/_/';
$fix_filename[2] = '/\s\s+/';
$img_alt_tag = ucwords(preg_replace($fix_filename, ' ', $img_alt_tag));
echo '<img class="' . esc_attr($item_classes) . '" alt="' . esc_attr($img_alt_tag) . '" src="' . esc_url($item_src[1][$i]) . '" loading=' . esc_attr('lazy') . '>';
if ($item_wrap) {
echo '</' . $item_wrap . '>';
}
};
}
} elseif ($item_type === 'link') {
for ($i = 0; $i < $items_total; $i++) {
if ($item_wrap) {
echo '<' . $item_wrap . ' class="' . esc_attr($item_wrap_classes) . '">';
};
echo '<a class="' . esc_attr($item_classes) . '" href="' . esc_url($link_parts[1][$i]) . '" ' . esc_attr($link_target_window) . '>' . esc_html($link_parts[2][$i]) . '</a>';
if ($item_wrap) {
echo '</' . $item_wrap . '>';
};
};
} elseif ($item_type === 'video') {
for ($i = 0; $i < $items_total; $i++) {
preg_match('/<iframe.+src=[\'"]([^\'"]+)[\'"].*>/i', $the_item_media[$i], $item_src);
if ($item_wrap) {
echo '<' . $item_wrap . ' class="' . esc_attr($item_wrap_classes) . '">';
};
if ($item_src[1]) {
echo '<iframe class="' . esc_attr($item_classes) . '" loading="lazy" src="' .
esc_url($item_src[1]) . '" frameborder="0" allowfullscreen></iframe>';
} else {
preg_match('/<video.+src=[\'"]([^\'"]+)[\'"].*>/i', $the_item_media[$i], $item_src);
if ($item_src[1]) {
echo '<video class="' . esc_attr($item_classes) . '" loading="lazy" src="' .
esc_url($item_src[1]) . '" preload="metadata" controls></video>';
} else {
echo '<p class="' . esc_attr($not_found_class) . '">' . esc_html('Media not found.') . '</p>';
};
};
if ($item_wrap) {
echo '</' . $item_wrap . '>';
};
};
} elseif ($item_type === 'audio') {
for ($i = 0; $i < $items_total; $i++) {
preg_match('/<audio.+src=[\'"]([^\'"]+)[\'"].*>/i', $the_item_media[$i], $item_src);
if ($item_wrap) {
echo '<' . $item_wrap . ' class="' . esc_attr($item_wrap_classes) . '">';
};
if ($item_src[1]) {
echo '<audio class="' . esc_attr($item_classes) . '" loading="lazy" src="' .
esc_url($item_src[1]) . '" preload="none" controls></audio>';
} else {
echo '<p class="' . esc_attr($not_found_class) . '">' . esc_html('Media not found.') . '</p>';
};
if ($item_wrap) {
echo '</' . $item_wrap . '>';
};
};
} else {
echo '<p class="' . esc_attr($not_found_class) . '">' . esc_html('Nothing found.') . '</p>';
};
} else {
echo '<p class="' . esc_attr($not_found_class) . '">' . esc_html('Media not found.') . '</p>';
};
};