Mohammad Umar Sattar
Mohammad Umar Sattar

Reputation: 39

Where is the HTML code for <?php wp_head(); ?> in wordpress?

I am new to WordPress and I am working on a website on which I have to make some changes to the Head tag contained elements. But I am unable to find the HTML because in Head part function is called and I have researched and get to know that the definition of the function is present in general-template.php file. I have gone through the file and got the following code

function wp_head() {
/**
     * Prints scripts or data in the head tag on the front end.
     *
     * @since 1.5.0
     */
    do_action( 'wp_head' );
}

I didn't get where is the HTML of the HEAD part is. Please guide, thank you in advance.

Upvotes: 1

Views: 3953

Answers (1)

DanieleAlessandra
DanieleAlessandra

Reputation: 1555

The function wp_head is usually called from the theme, this is how WordPress keeps logic and presentation apart.

Every WordPress theme must call the function wp_head inside the <head> portion of html, so look there.

Some of the places in which you will find that:

  • wp-content/themes/twentynineteen/header.php:11
  • wp-content/themes/twentytwenty/header.php:23

That said, if you are asking this question maybe you are trying to add content to the <head> part of your site. To do that you don't need to edit the theme files (because if the user changes theme your edit would be lost).

The interesting part is that do_action thing you see in the code you posted above, this is part of the WordPress hook logic.

Read this: https://developer.wordpress.org/plugins/hooks/

Practically you can register an action or a filter anywhere in your code (usually inside a plugin) and tell WordPress to execute a portion of code in another position.

Example: I need to add a <script> tag inside the <head> of my site, I will write this in my plugin:

function head_hello_world() {
    ?>
        <script>
            alert('Hello World!');
        </script>
    <?php
}
add_action('wp_head', 'head_hello_world');

The function output will be printed inside the <head> section of my page, just because it is registered in wp_head hook, and that hook is used by do_action inside the function you already found, and every theme would call wp_head in the right position.


This answer offer only an overview of the topic, I strongly suggest you to study the WordPress documentation before you continue.

Start here: https://developer.wordpress.org/

Upvotes: 1

Related Questions