0

I am using Site Origins Page Builder plugin and want it to install when my theme is installed, so to do that I am using TGMPluginActivation. This correctly installs and activates a bunch of plugins. However, for Page Builder and its complimentary plugin Widgets Bundle I want to set a few defaults when the plugins are activated, such as ensuring all the widgets are installed and for Page Builder that Tablet responsive is turned on and the correct breakpoints set.

The code I am using is

<?php
/*
     ============================================
        Page Builder and Widgets Integration
     ============================================
*/ 

if (!function_exists('activateAllSiteOriginWidgets')):
    function activateAllSiteOriginWidgets()
    {
        global $wpdb;
        $table_name = $wpdb->prefix . "options";

        $sow_button = array('widget_sow-button', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $sow_google_map = array('widget_sow-google-map', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $sow_image = array('widget_sow-image', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $sow_slider = array('widget_sow-slider', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $sow_post_carusel = array('widget_sow-post-carousel', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $sow_editor = array('widget_sow-editor', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $widgets_active = array('siteorigin_widgets_active', 'a:17:{s:6:"button";b:1;s:10:"google-map";b:1;s:5:"image";b:1;s:6:"slider";b:1;s:13:"post-carousel";b:1;s:6:"editor";b:1;s:3:"cta";b:1;s:8:"features";b:1;s:7:"contact";b:1;s:8:"headline";b:1;s:4:"hero";b:1;s:10:"image-grid";b:1;s:20:"social-media-buttons";b:1;s:11:"testimonial";b:1;s:14:"simple-masonry";b:1;s:11:"price-table";b:1;s:5:"video";b:1;}', 'yes');
        $sow_cta = array('widget_sow-cta', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $sow_features = array('widget_sow-features', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $contact_form = array('widget_sow-contact-form', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $sow_headline = array('widget_sow-headline', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $sow_hero = array('widget_sow-hero', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $image_grid = array('widget_sow-image-grid', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $social_media = array('widget_sow-social-media-buttons', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $sow_testimonials = array('widget_sow-testimonials', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $sow_simple_masonry = array('widget_sow-simple-masonry', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $price_table = array('widget_sow-price-table', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');
        $show_wideo = array('widget_sow-video', 'a:1:{s:12:"_multiwidget";i:1;}', 'yes');


        $options_array = array($sow_button, $sow_google_map, $sow_image, $sow_slider, $sow_post_carusel, $sow_editor, $widgets_active, $sow_cta, $sow_features, $contact_form, $sow_headline, $sow_hero, $image_grid, $social_media, $sow_testimonials, $sow_simple_masonry, $price_table, $show_wideo);
        foreach ($options_array as $option) {
            $option_name = $option[0];
            $option_content = $option[1];
            $option_active = $option[2];
            $data = array('option_name' => $option_name, 'option_value' => $option_content, 'autoload' => $option_active);
            $sql = "SELECT `option_name` FROM '$table_name' WHERE `option_name` = '$option_name'";
            $check_option = $wpdb->get_results($sql);
            if ($check_option) {
                $where = array('option_name' => $option_name);
                $wpdb->update($table_name, $data, $where);
            } else {
                $wpdb->insert($table_name, $data);
            }
        }
        //activate panels directory for current user
        $user = get_current_user_id();
        update_user_meta( $user, 'so_panels_directory_enabled', true );

//--------------THIS IS THE PART THAT DOESN'T RUN
        // Register SiteOrigin options, make the tablet layout acticve and set the breaks points to 991 for tables and 480 for mobile to match Bootstrap
        $SiteOriginOptions = 'a:14:{s:10:"post-types";a:2:{i:0;s:4:"page";i:1;s:4:"post";}s:10:"title-html";s:39:"<h3 class="widget-title">{{title}}</h3>";s:16:"add-widget-class";b:1;s:15:"bundled-widgets";b:0;s:19:"recommended-widgets";b:1;s:10:"responsive";b:1;s:13:"tablet-layout";b:1;s:12:"tablet-width";i:991;s:12:"mobile-width";i:480;s:13:"margin-bottom";i:30;s:22:"margin-bottom-last-row";b:0;s:12:"margin-sides";i:30;s:20:"full-width-container";s:4:"body";s:12:"copy-content";b:1;}';
        $data_options = array('option_name' => 'siteorigin_panels_settings', 'option_value' => $SiteOriginOptions, 'autoload' => 'yes');
        $where_options = array('option_name' => 'siteorigin_panels_settings');
        $wpdb->update($table_name, $data_options, $where_options);


    }
endif;

add_action( 'activated_plugin', 'activateAllSiteOriginWidgets' );


?>

For the widgets the code works fine, and for the Page Builder it doesn't appear to run the code when the plugin is installed and activated by the TGMPA, howeve, if I switch themes or de-activate, activate the plugin, then it does work.

The TGMPA part of the code I have is;

array(
    'name'               => 'Site Origins Page Builder', 
    'slug'               => 'siteorigin-panels', 
    'source'             => get_template_directory() . '/plugins/so-page-builder.zip',
    'required'           => true,
    'version'            => '',
    'force_activation'   => true,
    'force_deactivation' => true,
    'external_url'       => '',
    'is_callable'        => '',
),

array(
    'name'               => 'Site Origins Widgets Bundle', 
    'slug'               => 'so-widgets-bundle', 
    'source'             => get_template_directory() . '/plugins/so-widgets-bundle.zip',
    'required'           => true,
    'version'            => '',
    'force_activation'   => true,
    'force_deactivation' => true,
    'external_url'       => '',
    'is_callable'        => '',
),

Any ideas why the Widgets update code runs but not the Page Builder code?

  • bypassing the options API is probably a bad idea. – Milo May 15 '16 at 17:52
0

Why not try simply:

$SiteOriginOptions = 'a:14:{s:10:"post-types";a:2:{i:0;s:4:"page";i:1;s:4:"post";}s:10:"title-html";s:39:"<h3 class="widget-title">{{title}}</h3>";s:16:"add-widget-class";b:1;s:15:"bundled-widgets";b:0;s:19:"recommended-widgets";b:1;s:10:"responsive";b:1;s:13:"tablet-layout";b:1;s:12:"tablet-width";i:991;s:12:"mobile-width";i:480;s:13:"margin-bottom";i:30;s:22:"margin-bottom-last-row";b:0;s:12:"margin-sides";i:30;s:20:"full-width-container";s:4:"body";s:12:"copy-content";b:1;}';
update_option('siteorigin_panels_settings', $SiteOriginOption);

Also, you may want to set a flag to say the options have been initiated so the user can change them later and not have them overridden whenever they activate a plugin.

| improve this answer | |
  • Tried it, but it did not work. – Naz May 15 '16 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.