Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


Most viewed posts


Subscribe to RSS feed


Changing settings of built-in Visual Composer elements

Stanislav KhromovStanislav Khromov

If you are working with Visual Composer, you probably want to have some block settings set by default every time you create a block. For example, when you would create an Image gallery I wanted the gallery type to be set to “Flex slider slide”, the slides interval to be set to 10 seconds and the Image Size to be set to large. Thankfully you can do this via the very useful vc_map_update() function!

Here is an example that changes settings on the default Image Gallery block. You can use this in your themes functions.php file or a plugin.

add_action('init', function()
{
    //Get VC gallery shortcode config
    $shortcode_vc_gallery_tmp = WPBMap::getShortCode('vc_gallery');

    //Loop over config to find the condition we want to change
    foreach($shortcode_vc_gallery_tmp['params'] as $key => $param)
    {
        //We found our parameter to change, woop!
        if($param['param_name'] === 'img_size')
        {
            //Add standard value for gallery size
            $shortcode_vc_gallery_tmp['params'][$key]['value'] = 'large';
        }
        else if($param['param_name'] === 'interval')
        {
            //Add standard value for gallery transition
            $shortcode_vc_gallery_tmp['params'][$key]['std'] = 10;
        }
        else if($param['param_name'] === 'type')
        {
            //Add standard value for gallery easing
            $shortcode_vc_gallery_tmp['params'][$key]['std'] = 'flexslider_slide';
        }
    }

    //print_r($shortcode_vc_gallery_tmp);

    //VC doesn't like even the thought of you changing the shortcode base, and errors out, so we unset it.
    unset($shortcode_vc_gallery_tmp['base']);

    //Update the actual parameter
    vc_map_update('vc_gallery', $shortcode_vc_gallery_tmp);
}, 100);

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

Comments 14
  • sodbileg
    Posted on

    sodbileg sodbileg

    Reply Author

    Hi Stanislav,
    Is this still working on Visual Composer 4.1.1?


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

      Hey sodbileg,

      Yes this works fine with VC 4.1.1, I am using it on a project right now. :)


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

      Hey again sodbileg,

      Upon further inspection the code did actually break in version 4.1.1, due to changed hook timings in Visual Composer.

      The fix is very simple, we just change the hook to init with a priority of 100, and everything works again. I have updated the above code to reflect those changes. They should work fine on older VC versions as well.


      • sodbileg
        Posted on

        sodbileg sodbileg

        Reply Author

        Hi Stanislav,
        Thank you so much for sharing your fix.
        After I posted a comment on your blog, I looked at VC page on CodeCanyon.
        I found a solution below from there. If you are interested, this is the solution.

        add_action('init', 'vc_map_default_shortcodes'); //replace this line with a line below
        add_action( 'plugins_loaded', 'vc_map_default_shortcodes' );

        Thank you again :)


        • Stanislav Khromov
          Posted on

          Stanislav Khromov Stanislav Khromov

          Reply Author

          Thanks sodbileg,

          It sounds like this will be fixed in the next version of VC – Looks like a lot of people are having issues with the changes in hook timings. I’ll keep track on how the issue progresses. :)


  • Reza
    Posted on

    Reza Reza

    Reply Author

    Hi Stanislav,

    Thanks for sharing your approach. It saved my time.


  • Jeff
    Posted on

    Jeff Jeff

    Reply Author

    Everytime I publish a post I get an email notification from WordPress.com. The problem is the looks like the following…

    [vc_row][vc_column width=”1/1″][vc_column_text]I am text block. .[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/1″][vc_column_text]

    This is due to using VIsual Composer to create the post. The email contains part of the body of the post which is formatted for visual composer but looks like this in HTML and plain text.

    Is there anyway to format this email so it shows up correctly?

    Thanks for any help!


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

      Hey Jeff,

      WordPress (the open-source .org version) has no feature that sends an email when a new post is published, so you’d have to check with the third party plugin you are using for this. But regardless, it’s outside the scope of this article. Hope that helps.


  • Jeff
    Posted on

    Jeff Jeff

    Reply Author

    Thank you for the response!!

    It turned out to be caused by Jetpack which is a plugin co-Authored by WordPress.com that was actually firing the email notification through WordPress.com, even though the Blog is hosted elsewhere.

    The problem with the email is that it contained Visual Composer code elements like so…
    [vc_row][vc_column width=”1/1″][vc_column_text] This is content. This is where the main body of the post belongs [/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/1″]

    I was thinking it was some kind of setting in Visual Composer. I had to disable Jetpack, which ties a blog to WordPress.com. Doing this stopped the email.

    Thank you very much for the feedback!

    https://yellzbellz.com


  • Ngô Văn Thoại
    Posted on

    Ngô Văn Thoại Ngô Văn Thoại

    Reply Author

    I can’t get ‘type’ => ‘param_group’ in file shortcode, help me, thanks all


  • Ramon Fincken
    Posted on

    Ramon Fincken Ramon Fincken

    Reply Author

    Working on WP 4.9.8 with VC: 5.5.2
    Linked to this article when overriding the default single image element settings:
    https://www.mijnpress.nl/2018/override-visual-composer-default-element-settings-example-included/