2

Not sure if this is only problem for Elementor full width template, but it seems to override theme header.php. I tried achieving my goal by using elementor custom code feature, but it adds my code somewhere in middle of the tag.

What is the propper way of adding my own custom code as the first thing that is after the element?

Zedd
  • 167
  • 3
  • 16
  • Please show us what you tried so far! – Bhautik Apr 13 '21 at 13:11
  • I tried achieving my goal by using elementor custom code feature, but it adds my code somewhere in middle of the tag, not right after the . I also tried to put my code in header.php (which would be standard WP solution), but elementor seems to override it with its own files. Also tried digging through elementor files, but with no success. – Zedd Apr 13 '21 at 13:15

3 Answers3

7

You are right Elementor overrides the theme's header.php file so importing your code to this file is not effective. You need to add custom function to earn your goal. With the wp-head action you could add the code right into your header and Elementor will not override it.

Add this code to the functions.php file od your active theme.

add_action('wp_head', 'custom_head_function');
function custom_head_function(){
?>
YOUR HEADER CODE HERE
<?php
};

UPDATE - If you want to set your code at the top

As sephsekla mentioned in comment, there is a way to set the priority into your action to get it to the top. Try to set value to -999. So, choose a very low number and if there is no other lower number in your plugin or theme you will go straight to the top.

add_action('wp_head', 'custom_head_function', -999);
function custom_head_function(){
?>
YOUR HEADER CODE HERE
<?php
};
Lubo Masura
  • 1,034
  • 6
  • 20
  • Unfortunately, this doesnt work. I tried to put your code at the beginning of functions.php, but it only results in putting my header code around the middle of section and there is ton of other code from elementor and even other plugins above it. – Zedd Apr 13 '21 at 15:29
  • What is the purpose of putting it right behind the header? – Lubo Masura Apr 13 '21 at 15:34
  • It is requirement for 3rd party script that my client is required to deploy. – Zedd Apr 13 '21 at 16:00
  • 1
    Why it has to be right behind the ``, when it is in the head tag it will be recognised does not matter where it is. It has to be between ` HERE SOMEWHERE ` – Lubo Masura Apr 13 '21 at 16:14
  • It is custom cookie solution made for my client and this is a requirement in its PDF specifications. I dont know how it works neither am I pro at javascript so I want to implement it properly by specs rather than introduce some kind of unwanted behavior that might not be visible at first glance. Btw doesnt facebook and google ad scripts have a similar requirement (being either at the top or bottom of the )? There must be some reasoning for that. – Zedd Apr 13 '21 at 16:35
  • I tried going through Elementor files to find where it assembles the page, but the php seems to be pretty advanced and complicated. – Zedd Apr 13 '21 at 16:37
  • When I use fb and google I put it in the head and everything is working perfectly. – Lubo Masura Apr 13 '21 at 16:42
  • In my opinion you could use my code to achieve your goal, it should be all right. – Lubo Masura Apr 13 '21 at 16:49
  • Maybe it is not measuring things properly in some cases due to some behavior you are not aware of. I dont think FB and google would place those requirements in place just for nothing. – Zedd Apr 13 '21 at 16:49
  • I thank you for your help but I really want to do this properly. There should be a way to insert the code at top. – Zedd Apr 13 '21 at 16:50
  • FIY, check this: https://stackoverflow.com/questions/8996852/load-and-execute-order-of-scripts Havent read it whole, but it says that the scripts are executed in same order as they are on page. The 3rd party script I need to be on the top is cookie solution, so it blocks other scripts from creating cookies based on user preferences, so that is, I guess (with my limited js knowledge) why it needs to be first. To guarantee that no other script will create a cookie before it is started. – Zedd Apr 13 '21 at 17:02
  • 1
    You can set the priority of the action using the third argument in the function, I'd advise setting it as early as possible.`https://developer.wordpress.org/reference/functions/add_action/`. E.g. `add_action('wp_head', 'custom_head_function',1);`. Maybe try that. – Sephsekla Apr 22 '21 at 10:34
  • Yes that is right, but in this case its better to use lower number to be sure it will come straight to the top after the main meta fields. Try to set priority to -999, I have updated the answer. @Zedd – Lubo Masura Apr 22 '21 at 11:46
0

Elementor now supports custom code (javascript, html, etc) and supports the specific use of elements in the head of the page.

videohead
  • 1
  • 1
0

What you are looking for you can find at the Wordpress Dashboard> Elementor > Custom Code . Then you will be able to add a custom code to the head: https://elementor.com/help/custom-code-pro/

Marlon
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 02 '23 at 08:44