首页 > 解决方案 > Function to get the embedded content and links from a Wordpress post (from 1 to all of them)

问题描述

I am making a custom design which I will use with Wordpress (not a theme). I created a function to get one, more or all embedded items in a post. It works with <img>, <audio>, <video>, <iframe> and <a> tags, but it can be easily edited to get any html tag. I use it in a loop:

if (have_posts()) : while (have_posts()) : the_post();

I tested it breefly for all tags and different amounts of items, and it works properly with all but iframes. You can get all other tags. The function gets the src attribute and then recreates the whole element as needed, with additonal attributes if needed. I'll post the whole function below, it is a little long.

So I would like to get the iframe source. I tried many different preg_match ways to get it but it did not work. It's weird that it works with 'video' when it's either written first or in if part... but no iframes.

Since I am neither wordpress nor php developer, any other remarks - be it the security concerns or me doing something wrong - I would really appreciate to be told. I would also like to know if I use properly ob_start(); and does it help to make this function easier for the server, if there are many visitors at the same time... Also if there is a better way to make the arguments for the function...

I will add later a wrapper for each individual item, which is very useful (for example create a menu from posts links), and I hope that someone might find it useful, especially when those iframes get fixed.

This is the function:

// $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;
                };
            };
        };
    }
}

标签: phphtmlwordpressiframepreg-match-all

解决方案



推荐阅读