retrieve customize_register setting from wordpress - wordpress

I'm trying to retrieve some customizer settings that I initiated.
function notop_customize_register( $wp_customize ) {
$wp_customize->add_section( 'header_images', array(
'title' => 'Header Images',
'priority' => 2,
'description' => "Add images with links to be redirected to when the images are clicked",
) );
// Add color scheme setting and control.
$wp_customize->add_setting( 'header_image_1_setting', array(
'capability' => 'edit_theme_options',
'type' => 'option',
) );
$wp_customize->add_control(new WP_Customize_Upload_Control($wp_customize,'header_image_1_control', array(
'label' => __( 'Import Image', 'notop' ),
'section' => 'header_images',
'settings' => 'header_image_1_setting',
) ) );
}
add_action( 'customize_register', 'notop_customize_register' );
How do I get the url of header_image_1?
I thought it might be get_theme_mod('header_image_1_setting'); but that hasn't worked so far. Also, I'm not sure if I'm actually supposed to call the customize_register file in my index somewhere or if it's okay the way it is.

cale_b's comment is the answer.
get_option('header_image_1_setting')
=> http://...

Related

Getting values from Kirki customizer fields in WordPress Theme

I'm using the Kirki plugin to add fields and sections in the WordPress customizer. So far, I can get a field to be added to the customizer, but I'm confused on how to return that data back into my theme. I'm kinda tired so I might be missing something. This is what I have so far:
Kirki::add_config('theme_config_id', array(
'capability' => 'edit_theme_options',
'option_type' => 'theme_mod',
));
Kirki::add_section('footer_section', array(
'title' => __('Footer'),
'description' => __('Add custom footer here'),
'panel' => '', // Not typically needed.
'priority' => 160,
'capability' => 'edit_theme_options',
'theme_supports' => '', // Rarely needed.
));
Kirki::add_field('theme_config_id', [
'type' => 'editor',
'settings' => 'my_setting',
'label' => esc_html__('Footer Content', 'kirki'),
'description' => esc_html__('This content will show in the footer.', 'kirki'),
'section' => 'footer_section',
'default' => '',
]);
I was reading on trying to the values out from here using this:
$value = Kirki::get_option( $config_id, $option_id );
But I'm not sure where (or what) the $config_id or $option_id would be?
I have a feeling that I'm missing something and I've read through the docs and I feel that I'm not getting it.
This is the correct code
$value = Kirki::get_option( 'config_id', 'option_id' );
so in your case,
$value = Kirki::get_option( 'theme_custom', 'footer_content' );
After digging through the internet a little bit, I was able to read through some more documentation as well as some other examples and I was able to figure out what I was doing wrong. Overall, I was close, but I ended up cleaning it up and just using the WordPress get_theme_mod() outright in my template file (in this case it was the footer.php file).
Here's what I ended up with:
Kirki::add_config('theme_custom', array(
'capability' => 'edit_theme_options',
'option_type' => 'theme_mod'
));
Kirki::add_section('footer_section', array(
'title' => __('Footer'),
'description' => __('Add custom footer here'),
'panel' => '', // Not typically needed.
'priority' => 160,
'capability' => 'edit_theme_options',
'theme_supports' => '', // Rarely needed.
));
Kirki::add_field('theme_custom', array(
'type' => 'editor',
'settings' => 'footer_content',
'label' => esc_html__('Footer Content', 'kirki'),
'description' => esc_html__('This content will show in the footer.', 'kirki'),
'section' => 'footer_section',
'default' => '',
'priority' => 10
));
and in my footer.php file, I added this:
<?php $value = get_theme_mod('footer_content', ''); ?>
<?php echo($value); ?>
Granted, this is a super basic way of implementing this. I'm going to try and figure out how to get it to refresh the customizer preview before you publish it. But for now, this seems to be working.

How will i add an option under customizer woocommerce section?

What is the customizer section name? I want to add a field option under customizer woocommerce section. Or how will I add a field under woocommerce option? Can someone help me with that?
$wp_customize->add_section( 'woocommerce_secction_name' , array(
'title' => __( 'My Section Name', 'starter' ),
'priority' => 30
) );
$wp_customize->add_setting( 'starter_new_setting_name' , array(
'default' => '#000000',
'transport' => 'refresh',
) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'link_color', array(
'label' => __( 'Header Color', 'starter' ),
'section' => 'woocommerce_secction_name',
'settings' => 'starter_new_setting_name',
) ) );
// Add section to WooCommerce Panel
$wp_customize->add_section( 'SECTION_NAME',
array(
'title' => __( 'SECTION TITLE'),
'panel' => 'woocommerce',
'capability' => '',
'priority' => 500,
)
);
The important part is the panel name as you can see selecting woocommerce it will add to the woocomerce panel

WordPress customizer multiple add_control 's in section

Quick question.
I'm trying to add multiple controls within a WordPress customizer section.
$wp_customize->add_section( 'lr_panel2', array(
'title' => esc_html__( 'Panel 2', 'lr' ),
'active_callback' => 'is_front_page',
'panel' => 'lr_theme_options',
'description' => esc_html__( 'Add a background image to your panel by setting a featured image in the page editor. If you don’t select a page, this panel will not be displayed.', 'lr' ),
) );
$wp_customize->add_setting( 'lr_panel2', array(
'default' => false,
'sanitize_callback' => 'lr_sanitize_numeric_value',
) );
$wp_customize->add_control( 'lr_panel2', array(
'label' => esc_html__( 'Panel Content', 'lr' ),
'section' => 'lr_panel2',
'type' => 'dropdown-pages',
) );
So this one is working fine and dandy. I try to add a second one and neither render. I assumed I could just repeat the add_control class, something like:
$wp_customize->add_control( 'lodestar_panel2', array(
'label' => esc_html__( 'Panel Layout', 'lr' ),
'section' => 'lr',
'type' => 'select',
'choices' => array(
),
) );
But that's not working how I want it too, Has anyone done this before?
Thanks!
You are adding the control in another section. Section should be same
'section' => 'lr_panel2',
This is the section of first control you've added and
'section' => 'lr',
This is the section of second control you've added
Also a control wouldn't show unless you've added somethings in it.

Implementing A WordPress Settings Class for Options Pages

I'm trying to implement this class: A WordPress Settings Class for Options Pages
What I tried:
function add_wp_page(){
require_once( TEMPLATEPATH."/folder_admin/wm-settings.php" );
// A top level page
$my_top_page = create_settings_page(
'my_top_level_page',
__( 'My Top Level Page' ),
array(
'parent' => false,
'title' => __( 'Top Level Menu' ),
'icon_url' => 'dashicons-admin-generic',
'position' => '63.3'
),
array(
'my_standard_section' => array(
'title' => __( 'Standard' ),
'description' => __( 'My section description.' ),
'fields' => array(
'my_input' => array(
'label' => __( 'Input example' )
),
'my_checkbox' => array(
'type' => 'checkbox',
'label' => __( 'Checkbox example' )
),
'my_textarea' => array(
'type' => 'textarea',
'label' => __( 'Textarea example' )
)
)
)
),
array(
'tabs' => true,
'submit' => __( 'My Submit' ),
'reset' => __( 'My reset' ),
'description' => __( 'My page description.' ),
'updated' => __( 'My success message !')
)
);
// And a sub-page
$my_sub_page = create_settings_page(
'my_sub_page',
__( 'My Sub Page' ),
array(
'parent' => 'my_top_level_page',
'title' => __( 'Sub Level Menu' )
)
);
}
add_action( 'admin_init', 'add_wp_page' );
The TEMPLATEPATH is it ok. If I echo something in wp-settings.php it will print it.
The rest of the code it is from that website. It is from an example.
The menu doesn't show.
Am I missing something?
Thanks
The settings need to be registered before 'admin_init'.
Replace the last line with add_action( 'init', 'add_wp_page' );.
Also, avoid wrapping the require_once in an action or you won't be able to access get_setting() outside your hook.
Oh, and the position can be a string. And it is actually recommended to use a decimal number to prevent possible conflicts for these menu positions with other plugins or themes.
Please set a 'position' => '63.3' to integer.
or use positions of core menu:
2 Dashboard
4 Separator
5 Posts
10 Media
15 Links
20 Pages
25 Comments
59 Separator
60 Appearance
65 Plugins
70 Users
75 Tools
80 Settings
99 Separator
The position in the menu order this menu should appear.
Hope it solves your problems :)

Fatal error when adding setting to customizer in wordpress

I am trying to add an extra setting in the Wordpress customizer. Can you add extra settings in the site title & tagline section or do you have to create your own section? This is the code I have so far in my functions.php file:
function mytheme_customize_register( $wp_customize )
{
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'your_setting_id',
array(
'label' => __( 'Your Label Name', 'theme_name' ),
'section' => 'title_tagline',
'settings' => 'your_setting_id',
'type' => 'text'
)
)
);
}
add_action( 'customize_register', 'mytheme_customize_register' );
If I try to go to the customize theme page in the admin area though I get the following error:
Fatal error: Call to a member function check_capabilities() on a non-object in C:\xampp\htdocs\one-gas\wp-includes\class-wp-customize-control.php on line 161
Yes, you can add to the title and tagline section. Try using this code:
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'your_setting_id',
array(
'label' => __( 'Your Label Name', 'theme_name' ),
'section' => 'title_tagline',
'settings' => 'your_setting_id',
'type' => 'text'
)
)
)
);
Read more about the Theme Customization API at the WordPress Codex.
I fixed it in the end it's because I had to define a setting first so this is the working code:
function mytheme_customize_register( $wp_customize )
{
$wp_customize->add_setting('subtagline', array(
'default' => 0,
'type' => 'option'
));
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'subtagline',
array(
'label' => __( 'Sub Tagline', 'One Gas' ),
'section' => 'title_tagline',
'settings' => 'subtagline',
'type' => 'text'
)
)
);
}
add_action( 'customize_register', 'mytheme_customize_register' );

Resources