Archived topic
The option_generate_settings filter is not applied if there are no options yet
7 replies · Started by trebere on July 4, 2022
Hi, I'm using the option_generate_settings filter to customise a GP child theme, without activating the Premium plugin yet.
I have noticed that when there are no modified options in the Customizer or the options are reset, the filter does not apply. Is there any way to force it to apply?
Thank you very much.
Hi there,
Which option are you trying to modify within option_generate_settings?
Can you share the exact code you are using?
Let me know :)
Hi,
I want to use the filter with as much options as I can without using the Customizer. But for example, the following does not apply with the Customizer options reseted. It doesn't work unless I modify and Publish a single option in the Customizer.
function gpc_gp_options( $settings ) {
$settings['hide_title'] = true;
$settings['container_width'] = 1428;
return $settings;
}
add_filter( 'option_generate_settings','gpc_gp_options' );
Thank you.
Hi there,
In your case, you actually need to rewrite the GP default settings, check the defaults.php below:
https://github.com/tomusborne/generatepress/blob/adfe090929b0515cdf894f4c6b722cfe8c0790dc/inc/defaults.php
You can copy the code from line 12 to line 113 and make your modifications based on that.
Hi,
Thanks, I've a better picture of the defaults now. The following is the one I'm using, but as I said it doesn't override the defaults unless I save a modification in the Customizer.
function gpc_gp_options( $settings ) {
/* Site Identity */
$settings['hide_title'] = true;
$settings['hide_tagline'] = true;
$settings['logo_width'] = 150;
/* Layout - Container */
$settings['container_width'] = 1228;
$settings['content_layout_setting'] = 'separate-containers'; // one-container, separate-containers
$settings['container_alignment'] = 'text'; // text, boxes
/* Layout - Top Bar */
$settings['top_bar_width'] = 'full'; // full, contained (full-width ¿?)
$settings['top_bar_inner_width'] = 'contained'; // full, contained (full-width ¿?)
$settings['top_bar_alignment'] = 'right'; // left, center, right
/* Layout - Header */
$settings['header_layout_setting'] = 'fluid-header'; // fluid-header, contained-header
$settings['header_inner_width'] = 'contained'; // contained, full-width
$settings['header_alignment_setting'] = 'left'; // left, center, right
/* Layout - Primary Navigation */
$settings['nav_layout_setting'] = 'fluid-nav'; // fluid-nav. contained-nav
$settings['nav_inner_width'] = 'contained'; // contained, full-width
$settings['nav_alignment_setting'] = 'left'; // left, center, right
$settings['nav_position_setting'] = 'nav-float-right'; // nav-below-header, nav-above-header, nav-float-right, nav-float-left, nav-left-sidebar, nav-right-sidebar, '' (no navigation)
$settings['nav_drop_point'] = 768;
$settings['nav_dropdown_type'] = 'hover'; // hover, click, click-arrow
$settings['nav_dropdown_direction'] = 'right'; // right, left
$settings['nav_search'] = 'enable'; // enable, disable
/* Layout - Sidebars */
$settings['layout_setting'] = 'right-sidebar'; // left-sidebar, right-sidebar, no-sidebar, both-sidebars, both-left, both-right
$settings['blog_layout_setting'] = 'right-sidebar'; // left-sidebar, right-sidebar, no-sidebar, both-sidebars, both-left, both-right
$settings['single_layout_setting'] = 'right-sidebar'; // left-sidebar, right-sidebar, no-sidebar, both-sidebars, both-left, both-right
/* Layout - Footer */
$settings['footer_layout_setting'] = 'fluid-footer'; // fluid-footer, contained-footer
$settings['footer_inner_width'] = 'contained'; // contained, full-width
$settings['footer_widget_setting'] = 5;
$settings['back_to_top'] = 'enable'; // enable, '' (disable)
$settings['footer_bar_alignment'] = 'center'; // left, center, right (Not in the Customizer)
/* Layout - Blog */
$settings['post_content'] = 'excerpt'; // full, excerpt
/* General */
$settings['icons'] = 'svg'; //svg, font
$settings['underline_links'] = 'not-hover'; // always, hover, not-hover, never
$settings['dynamic_css_cache'] = false;
return $settings;
}
add_filter( 'option_generate_settings','gpc_gp_options' );
Thank you.
As I said, this filter option_generate_settings won't work in your case, you need to rewrite the entire theme function generate_get_defaults().
Try this code instead:
if ( ! function_exists( 'generate_get_defaults' ) ) {
/**
* Set default options
*
* @since 0.1
*/
function generate_get_defaults() {
return apply_filters(
'generate_option_defaults',
array(
'hide_title' => true,
'hide_tagline' => true,
'logo' => '',
'inline_logo_site_branding' => false,
'retina_logo' => '',
'logo_width' => '',
'top_bar_width' => 'full',
'top_bar_inner_width' => 'contained',
'top_bar_alignment' => 'right',
'container_width' => '1200',
'container_alignment' => 'text',
'header_layout_setting' => 'fluid-header',
'header_inner_width' => 'contained',
'nav_alignment_setting' => is_rtl() ? 'right' : 'left',
'header_alignment_setting' => is_rtl() ? 'right' : 'left',
'nav_layout_setting' => 'fluid-nav',
'nav_inner_width' => 'contained',
'nav_position_setting' => 'nav-float-right',
'nav_drop_point' => '',
'nav_dropdown_type' => 'hover',
'nav_dropdown_direction' => is_rtl() ? 'left' : 'right',
'nav_search' => 'disable',
'content_layout_setting' => 'separate-containers',
'layout_setting' => 'right-sidebar',
'blog_layout_setting' => 'right-sidebar',
'single_layout_setting' => 'right-sidebar',
'post_content' => 'excerpt',
'footer_layout_setting' => 'fluid-footer',
'footer_inner_width' => 'contained',
'footer_widget_setting' => '3',
'footer_bar_alignment' => 'right',
'back_to_top' => '',
'background_color' => 'var(--base-2)',
'text_color' => 'var(--contrast)',
'link_color' => 'var(--accent)',
'link_color_hover' => 'var(--contrast)',
'link_color_visited' => '',
'font_awesome_essentials' => true,
'icons' => 'svg',
'combine_css' => true,
'dynamic_css_cache' => true,
'structure' => 'flexbox',
'underline_links' => 'always',
'font_manager' => array(),
'typography' => array(),
'google_font_display' => 'auto',
'use_dynamic_typography' => true,
'global_colors' => array(
array(
'name' => __( 'Contrast', 'generatepress' ),
'slug' => 'contrast',
'color' => '#222222',
),
array(
/* translators: Contrast number */
'name' => sprintf( __( 'Contrast %s', 'generatepress' ), '2' ),
'slug' => 'contrast-2',
'color' => '#575760',
),
array(
/* translators: Contrast number */
'name' => sprintf( __( 'Contrast %s', 'generatepress' ), '3' ),
'slug' => 'contrast-3',
'color' => '#b2b2be',
),
array(
'name' => __( 'Base', 'generatepress' ),
'slug' => 'base',
'color' => '#f0f0f0',
),
array(
/* translators: Base number */
'name' => sprintf( __( 'Base %s', 'generatepress' ), '2' ),
'slug' => 'base-2',
'color' => '#f7f8f9',
),
array(
/* translators: Base number */
'name' => sprintf( __( 'Base %s', 'generatepress' ), '3' ),
'slug' => 'base-3',
'color' => '#ffffff',
),
array(
'name' => __( 'Accent', 'generatepress' ),
'slug' => 'accent',
'color' => '#1e73be',
),
),
)
);
}
}
Hi,
Thanks Ying, I could make the defaults work but still it doesn't work in combination with `option_generate_settings', let's say I need a conditional options for the front-page. It still needs to save an option modification in the Customizer.
UPDATE: I've found the following that seems to solve my problem. https://wordpress.org/support/topic/override-defaults-php/
Thank you very much!
Glad you found that solution! :)