Ngā take me ngā otinga mō te kore e tātarihia ngā waehere poto o te wāhanga kōrero WordPress

WordPress Whakauruhia ki te wāhanga kōrero o te tuhinga [shortcode]Heoi, ko te hua anake e whakaatu ana i te tuhinga māmā, kaua ko te hua e hiahiatia ana? Koinei te tikanga taunoa a WordPress. Kei raro nei, ka tātarihia e au ngā take me te whakarato i tētahi otinga katoa.

He aha te wāhanga kōrero kāore e tautoko i ngā waehere poto?

  • Ngā here taunoa
    Hei take haumarutanga, kāore a WordPress e rere aunoa i te wāhanga kōrero. do_shortcode()Mā tēnei ka ārai i ngā kaiwhakamahi i te whakauru noa i ngā puka, i ngā tuhinga, i ētahi atu ihirangi rānei ka pā ki te haumarutanga o te pae.

  • Te whānuitanga iti o te tono
    Ko te whakarāpopototanga he mea hanga mō te whakamahi i roto i ngā tuhinga (the_contentKei roto i tēnei ko ngā taputapu me ngā monomai motuhake, engari kāore te wāhanga kōrero e tautokona ana e te taunoa.

  • Te pokanoa hōputu aunoa
    Ko te tuhinga kōrero ka wpautop Tāpiri aunoa <p> A <br> Ngā tūtohu, ka pakaru pea te hanganga wetereo o ngā waehere poto, ka kore ai e taea te wetewete.

Ngā take me ngā otinga mō te kore e tātarihia ngā waehere poto o te wāhanga kōrero WordPress

Te tukatuka...

Me pēhea taku whakahohe i te tautoko waehere poto mō ngā kōrero?

  • Whakahohehia ngā waehere poto o ngā kōrero
    Mō te kaupapa functions.php Tāpirihia te waehere e whai ake nei ki tō kōnae, ki tō monomai rānei mō te wāhanga waehere:

    add_filter('comment_text', 'do_shortcode');
    

    Mā tēnei ka whakamahia ngā waehere poto e te wāhanga kōrero.

  • Tirohia mēnā kua whakahohehia te monomai.
    He maha ngā waehere poto e ahu mai ana i ngā monomai (pērā i te Contact Form 7 me WooCommerce). Ki te kore te monomai e whakahohea, e kore ngā waehere poto e mahi.

  • Ārai i ngā hapa wetereo
    Whakaūtia mai mēnā kua tika te tuhi o te whakarāpopototanga, hei tauira:

    [shortcode attribute="value"]内容[/shortcode]
    
  • Tirohia mēnā he pakanga kaupapa, he pakanga monomai rānei
    Huri ki te kaupapa taunoa (pēnei i te Twenty Twenty-Five), monohia ia monomai takitahi hei whakamātautau me te whakaū mēnā he taupatupatu e raru ai te mahi a te waehere poto.

  • marama keteroki
    Ina whakamahi ana i ngā monomai keteroki, i ngā CDN rānei, me ūkui e koe te keteroki i muri i te whakatikatika kia kite ai i te hua.

Waehere hei whakahohe haumaru i ngā waehere poto o te wāhanga kōrero

Hei karo i ngā tūponotanga haumaru, ka taea te functions.php Tāpirihia te tauira katoa e whai ake nei:

<?php
// 在评论区启用简码解析
add_filter('comment_text', 'do_shortcode');

// 限制允许的简码列表
function safe_comment_shortcodes($content) {
    // 定义允许的简码(加入 su_posts)
    $allowed_shortcodes = array('gallery', 'audio', 'video', 'contact-form-7', 'su_posts');

    // 遍历评论内容中的简码
    return preg_replace_callback('/\[(\w+)([^\]]*)\]/', function($matches) use ($allowed_shortcodes) {
        $shortcode = $matches[1];
        // 如果简码在允许列表中,正常解析
        if (in_array($shortcode, $allowed_shortcodes)) {
            return do_shortcode($matches[0]);
        }
        // 否则直接输出原始文本,避免执行
        return esc_html($matches[0]);
    }, $content);
}
add_filter('comment_text', 'safe_comment_shortcodes', 9);

whakaahuatanga waehere

  • Whakahohehia ngā waehere potoRaina tuatahi add_filter('comment_text', 'do_shortcode'); Whakahohehia te tautoko waehere poto i te wāhanga kōrero.
  • Ngā Waehere Poto Kua Herea:haere $allowed_shortcodes Hei tauira, ka taea e ngā whakamāramatanga o te rarangi te whakarāpopototanga gallery,audio,video,contact-form-7,su_posts.
  • Te whakahaere haumaruKi te tāuru te kaiwhakamahi i tētahi waehere poto kāore i te rārangi whakaaetia, ka mawhiti aunoa te waehere ki te tuhinga noa hei karo i te whakahaere i ngā waehere poto morearea pea.

Nga Mahi Pai

  • Whakaaetia ngā waehere poto e tika ana anakeHei tauira, i roto i ngā whakaaturanga pāpāho, i ngā puka rānei, kaua e whakamahi i ngā waehere poto maha rawa.
  • Tirohia auau mō ngā whakahōunga monomaiMe whakarite kia haumaru, kia pono hoki te monomai pūtake waehere poto.
  • Te whakakotahi i ngā monomai pupuri me te tiakiHei karo i te pēhanga mahi i puta mai i te whakaputanga waehere poto.

Putanga Whakarei Ake: Atanga Tautuhinga Whakamuri (Rapu + Whakarōpū + Tohutohu o te Mua)

Mena kei te hiahia koe kia nui ake te ngāwari, ka taea e koe te whakamahi i te waehere whakarei ake e whai ake nei:

  • Whakarārangihia aunoa ngā waehere poto katoa kua rēhitatia(Tae atu ki ngā mea e whakaratohia ana e ngā monomai).
  • Tautoko ana i te whakauru i ngā waehere poto ritenga(如 )。
  • pouaka rapu:tereWhakatūngaWhakarāpopototanga.
  • Whakaaturanga rōpūMā te whakarōpū mā te pūtake monomai ka māmā ake te whakamahi.
  • Ngā Whakaaetanga TūrangaKo ngā waehere poto anake i roto i ngā kōrero a ngā kaiwhakahaere ka wetewetehia; mā ngā waehere poto i roto i ngā kōrero a ngā kaiwhakamahi noa ka whakaatuhia te karere "E wātea ana tēnei waehere poto ki ngā kaiwhakahaere anake".
<?php
// 添加后台菜单
function cwl_shortcode_settings_menu() {
    add_options_page(
        '评论简码设置',
        '评论简码设置',
        'manage_options',
        'cwl-shortcode-settings',
        'cwl_shortcode_settings_page'
    );
}
add_action('admin_menu', 'cwl_shortcode_settings_menu');

// 注册设置
function cwl_register_shortcode_settings() {
    register_setting('cwl_shortcode_settings_group', 'cwl_allowed_shortcodes');
    register_setting('cwl_shortcode_settings_group', 'cwl_custom_shortcodes');
}
add_action('admin_init', 'cwl_register_shortcode_settings');

// 设置页面内容
function cwl_shortcode_settings_page() {
    global $shortcode_tags;
    $allowed = get_option('cwl_allowed_shortcodes', array());
    $custom  = get_option('cwl_custom_shortcodes', array());

    ?>
    <div class="wrap">
        <h1>评论区简码设置</h1>
        <form method="post" action="options.php">
            <?php settings_fields('cwl_shortcode_settings_group'); ?>

            <h2>搜索简码</h2>
            <input type="text" id="cwl_shortcode_search" placeholder="输入简码关键字..." style="width:300px;">

            <h2>已注册简码(按插件来源分组)</h2>
            <p>勾选允许在评论区执行的简码:</p>
            <div id="cwl_shortcode_list">
                <?php
                // 按插件来源分组
                $groups = array();
                foreach ($shortcode_tags as $tag => $callback) {
                    $source = is_array($callback) ? get_class($callback[0]) : (is_object($callback) ? get_class($callback) : '主题/未知来源');
                    $groups[$source][] = $tag;
                }

                foreach ($groups as $source => $tags) {
                    echo "<h3>" . esc_html($source) . "</h3>";
                    foreach ($tags as $tag) {
                        ?>
                        <label class="cwl_shortcode_item">
                            <input type="checkbox" name="cwl_allowed_shortcodes[]" value="<?php echo esc_attr($tag); ?>" <?php checked(in_array($tag, $allowed)); ?>>
                            <?php echo esc_html($tag); ?>
                        </label><br>
                        <?php
                    }
                }
                ?>
            </div>

            <h2>自定义简码</h2>
            <p>输入额外允许的简码(用逗号分隔):</p>
            <textarea name="cwl_custom_shortcodes" rows="3" cols="50"><?php echo esc_textarea(implode(',', (array)$custom)); ?></textarea>

            <?php submit_button(); ?>
        </form>
    </div>

    <script>
    // 简码搜索功能
    document.getElementById('cwl_shortcode_search').addEventListener('keyup', function() {
        var keyword = this.value.toLowerCase();
        document.querySelectorAll('.cwl_shortcode_item').forEach(function(item) {
            var text = item.textContent.toLowerCase();
            item.style.display = text.indexOf(keyword) > -1 ? '' : 'none';
        });
    });
    </script>
    <?php
}

// 评论区简码过滤(结合角色权限 + 前端提示)
function cwl_safe_comment_shortcodes($content) {
    $allowed_shortcodes = get_option('cwl_allowed_shortcodes', array());
    $custom_shortcodes  = get_option('cwl_custom_shortcodes', array());
    $allowed_shortcodes = array_merge($allowed_shortcodes, (array)$custom_shortcodes);

    return preg_replace_callback('/\[(\w+)([^\]]*)\]/', function($matches) use ($allowed_shortcodes) {
        $shortcode = $matches[1];
        // 管理员可解析简码
        if (current_user_can('manage_options')) {
            if (in_array($shortcode, $allowed_shortcodes)) {
                return do_shortcode($matches[0]);
            }
            return esc_html($matches[0]);
        } else {
            // 普通用户提示信息
            if (in_array($shortcode, $allowed_shortcodes)) {
                return '<span style="color:red;">此简码仅管理员可用</span>';
            }
            return esc_html($matches[0]);
        }
    }, $content);
}
add_filter('comment_text', 'cwl_safe_comment_shortcodes', 9);

总结

Kāore ngā wāhanga kōrero a WordPress e wetewete i ngā whakarāpopototanga i te taunoa mō ngā take haumarutanga me te mahi. Ka taea e koe te whakamahi i [te taputapu wetewete] hei utu. functions.php Tāpirihia he tātari hei whakahohe i ngā waehere poto, ā, whakakotahihia ki te haumarutanga.

发表 评论

Ka kore e whakaputaina to wahitau imeera. 必填 项 已 用 * Tapanga

Panuku ki te Runga