WordPress 页面显示短代码

问题描述 投票:0回答:2

我的 WordPress 页面显示类似 [vc_row][vc_column][vc_column_text] 的短代码。 帮助我找出根本原因。

我已将 WordPress sql 文件从一台主机迁移到另一台主机。

php wordpress visual-composer
2个回答
2
投票

我得到了同样的结果。我的主题是wp Baker。我激活了edge core插件和其他必需的插件,问题就解决了。


0
投票

您可以修改此 Utility Script Runner 脚本以删除不再存在但存在于页面内容中的短代码。在现场表演之前,一定要在舞台上这样做。破坏这个地方的可能性很大。这个脚本已经搁置了近一年了,老实说我不记得其中是否有任何一个没有按预期工作。在对您真正关心的任何内容运行此操作之前,请在分阶段进行大量测试。

<?php if(!defined('ABSPATH')) { die(); } 
/**
 * Utility Name: Remove and Flatten Shortcodes
 * Description: Strip Shortcodes from all posts, with several processing options
 * Supports: input
 * Version: 0.0.2
 **/

if (!class_exists('StripShortcodes')) {
    class StripShortcodes {
        var $wrappers = array(),
            $swaps = array();

        public static function Instance() {
            static $instance = null;
            if ($instance === null) {
                $instance = new self();
            }
            return $instance;
        }

        public function __construct() {
            add_filter('wp_util_script', array($this, 'strip_shortcodes_run'), 10, 3);
            add_filter('wp_util_input_html', array($this, 'strip_shortcodes_input_html'));
        }

        public function strip_shortcodes_input_html( $html ) {
            global $shortcode_tags;
            $post_types = get_post_types( array(
                'exclude_from_search' => false
            ), 'names' );
            $post_types = array_diff( $post_types, array( 'revision', 'attachment' ) );
            ?>
            <p>
                <strong>WARNING! DO NOT RUN THIS ON A LIVE SITE!</strong><br>
                This utility may cause data loss. Even restoring a backup may wipe out any changes since the last run of this utility.
            </p>
            <label>
                <input type="checkbox" name="restore" value="restore"/>
                <span>Restore From Backup</span>
            </label>
            <label>
                <span>Post Types to Process</span>
                <select name="post_types[]" multiple="multiple">
                    <option value="none" selected="selected">none</option>
                    <option value="any">all</option>
                    <?php
                    foreach( $post_types as $post_type ) {
                        echo '<option value="' . esc_attr( $post_type ) . '">' . $post_type . '</option>';
                    }
                    ?>
                </select>
            </label>
            <hr/>
            <p>
                Select what you would like to do with each shortcode.
            </p>
            <?php
            if( !empty( $shortcode_tags ) ) {
                foreach( $shortcode_tags as $tag => $callable ) {
                    ?>
                    <div class="shortcode-options-wrapper">
                        <label>
                            <span>[<?php echo $tag; ?>]</span>
                            <select class="shortcode-select" name="shortcodes[<?php echo esc_attr( $tag ); ?>]"/>
                            <option value="skip">Skip (do not process)</option>
                            <option value="strip">Remove (deletes shortcode content)</option>
                            <option value="unwrap">Unwrap Content</option>
                            <option value="parse">Flatten (parses to HTML)</option>
                            <option value="swap">Swap (Replace with another shortcode)</option>
                            </select>
                        </label>
                    </div>
                    <?php
                }
            }
            echo $this->build_form_script();
            return ob_get_clean();
        }

        private function build_form_script () {
            global $shortcode_tags;
            ob_start(); ?>
            <script type="text/javascript">
                (jQuery)(function($) {
                    'use strict';

                    var unwrap = '<div class="wrap-wrapper"><p>Wrapper for content (use "sprintf()" formatting, including the %s for the content)</p><label>Wrapper<input class="shortcode-wrap"></label></div>';
                    var swap = '<div class="swap-wrapper"><select class="shortcode-swap"><?php foreach ($shortcode_tags as $tag => $callable) { echo '<option value="' . $tag . '">' . esc_attr($tag) . '</option>'; }?></select></div>'
                    $(document).on('change', '.shortcode-select', function () {
                        var $this = $(this);
                        var name = $this.attr('name');
                        if ($this.val() == 'unwrap') {
                            $this.closest('.shortcode-options-wrapper').append(unwrap);
                            $this.closest('.shortcode-options-wrapper').find('.shortcode-wrap').attr('name', 'wrap_' + name);
                            $this.closest('.shortcode-options-wrapper').find('.swap-wrapper').remove();
                        }
                        else if ($this.val() == 'swap') {
                            $this.closest('.shortcode-options-wrapper').append(swap);
                            $this.closest('.shortcode-options-wrapper').find('.shortcode-swap').attr('name', 'swap_' + name);
                            $this.closest('.shortcode-options-wrapper').find('.wrap-wrapper').remove();
                        } else {
                            $this.closest('.shortcode-options-wrapper').find('.wrap-wrapper').remove();
                            $this.closest('.shortcode-options-wrapper').find('.swap-wrapper').remove();
                        }
                    })
                });
            </script>
            <?php return ob_get_clean();
        }

        public function strip_shortcodes_run( $legacy, $state, $atts ) {
            $batch = 10;
            $offset = 0;
            if( !empty( $state['offset'] ) ) {
                $offset = $state['offset'];
            }
            $result = array(
                'state'   => 'error',
                'message' => 'an unknown error has occurred',
            );
            $post_types = 'none';
            if( !empty( $atts['post_types'] ) && !in_array( 'none', $atts['post_types'] ) ) {
                $post_types = $atts['post_types'];
            }
            $shortcode_settings = array();
            if( !empty( $atts['shortcodes'] ) ) {
                $shortcode_settings['core']    = $atts['shortcodes'];
                $shortcode_settings['wrap']    = $atts['wrap_shortcodes'];
                $shortcode_settings['swap']    = $atts['swap_shortcodes'];
            }

            $restore = true;
            if( empty( $atts['restore'] ) ) {
                $this->replace_shortcode_functions( $shortcode_settings );
                $restore = false;
            }
            $query = new WP_Query( array(
                'posts_per_page' => $batch,
                'offset' => $offset,
                'post_type' => $post_types
            ) );
            if( !$query->have_posts() ) {
                $result = array(
                    'state' => 'complete',
                    'message' => 'successfully processed all posts'
                );
            } else {
                $offset += $query->post_count;
                while( $query->have_posts() ) {
                    $query->the_post();
                    $post = get_post();
                    $backup = get_post_meta( $post->ID, 'flatten_shortcodes_backup', true );
                    if( $restore ) {
                        if( $backup ) {
                            $post->post_content = $backup;
                            wp_update_post( $post );
                            delete_post_meta( $post->ID, 'flatten_shortcodes_backup' );
                        }
                    } else {
                        if( !$backup ) {
                            update_post_meta( $post->ID, 'flatten_shortcodes_backup', $post->post_content );
                        }
                        $post->post_content = do_shortcode( $post->post_content );
                        wp_update_post( $post );
                    }
                }
                $result = array(
                    'state' => array(
                        'offset' => $offset
                    ),
                    'message' => $offset . ' posts processed'
                );
            }

            return $result;
        }

        private function replace_shortcode_functions( $settings = array() ) {
            global $shortcode_tags;
            foreach( $shortcode_tags as $tag => $callable ) {
                $setting = 'skip';
                if( !empty( $settings['core'][$tag] ) ) {
                    $setting = $settings['core'][$tag];
                }
                switch( $setting ) {
                    case 'strip' :
                        $shortcode_tags[$tag] = "__return_empty_string";
                        break;
                    case 'unwrap':
                        $shortcode_tags[$tag] = array($this, 'replace_shortcode_unwrap');
                        $this->wrappers[$tag] = $settings['wrap'][$tag];
                        break;
                    case 'parse' :
                        // nothing needed
                        break;
                    case 'swap' :
                        $shortcode_tags[$tag] = array($this, 'swap_shortcode');
                        $this->swaps[$tag] = $settings['swap'][$tag];
                        break;
                    case 'skip'  :
                    default      :
                        unset( $shortcode_tags[$tag] );
                }

            }
        }
        public function replace_shortcode_unwrap( $atts=array(), $content='', $tag ) {
            return sprintf($this->wrappers[$tag], $content);
        }
        public function swap_shortcode( $atts=array(), $content='', $tag ) {
            $attString = '';
            $newTag = $this->swaps[$tag];
            if (!empty($atts)) {
                foreach ($atts as $key => $att) {
                    $attString .= ' ' . $key . '="' . $att . '"';
                }
            }
            $output = '[' . $newTag . $attString . ']';
            if ($content) {
                $output .= $content . '[/' . $newTag . ']';
            }
            return $output;
        }
    }

    StripShortcodes::Instance();
}
© www.soinside.com 2019 - 2024. All rights reserved.