0

When I put this code in my functions.php

add_action('wp_head', 'my_favicon');
function my_favicon() {
    echo "<link rel='shortcut icon' href=' " . get_stylesheet_directory_uri() .
         '/images/favicon.ico'"'>";
}

I get

[Message from webpage]: There has been a critical error on this website. Learn more about debugging in WordPress.

What's wrong with my code?

8
  • 1
    where did you add this code? what is the full text of the error message?
    – Michael
    Feb 15 '21 at 0:27
  • @Michael The error was when a reload the webpage. [Message from webpage]: There has been a critical error on this website. Learn more about debugging in WordPress. I put this line of code in my functions.php. code **add_action('wp_head', 'my_favicon'); function my_favicon() { // echo "<link rel='shortcut icon' href=' " . get_stylesheet_directory_uri() . '/images/favicon.ico'"'>"; } ** code Feb 15 '21 at 0:30
  • 1
    You've got a syntax error: that '/images/favicon.ico'"' should be "'/images/favicon.ico'. And the href should have no whitespace at the beginning, so use href='".
    – Sally CJ
    Feb 15 '21 at 0:39
  • 1
    Sorry @JerryFleurival, I meant, "/images. But why don't you just use the Customizer → Site Identity → Site Icon to set the site icon (or favicon)?
    – Sally CJ
    Feb 15 '21 at 1:42
  • 1
    @SallyCJ Awesome that fixed it. Thank you so much. Muneeb solved it too. I'm aware of using Customizer. I'm just a person who likes to know everything under the hood. 😊 Feb 15 '21 at 3:16
1
add_action('wp_head', 'my_favicon');
function my_favicon() {
    echo "<link rel='shortcut icon' href='" . get_stylesheet_directory_uri() .
         "/images/favicon.ico'>";
}

I think the problem was incorrect quotation marks on the string. Can you try this code.

2
  • Thanks a lot Muneeb! It was the quotation marks not being right. It worked. 😊 Feb 15 '21 at 3:20
  • you are welcome :)
    – MMK
    Feb 15 '21 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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