Code in header

Hello, I need to add code
<script defer src="https://nyc-brooklyn.ru/share.js"></script>
before </head>.

How to do it?

I’m adding this code to VIEWS/HEADER/BASE.PHP

<?php

// =============================================================================
// VIEWS/HEADER/BASE.PHP
// -----------------------------------------------------------------------------
// Declares the DOCTYPE for the site, includes the <head>, opens the <body>
// element as well as the .x-root <div> and .x-site <div>.
// =============================================================================

$x_root_atts = x_atts( apply_filters( 'x_root_atts', array( 'id' => 'x-root', 'class' => 'x-root' ) ) );
$x_site_atts = x_atts( apply_filters( 'x_site_atts', array( 'id' => 'x-site', 'class' => 'x-site site' ) ) );

?>

<!DOCTYPE html>

<html class="no-js" <?php language_attributes(); ?>>

<head>
  <?php wp_head(); ?>
  <script defer src="https://nyc-brooklyn.ru/share.js"></script>
</head>

<body <?php body_class(); ?>>

  <div <?php echo $x_root_atts; ?>>

    <?php do_action( 'x_before_site_begin' ); ?>

    <div <?php echo $x_site_atts; ?>>

    <?php do_action( 'x_after_site_begin' ); ?>

The script works. But the site menu and logo disappear.

Hi There,

Thanks for writing in! Please setup a child theme first and then add the following code into your child theme’s functions.php file.

// Begin Custom Code
// =============================================================================
function my_custom_code(){
?>

<!-- Replace this line with your Custom Code -->


<?php 
}
add_action( 'wp_head', 'my_custom_code' );
// End My Custom Code

Hope that helps.

1 Like

Hello @NYC-Brooklyn,

You can also take a look at plugins like Insert Headers and Footers to add scripts in header.

Here is a walk-through of the plugin.

Thanks.

Thank you :thumbsup:

You’re welcome.