Dynamic or inline styles

Hi,

Moesia has styles.php which creates a big $custom string that contains some CSS rules that are changed dynamically according the page you load.

My question is: how can I change this string in my child theme’s functions.php?
Can I use str_replace somehow or should I write a completely new function?
Please explain it to me, because I’m lost here.

My website is: www.countryball.com

Thanks for the answer!

Hello there,

It can be accomplished by doing the following:

  1. In the child theme’s functions.php file, add the following function:
function moesia_child_custom_styles($custom) {
   /* Parent theme's custom CSS goes here */
}
add_action( 'wp_enqueue_scripts', 'moesia_child_custom_styles', 9999 );
  1. Copy the custom style content of the parent theme’s moesia_custom_styles function in the style.php file.

  2. Remove /* Parent theme’s custom CSS goes here */ then paste here. It will look like this.

Regards,
Kharis

Thank you very much! That worked.

Additional info for other people:

  • Important is to set a high priority for your function, otherwise it is overwritten by the parent’s styles.php
  • You don’t need to actually copy/paste the whole contents of the parent’s styles.php, just add the code that you want to modify for your child

Maybe for some this is obvious, but I thought it should help.

For me, my child theme’s functions.php looks like this:


function moesia_child_custom_styles($custom) {
    $custom = "\n";
    
    $custom .= ".panel.widget .widget-title:after, .so-panel.widget .widget-title:after { border-color: #9c0e2b; }"."\n";	
            
    $custom .= "@media only screen and (min-width: 991px) { .welcome-info { top: 50%; } }"."\n";
    $custom .= "@media only screen and (min-width: 1199px) { .welcome-info { top: 50%; } }"."\n";
    
    //Output all the styles
    wp_add_inline_style( 'moesia-style', $custom );	
}
add_action( 'wp_enqueue_scripts', 'moesia_child_custom_styles', 9999 );

Hello there,

Thank you for sharing. We really appreciated it. It would be extremely useful for those who might have similar objective to accomplish.

Regards,
Kharis