1

I have an faiulure page https://www.---.com/app/wp-comments-post.php

Is there a way to edit the grey background and change it to white? The style is automatically inserted into the page. I've been searching for the file to see where the styles load from so I can change it but I cannot find the file.

Does anyone know the name of the file where the style is located or its path?

Pointing to style attribute

2 Answers 2

0

The _default_wp_die_handler function is responsible for outputing the CSS you're seeing, and whilst the callback function is hookable(see wp_die_handler), meaning you could use your own callback in it's place, it seems a bit much to be copying and modifying the whole handler just to tweak some CSS.

There are barely any hooks in this function and it also doesn't run the usual head actions, like wp_head. There is however one function that runs in the head part of the page when that function runs, which is hookable.

This is somewhat hacky and places the output before the existing style tag in the page, but using CSS specificity or the !important property where an ID is isn't available for precedence, we can override later styling.

add_filter( 'wp_robots', 'wpse_question_411792' );
function wpse_question_411792( $r ) {
    global $pagenow;
    if( 'wp-comments-post.php' !== $pagenow )
        return $r;
    ?>
    <style type="text/css">
    html { background-color: #000!important; }
    body#error-page { background-color: #ccc; }
    </style>
    <?php
    return $r;
}

It's definitely a bit hacky, but it's cleaner than writing out a whole new die handler with modifications.

You could just as easily link a stylesheet in place of doing the styling inline, eg.

<link rel="stylesheet" href="mystyle.css">

And then do appropriate CSS overrides in the stylesheet.

3
  • When I place the code in my functions.php I get an error on line 38 where I placed the code. "Your PHP code changes were rolled back due to an error on line 38 of file wp-content/themes/twentytwenty/functions.php. Please fix and try saving again. syntax error, unexpected 'public' (T_PUBLIC), expecting end of file"
    – Rob
    Dec 3, 2022 at 14:53
  • Oh yes, i was testing inside a class, i forgot to remove that from the sample code, you only need to remove the word public from the function (i'll edit my answer).
    – t31os
    Dec 3, 2022 at 16:13
  • thank you this worked for me!
    – Rob
    Dec 3, 2022 at 17:13
0

It is not a good idea to update the core file, however, you can push another style and force it (if this is really needed).

add_action('wp_head', 'my_forced_styles');
function my_forced_styles() { 
  echo '<style>html {background:#fff!important;} </style>';
}

Place the above code in functions.php of your child theme or parent if you don't have child. You can also use this in plugin.

3
  • It did not work for me. Do you think that perhaps there is another head rather than 'wp_head' that it would be inserted into?
    – Rob
    Dec 3, 2022 at 3:15
  • wp_head is the standard hook to insert within <head> of HTML. It is not useful if the theme is missing this hook in header.php. Plus. View the page source, does this newly placed code loaded?
    – Aqsa J.
    Dec 3, 2022 at 3:22
  • It is not in the page source. I don't think this page is part of the theme. The page looks like a a standard wordpress page since it has its styles.
    – Rob
    Dec 3, 2022 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.