0

I have a problem about Visual Compser Backend not showing any elements, it only shows the code in the classic mode editor.

See Screenshot

Warning: preg_match_all(): Compilation failed: invalid range in character class at offset 11 in /home/customer/www/******.com/public_html/wp-content/plugins/js_composer/include/autoload/hook-vc-grid.php on line 162

This is line 162
preg_match_all( "/$pattern/", $post->post_content, $found ); // fetch only needed shortcodes

```public function gridSavePostSettingsId( array $settings, $post_id, $post ) {
    $pattern = $this->getShortcodeRegexForId();
    preg_match_all( "/$pattern/", $post->post_content, $found ); // fetch only needed shortcodes
    $settings['vc_grid_id'] = array();
    if ( is_array( $found ) && ! empty( $found[0] ) ) {
        $to_save = array();
        if ( isset( $found[1] ) && is_array( $found[1] ) ) {
            foreach ( $found[1] as $key => $parse_able ) {
                if ( empty( $parse_able ) || '[' !== $parse_able ) {
                    $id_pattern = '/' . $this->grid_id_unique_name . '\:([\w-_]+)/';
                    $id_value = $found[4][ $key ];

                    preg_match( $id_pattern, $id_value, $id_matches );

                    if ( ! empty( $id_matches ) ) {
                        $id_to_save = $id_matches[1];

                        // why we need to check if shortcode is parse able?
                        // 1: if it is escaped it must not be displayed (parsed)
                        // 2: so if 1 is true it must not be saved in database meta
                        $shortcode_tag = $found[2][ $key ];
                        $shortcode_atts_string = $found[3][ $key ];
                        /** @var $atts array */
                        $atts = shortcode_parse_atts( $shortcode_atts_string );
                        $content = $found[6][ $key ];
                        $data = array(
                            'tag' => $shortcode_tag,
                            'atts' => $atts,
                            'content' => $content,
                        );

                        $to_save[ $id_to_save ] = $data;
                    }
                }
            }
        }
        if ( ! empty( $to_save ) ) {
            $settings['vc_grid_id'] = array( 'shortcodes' => $to_save );
        }
    }

    return $settings;
}```

I tried disabling all plug-ins and rollback wordpress version but still the same. Is this problem related to the warning above? I know very little about coding. Please point me in the right direction.

Thank you!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Oddity
  • 11
  • 1
  • Does this answer your question? [preg\_match(): Compilation failed: invalid range in character class at offset](https://stackoverflow.com/a/56551371/3832970) – Wiktor Stribiżew Mar 06 '20 at 07:53
  • `[\w-_]` is wrong, it must be `[\w-]` – Wiktor Stribiżew Mar 06 '20 at 07:54
  • Hi @wiktor, Thank you so much for your quick response and suggestions. I haven't found a fix yet but I found a temporary fix for my problem.by changing my PHP version from 7.3.15 (which is the recommend version) to 7.1.33. It'll do for now :) – Oddity Mar 06 '20 at 08:47
  • Change `'\:([\w-_]+)/'` with `':([\w-]+)/'`. Though it is not clear what regex you have in `$pattern = $this->getShortcodeRegexForId()` – Wiktor Stribiżew Mar 06 '20 at 08:49

2 Answers2

1

Exactly same problem back-end mode didn't show any elements and /hook-vc-grid.php on line 162 error. " [\w-_] is wrong, it must be [\w-] " this helps only dissappear error but not solution for not showing elements.

Edit: php 7.1 works. It solved elements thing also. Thank you guys

  • The underscore '\_' shoud not be removed, but the hyphen '-' should be escaped. So the answer should be: "[\w\-\_]" (See my complete answer with explanation below) – Sjoerd Linders Dec 01 '22 at 10:59
0

All hyphen '-' characters (that are not used as a range character) should be escaped '\-' as of PHP 7.3 so that it will NOT be treated anymore as a range character. Therefor the following WordPress function needs to be changed this way to make this expression work the same again:

private function getShortcodeRegexForId() {
    return '\\['           // Opening bracket
        . '(\\[?)'         // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
        . '([\\w\-_]+)'    // 2: Shortcode name
        . '(?![\\w\-])'    // Not followed by word character or hyphen
Sjoerd Linders
  • 447
  • 5
  • 8