I'm developing a WordPress theme and I'm trying to add an option in WordPress customizer to make the user able to choose between many styles files included in the theme, what I'm trying to do is quite simple, a select option that allow to the user to choose style 1-style 2- etc... the problem is that I didn't find any tutorial on how to do that, all what I have find is how to customize the colors and that's not what I'm looking for. can anyone help or at least gives me ideas on how to do that.
i have find this solution myself and it works preety fine
function stlye_selector( $wp_customize ) {
$wp_customize->add_section(
'stlye_files_selector',
array(
'title' => 'stlye files selector',
'description' => 'you can set here your style file',
'priority' => 35,
)
);
$wp_customize->add_setting(
'colors',
array(
'default' => 'blue',
)
);
$wp_customize->add_control(
'colors',
array(
'type' => 'select',
'label' => 'your style',
'section' => 'stlye_files_selector',
'choices' => array(
'blue' => 'blue',
'red' => 'red',
),
)
);
}
add_action( 'customize_register', 'stlye_selector' );
and to use it just add this to ur template file
<?php $stat= get_theme_mod( 'colors','blue'); ?>
if no color is slected it will be on blue style
Related
I have two themes installed on a wordpress website. I have moved all my non-theme specific functions into a plugin as recommended and all is working well except customizer.php which was included in my functions.php file. I moved it away from functions.php into the plugin. The customizer contents showed when first theme was activated but won't show when I switched to the other theme. Below are code snippets for reference:
I included customizer.php into my plugin using the code below
require( plugin_dir_path( __FILE__ ) . 'inc/customizer.php' );
Below is the panel that controls the logo
if ( class_exists( 'WP_Customize_Panel' ) ):
$wp_customize->add_panel( 'panel_general', array(
'priority' => 29,
'capability' => 'edit_theme_options',
'title' => __( 'General options', 'gswmi' )
));
/* LOGO */
$wp_customize->add_setting( 'site_logo', array(
'sanitize_callback' => 'esc_url_raw',
'transport' => 'postMessage'
));
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'themeslug_logo',
array(
'label' => __( 'Logo' ),
'section' => 'title_tagline',
'settings' => 'site_logo',
'priority' => 1,
)
)); endif;
The logo and contents show when the first theme is activated
But won't show when the second theme is activated
I have tried different fixes, even deleting the text domain in the customizer.php code, none seemed to work. I want to be able to switch themes and still be able to show the same contents in the customizer and the frontend irrespective of the theme activated. I will appreciate any help.
I was finally able to sort it, thanks to some articles I found online (credits below).
By default, my setting's type above is 'theme_mod'
Theme mods are stored in a single array in the options table in the database, one field per theme. In my case, i found all the theme customizations in a single array under theme_mods_(mythemename) in wp_options table in my database. As you can guess, this is not good for performance.
To make my logo and other settings readily availabe irrspective of the theme I use, I just had to change the setting type to 'option' from 'theme_mod' which is the default setting type.
But what’s the difference? To try to keep this simple:
Theme Modifications(Theme Mod) represent what’s available for a specific theme. WordPress saves all options managed by The Customizer as a single array in the database.
Options are available to any theme and/or any plugin. In contrast to the The Customizer, WordPress saves each value as a single record in the database.
Update code below
if ( class_exists( 'WP_Customize_Panel' ) ):
$wp_customize->add_panel( 'panel_general', array(
'priority' => 29,
'capability' => 'edit_theme_options',
'title' => __( 'General options', 'gswmi' )
));
/* LOGO */
$wp_customize->add_setting( 'site_logo', array(
'type' => 'option',
'sanitize_callback' => 'esc_url_raw',
'transport' => 'postMessage'
));
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'themeslug_logo',
array(
'label' => __( 'Logo' ),
'section' => 'title_tagline',
'settings' => 'site_logo',
'priority' => 1,
)
)); endif;
The above Setting’s value will get its own column inside the WP_OPTIONS table and there will be a performance boost when the value is accessed for outputting in the frontend.
Credits:
https://www.usablewp.com/learn-wordpress/wordpress-customizer/theme-mods-vs-options-in-wordpress/
https://tommcfarlin.com/wordpress-options/
I use the Canvas theme on my german website with a woocommerce shop.
However I have the problem, that my product pagination still uses "previous" and "next" as button texts, even through everything is translated to german.
The Canvas Theme uses an admin-functions.php with following code:
$defaults = array(
'base' => esc_url_raw( add_query_arg( 'paged', '%#%' ) ),
'format' => '',
'total' => $max_num_pages,
'current' => $current,
'prev_next' => true,
'prev_text' => __( '← Previous', 'woothemes' ), // Translate in WordPress. This is the default.
'next_text' => __( 'Next →', 'woothemes' ), // Translate in WordPress. This is the default.
'show_all' => false,
'end_size' => 1,
'mid_size' => 1,
'add_fragment' => '',
'type' => 'plain',
'before' => '<div class="pagination woo-pagination">', // Begin woo_pagination() arguments.
'after' => '</div>',
'echo' => true,
'use_search_permastruct' => true
);
/* Allow themes/plugins to filter the default arguments. */
$defaults = apply_filters( 'woo_pagination_args_defaults', $defaults );
I can neither override this (The comment says not to override anyway) nor find an option in my page's backend to translate this.
Where or how can I change the Text on those buttons?
Thank you.
In case anyone ever stumbles across this problem:
I solved it now using the plugin "Say, what!" and translated the String with it. However, this only works, if the String is inside the __() or _e() function.
Im trying to create a sub section within the customiser options in WordPress, so i have a new section called Typography and i want to make sub sections inside that which will be Heading 1, Heading 2 etc... those should open out into their own set of controls. However i have been looking through the codex and cant seem to find a way to link those menus up, any ideas?
Below is the normal code for adding a section but adding a new one doesnt seem to have a option for attaching it to the parent? Ive tried a few variations of the code below without any luck, all i get is 2 typography menus on the main menu.
EDIT: Still having no luck, i read in the codex about a option for panels but cant seem to implement it.
i am using the code below, its just a simplified version of the code to make sure i have it right. I assume a section goes into the panel and the settings and control go into the section. Using this codex
// ADD PANEL
$wp_customize->add_panel( 'typography_panel', array(
'priority' => 10,
'capability' => 'edit_theme_options',
'theme_supports' => '',
'title' => 'Typography',
'description' => 'contains options for all font settings on the website.',
) );
// ADD SECTION TO GO INTO PANEL
$wp_customize->add_section( 'heading_1', array(
'priority' => 20,
'capability' => 'edit_theme_options',
'theme_supports' => '',
'title' => 'H1 Options',
'description' => 'all options for heading 1 font.',
'panel' => 'typography_panel',
) );
// ADD SETTINGS
// Selector Background - Highlighting
$wp_customize->add_setting(
'typography_highlight_background_h1',
array(
'default' => '#333948',
'sanitize_callback' => '',
) );
// ADD CONTROLS
// Highlight Background for Browser
$wp_customize->add_control(
new WP_Customize_Color_Control(
$wp_customize, 'typography_highlight_background_h1',
array(
'label' => __( 'Highlight Background', 'typography_highlight_background_h1' ),
'section' => 'heading_1',
'settings' => 'typography_highlight_background_h1',
) ) );
You're looking for add_setting and add_control. Sub-sections can be added by passing the name of the section as an argument to add_control.
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://...
I have a problem, I need to change the default options for a “List-item” option in Option tree . Right now the default are “Title/Image/Link/Descriptions” .. I want to remove them and add my own. I have written this code:
array(
'id' => 'academic_success_content',
'label' => 'Academic Success Content',
'desc' => 'Enter the academic success content. It will appear in the home page in list items format',
'std' => '',
'type' => 'list-item',
'section' => 'academic_perfomance',
'settings' => array(
array(
'id' => 'academic_success',
'label' => 'Academic Success',
'type' => 'textarea-simple',
)
)
),
But when I preview the themes options , the default list item "title" is still there and I only want to see the Academic Success textarea. What should I do?
I also suffered from the same situation.
And after that, I use the older version of "list-item" and "gallery".
This has worked well. Ex: v2.4.6
Is the theme options page being included?
You'll need to reference it in your functions.php, something to the effect of:
require_once locate_template('/path-to-your/theme-options.php' );