Wordpress - create customizer objects from wpdb response - php

So I have the following code:
add_action('customize_register', 'homepage_sections');
//products
function homepage_sections($wp_customize){
$wp_customize->add_panel('homepage_sections', array(
'title' => 'Homepage Sections',
'priority' => '20'
));
$wp_customize->add_section('homepage_settings_section', array(
'title' => 'Homepage settings',
'panel' => 'homepage_sections',
));
$wp_customize->add_setting('homepage_settings_setting', array(
'default' => 1
));
$wp_customize->add_control('homepage_settings_control', array(
'section' => 'homepage_settings_section',
'settings' => 'homepage_settings_setting',
'label' => 'Number of sections',
'description' => 'Number of sections in homepage',
'type' => 'number'
));
global $wpdb;
$sections=$wpdb->get_results('SELECT section_id, section_title FROM vt_homepage_sections;');
foreach($sections as $key){
$section_id=$key->section_id;
$cust_setting_id=$section_id.'_setting';
$cust_control_id=$section_id.'_control';
$wp_customize->add_setting($cust_setting_id,array(
));
$wp_customize->add_control($cust_control_id,array(
'settings' => $cust_setting_id,
'section' => 'homepage_settings_section',
'label' => 'test Control'
));
}
}
Issue
Everything works fine when i don't use variables which contain a value fetched using $wpdb. Is $wpdb object loaded after customizer framework?
When I use the code above, the above customized objects font appear in the customizer panel. Would appreciate hints to what's wrong with my code above.
Thanks in advance,
J

Please try this like this '$sections = $wpdb->get_results('SELECT section_id, section_title FROM vt_homepage_sections');'

define global class at the start of function and check.

Related

Adding a dropdown menu in Prestashop 1.7 module

I'm so beginner in Prestashop 1.7, I wanted to add a dropdown select section in my banner module to select the way to open the banner link.
but the selected value is never passed to the HTML, the code below IS passed but the one under isn't, can you please assist me?
[enter image description here][1]
array(
'type' => 'text',
'lang' => true,
'label' => $this->trans('Banner description', array(), 'Modules.Banner.Admin'),
'name' => 'BANNER_DESC',
'desc' => $this->trans('Please enter a short but meaningful description for the banner.', array(), 'Modules.Banner.Admin')
)
array(
'type' => 'select', //select
'lang' => true,
'label' => $this->trans('Banner tab', array(), 'Modules.Banner.Admin'),
'name' => 'BANNER_TAB',
'required'=>'true',
'options' => array(
'query' => array(
array('key' => '_blank', 'name' => 'New tab'),
array('key' => '_self', 'name' => 'Same tab'),
),
'id' => 'key',
'name' => 'name'
),
'desc' => $this->trans('Please select the way to open the link.', array(), 'Modules.Banner.Admin')
)
This is how it looks in the Backoffice:
Here
You not only need to add a new field to your form but also handle saving the data from it.
Take a look at a few examples:
https://github.com/PrestaShop/ps_featuredproducts/blob/dev/ps_featuredproducts.php#L122
Notice how the module author managed to save each configuration field from the form. This is what you need to do.
If you want to have access to data in your view, you have to pass it:
https://github.com/PrestaShop/ps_featuredproducts/blob/dev/ps_featuredproducts.php#L244
Maybe after you added a new field, you forgot to handle the saving + passing to the view?

Check value from Gravity Forms API

I am creating a custom Gravity Forms add-on and it appears to work so far. The settings are showing and saving as expected.
Here's what I have:
public function plugin_settings_fields() {
return array(
array(
'title' => esc_html__( 'Animal Types', 'animaltypes' ),
'fields' => array(
array(
'name' => 'gravity_forms_animal_types',
'type' => 'checkbox',
'label' => esc_html__( 'Animal Types', 'animaltypes' ),
'choices' => array(
array(
'label' => esc_html__( 'Cat', 'animaltypes' ),
'name' => 'option_cat',
'default_value' => 0,
),
array(
'label' => esc_html__( 'Dogs', 'animaltypes' ),
'name' => 'option_dog',
'default_value' => 0,
)
)
),
)
)
);
}
But what I can't figure out is how to check, for example, if option_cat has been set so that I can then run a custom function if it is.
So essentially (and I know the below code is not correct) something like this:
if(option_cat == true) {
my_cat_function();
}
In gravity forms when you create a new addon you provide a slug for that addon. Gravity forms save that settings with the help of that slug.
So if you want to get that settings you can use below code.
$settings = get_option('gravityformsaddon_slugname__settings');
$option = rgar( $settings, 'gravity_forms_animal_types' );
In options you can get selection of your settings, and if you want to one selection at a time you must use radio button instead of checkbox.
It was quite simple after all.
$options = get_option('gravityformsaddon_animaltypes_settings');
$cat = $options['option_cat'];
if($cat) {
my_cat_function();
}

Wordpress select setting in customizer not outputing proper code

I have a customizer section inside my wordpress theme. I made a setting and a control for an icon. I want my user to be able to choose what icon he wants. I implemented the icomoon icon font with classes like icon-home.
I made a setting and a control for that icon like this:
$wp_customize->add_setting(
'service1_icon',
array(
'default' => 'icon1',
'type' => 'option',
)
);
$wp_customize->add_control(
'service1_icon',
array(
'label' => 'Service 1 Icon',
'section' => 'section_services',
'type' => 'select',
'choices' => array(
'icon1' => 'mobile',
'icon2' => 'home',
),
)
);
and in html/php:
<span class="service-icon icon-<?php echo get_theme_mod('service1_icon', 'icon1'); ?>"></span>
But when in my browser I see the output like this:
<span class="service-icon icon-icon1"></span>
instead of:
<span class="service-icon icon-mobile"></span>
I was trying to fix it and did some stuff that were illogical to me and I fixed it.
First, I removed the type from the setting:
$wp_customize->add_setting(
'service1_icon',
array(
'default' => 'icon1',
)
);
Then I renamed icon1, icon2... to the actual name of the icon:
$wp_customize->add_control(
'service1_icon',
array(
'label' => 'Service 1 Icon',
'section' => 'section_services',
'type' => 'select',
'choices' => array(
'mobile' => 'mobile',
'home' => 'home',
),
)
);

wordpress Call to a member function add_section() on a non-object in functions.php line 5

Hey I'm kinda new to php and wordpress development and I'm just experimenting with how wordpress works and how the themes work. Now I have tried looking this up many times but no help. It was always something different and things that worked for those people, didnt work for me. here is the code,
<?php
function sandor_hero_cap() {
$wp_customize->add_section('sandor_subhead_title', array(
'title' => __('Sub-Header', 'sandor'),
));
$wp_customize->add_setting('sandor_subhead_title', array(
'default' => 'A Virtual PLAYGROUND',
'capability' => 'edit_theme_options',
'sanitize_callback' => 'sanitize_text_field'
));
$wp_customize->add_control('sandor_subhead_title', array(
'label' => __('Sub-Header Title', 'sandor'),
'section' => 'sandor_subhead_section',
'priority' => 5,
'settings' => 'sandor_subhead_title'
));
}
add_action( 'customize_register', 'sandor_hero_cap' );
?>
the error that im getting is:
Call to a member function add_section() on a non-object in functions.php line 5
I have tried a lot of things but nothing seems to be working, hopefully you guys can help me solve this issue. thanks :)
You're missing the $wp_customize argument. And, if I'm not mistaken, it might be an idea to use a different name for add_setting and add_control. See the code below.
function sandor_hero_cap($wp_customize) {
$wp_customize->add_section('sandor_subhead_title', array(
'title' => __('Sub-Header', 'sandor'),
));
$wp_customize->add_setting('sandor_playground', array(
'default' => 'A Virtual PLAYGROUND',
'capability' => 'edit_theme_options',
'sanitize_callback' => 'sanitize_text_field'
));
$wp_customize->add_control('sandor_playground', array(
'label' => __('Sub-Header Title', 'sandor'),
'section' => 'sandor_subhead_section',
'priority' => 5,
'settings' => 'sandor_subhead_title'
));
}
add_action( 'customize_register', 'sandor_hero_cap' );

dynamically add tab in YiiBooster TbTabs widget

I have created bootstrap.widgets.TbTabs as below, how can I add a new tab dynamically on client side. Is there any event using which I can add new tab just like the "shown" event in below code.
<?php $this->widget('bootstrap.widgets.TbTabs', array(
'id' => 'mytabs',
'type' => 'tabs',
'tabs' => array(
array('id' => 'tab1', 'label' => 'Tab 1', 'content' => $this->renderPartial('tab1', null, true), 'active' => true),
array('id' => 'tab2', 'label' => 'Tab 2', 'content' => 'loading ....'),
array('id' => 'tab3', 'label' => 'Tab 3', 'content' => 'loading ....'),
),
'events'=>array('shown'=>'js:loadContent')
));?>
There is none but that doesn't mean you can't create it yourself.
Just pass the 'tabs' part via your controller, something like this:
in your controller:
$tabs => array(
array('id' => 'tab1', 'label' => 'Tab 1', 'content' => $this->renderPartial('tab1', null, true), 'active' => true),
array('id' => 'tab2', 'label' => 'Tab 2', 'content' => 'loading ....'),
array('id' => 'tab3', 'label' => 'Tab 3', 'content' => 'loading ....'),
);
$this->render('pageName', array('tabs' => $tabs));
In your view:
<?php $this->widget('bootstrap.widgets.TbTabs', array(
'id' => 'mytabs',
'type' => 'tabs',
'tabs' => $tabs,
'events'=>array('shown'=>'js:loadContent')
));?>
If you want to achieve something similar but in your main layout, you can create a Model which extends from CActiveRecord and stores the tabs in the database. After that you could use a static method in your model class to call and retrieve the tabs. Then in your layout view you can do something like this:
<?php $this->widget('bootstrap.widgets.TbTabs', array(
'id' => 'mytabs',
'type' => 'tabs',
'tabs' => Model::getTabs(),
'events'=>array('shown'=>'js:loadContent')
));?>
Add the new tab link in header, add the new tab, display the new tab
$('#tabHeaders').append($('<li>New Tab</li>'));
$('#tabContent').append($('<div class="tab-pane" id="newtab"></div>'));
$('#newtab').tab('show');
Note YiiBooster automatically generates element id, you need to use appropriate jquery selectors
I don't think it's good practice to renderPartial the included views inside the controller.
I found this to work for me in the view:
$this->widget('bootstrap.widgets.TbTabs', array(
'type'=>'tabs',
'tabs'=>array(
array( //
'label'=>'Comments ('.$model->commentCount.')',
'content'=>$this->renderPartial( '_comments', array( 'model'=>$model ), true ),
),
)
));
Obviously, for this to work you need "_comments.php" to exist...

Categories