The Internet has firmly entered our lives, and we can no longer imagine our existence without it. When we wake up, the first thing we do is open our beloved social network to read the latest news. And throughout the day we post new photos and stories from our lives on our blogs. Every day thousands of websites are born, and the same number die. But few people think about what is on the other side of the monitor. Few people know what the site consists of and by what principles it works. Today we want to tell you how to insert PHP code into HTML and why it is needed.

What is HTML

In order to get directly to the topic, you need to find out what the site consists of and how it is structured. If you open any page in your browser, you will see that it contains text, pictures, and sometimes videos. And there is no code there. But he's there! What we see in front of us is the display of the code. Just press the key combination CTRL+U, and in the next window we will find it. This is HTML.

But HTML is not a programming language, it is just hypertext markup. He cannot perform any logical operations how programming languages ​​do it. In other words, HTML is responsible for the arrangement of content on the page: text, pictures, media, video, tables. Once upon a time, back at the dawn of the Internet, all sites consisted of solid HTML, but this turned out to be not entirely convenient. Such sites are called static, and to change the content on the page it was necessary to edit all the code. This is how server-side programming languages ​​were invented, and one of such languages ​​is PHP. After this, the need arose to insert PHP code into HTML. We will describe how to do this below.

What is PHP

The PHP language code is not displayed in the browser. This is a server-side programming language. How to understand this? The server is remote computer. When a user performs some action on the page: fills out a registration form, presses buttons, writes a message - all the results of his actions are sent to the server. There they are processed and output back to the screen. This is how the user sees the result of his actions.

When server-side programming languages ​​began to be used, it became necessary to insert PHP code into an HTML page. How to do this correctly will be described below. The PHP language can interact not only with HTML, it also acts as an intermediary when working with databases. There are special functions that make queries to databases: select data from them, change, delete, update, and so on.

Why you can’t create a website using HTML alone

As mentioned above, sites based on HTML alone are static sites. You cannot change the content on them without editing the code itself. The PHP language provides great opportunities for managing websites. In particular, manage content from the administrative panel directly in the browser, without going to the server to change the code. It turned out to be very convenient solution: The site administrator can edit or add content independently, without seeking help from a programmer.

Thanks to PHP language Content management systems appeared. The most famous of them are Wordpress, Joomla, DLE. There are a lot of such CMCs, and more and more of them appear every day. But every self-respecting webmaster should be able to edit the code if the need arises. He should know how to insert PHP code into HTML without having to contact a programmer every time for such a small thing.

What is PHP Insertion Required for?

When designing templates in HTML, dynamic insertion is almost always used. For what? Modern websites are multi-page portals, and the number of pages they contain can vary depending on the task. That's why all modern websites use templates. The same template is displayed on all pages. This simplifies the work of programmers and makes it easy to change the display of the site in the browser.

To ensure this, in HTML template A special PHP code is implemented, which ensures dynamic replacement of the entire interface. Without such inclusion it is impossible to create a modern website. We will look further at how to insert html code into a php file.

Insert options

Depending on how the site template is written, the insertion options may be different. If the template is written in HTML, then PHP code is embedded in it, and if it is written in PHP, on the contrary, HTML is included in PHP. Both methods will be discussed here, and at the same time you can learn how to insert html code in php into a script.

In the first option everything is quite simple. The required PHP function is inserted into the HTML break. In the right place, open the tag with which PHP begins, after it the necessary function is written, then the tag is closed. This is the most common insertion option, although it is not the only one.

Another way is to include the file in the desired location. All necessary content is placed in separate file and connect it to the template using the include directive. Now if you change the file you are looking for, the contents of the template will change. There is another unconventional method, but you can learn more about how to insert html code into php in eof in the next article.

In the second option, consider a template written in PHP. In this example, the insertion will be a little more complicated. In such cases, the ECHO function is used, which contains HTML code. This design is also common, although it is not the only one. The HTML construct can be inserted directly into a PHP break: then the function must be closed and, having inserted the HTML, write the PHP code again, starting with the opening parenthesis.

Conclusion

From this article we learned how to insert php code into html. It's not difficult at all. There are many options, and everyone chooses what they think is simpler and more acceptable in their situation. It is important to know that programming is a complex science, and you cannot become a real programmer by reading just one article. There is a long way to go. Mistakes are inevitable at all stages, and this must be tolerated. Only acquired experience can give the result you are striving for.

Moreover, I would like to immediately clarify that we will insert the code into the article itself, so that our readers can copy it without wasting time typing the text itself. Thus, we will increase the convenience () of our resource - this time. And the articles will look more professional and complete - that's two.

I would also like to note that the information in this article will be useful to people who either, as we tell you, how to make various kinds of additions to the site, or users who simply like to share useful information. And it doesn’t matter that this information is code :) By the way, I’ll tell you a little secret; in this project we display it using the Wp-Syntex plugin.

You may ask: “Why do we use it?” The answer is simple - there are no comrades according to taste and color. Joke. In fact, we chose this plugin because it is easy to use, it perfectly adds visual style to various types of programming languages ​​(css, html, java, javascript, perl, sql, etc.), and does not overload our server.

Well, since I let it slip about Wp-Syntex, let’s use its example to show how to insert code into articles on WordPress.

First you need to install this plugin. We talked about how to do this in the article about . Therefore, I will not describe this action here.

As a result, you will see the following:

The code you want to be output

Also, instead of php in this code, you can insert another programming language, for example css or java. At the same time, the design style appearance will change.

Additional features of Wp-Syntex

If you add the attribute to the opening "pre" tag line, that is, the code you will need to paste will start like this:

The code you want to be output

Hope you notice the difference?

There is one more small attribute that you may need - this escaped. It allows you to convert html codes of characters directly into the characters themselves. For example, it converts ">" to ">". In order to make money this function, in the opening “pre” tag, insert the following attribute:

escaped="true"

Accordingly, the tag will begin like this:

< pre lang= "php" line= "1" escaped= "true" >

I also found information on the Internet that you can install another plugin (WP-Syntax Button), which works in conjunction with our plugin and adds a code insertion button to the WP editor. I wasn't too lazy and decided to test it.

Immediately when installing it, I was alarmed by the fact that it had not been updated for quite a long time and had not been tested with our version of WordPress. Well, what can I say, my fears were justified.

After activating the WP-Syntax Button, I decided to try inserting a java script into one of our articles. I did everything as indicated in the mountain of instructions and recommendations. I inserted the script into the article, selected it and pressed the “code” button.

Then he indicated the programming language and the line number from which the script output should begin.

You may be asking why I wrote about this experiment in this article? By this I wanted to say that when you are looking for some information on the Internet, pay attention to the date of publication. After all, there is a very high probability that the information in the old article was relevant several years ago, and this moment it simply doesn't work, and its use or implementation may leave your project vulnerable.

On our blog, I try to keep all the material up to date, constantly monitor updates and, if necessary, make adjustments to already written articles.

If you want to receive up-to-date information to your place email, then I recommend it.

Video “How to insert HTML code into an article”

Friends, if someone prefers to see how this is done, then I recorded a video for you. And I remind you that we also have a YouTube channel, which you can also subscribe to.

Related articles:

Well, I hope the article was not complicated, and I was able to tell you in detail about how you can insert html code into a website without unnecessary hassles.

Don't neglect the aesthetics of your blog. After all, our reader, as in life, meets you by his clothes and sees you off by his mind.

That's all for me!

Bye bye!

Sincerely, Kalmykov Anton

Do you want to post code with some kind of solution or instruction on your website, but are you having problems with it? Find out how to do everything correctly and display the code as it appears in the code editor.

It would seem not a difficult task to insert the code, but difficulties may arise if you do not know about some of the rules that will be discussed.

Paste code as code

To insert code into a website, it is wrapped in tags

AND .  This results in the following construction:

Here's the code

To explain briefly and to the point, in this design the code tag tells the browser that there is program code inside it, and the pre tag stores line breaks, tabs (indents) of lines and spaces.

It seems that everything is clear and there is nothing complicated, but the problem is that if you want to insert html code or code containing symbols< , >, & , " , ` , then problems may arise - the browser will process your code as HTML. For example, if you want to show the code of a form with a button, input fields, etc. in the code, the browser will display the form, and not its code.

Escaping code

To prevent this from happening, they do so-called code escaping - this is when the characters mentioned above are replaced with their UTF codes.

Scheme for replacing characters with UTF codes

Below are the symbols and codes that replace them:

Of course, no one is replacing symbols manually - this is done programmatically. The most accessible tool for this is online code escaping, where in an instant all characters will be replaced, and the code will retain all spaces and hyphens and will be completely ready for insertion into tag construction

As a result, after escaping, you can insert any piece of code. You can see an example of such an insert after escaping the code below:

Heading

Related posts

The method for inserting the html code varies slightly depending on where exactly you need to insert it. In accordance with this, we will consider different options.

To insert html code into the site, and in particular into the text of an article on one of the site pages, go to the administrative panel, select and open the desired article for editing. Next, the visual editor mode must be changed to editing mode. When working with a CMS system, simply click to change mode. Then copy and paste the html code into the planned location.

Often, after inserting, you want to format the code a little, for example, set the text to wrap around the code on the other side. There is a simple way to do this using CSS properties.

Using tag

, wrap your code in a container. This can also be done using table form tags. And inside this container, use the “style” property to set all the desired attributes.

To ensure that when saving an article the visual editor does not cut out changes that, in its opinion, are not html code, it is better to disable it when inserting the code. The editor's cutting of changes is designed to prevent the threat of possible malicious code.

How to disable the visual editor?

In the administrative panel, click “Site” - “ General settings" - "Site" and in the "Visual editor" line select "No Editor". Now it will be possible to edit and insert html code in html code mode. After you paste the code and save your changes, the visual editor can be turned back on.

If you want to re-edit the article, the html code will also need to be re-inserted. Sometimes it is required that the html code be displayed on each of the pages of the site. Inserting HTML code so that it appears on each page of the site.

To solve this problem, use Notepad or another editor to enter the main site file (by default this is index.php or index.html). IN open file select your preferred location to paste the code. For convenience, you can set a phrase in the search located in the place where you need to insert the code, so you will find it faster. Then copy the code and paste it to the desired location. If necessary, the code can be formatted in the same way that was described for formatting the code in the article.

There are many tools that are ready to help you improve your website, increase efficiency online business and establish interaction with clients. Many of these tools are free. But what if you're not a web developer and don't know how to install them on your site? This is a big obstacle.

As ironic as it may sound, a business that develops useful tools for your site also has an obstacle to its own prosperity - the need to force the installation of its code on users.

Let's try to solve two problems at once by talking about the most common ways to insert JavaScript code/snippets (fragments) for websites:

  • On WordPress.
  • Self-written admin accounts (using old-school FTP).
  • On Squarespace.
  • On Shopify.

At the end of the article we will touch on Wix. In most cases, this platform does not allow you to install JavaScript codes or snippets, but it does offer an alternative in the form of integrations for various services.

Where can I get the installation code?

Let's see where to look for the installation code using the Crazy Egg service as an example.

The process is standard: we enter registration data, after loading we get to the dashboard. There we look for the section with the code.

How to insert code into a WordPress site

You can use a special plugin – Tracking Code Manager.

To insert Tracking Code Manager, go to the site admin area and look for the plugins section in the control panel.

Select the “Add New” function.

A search bar will appear on the right. Enter “Tracking Code Manager”, then click “Install Now”.

After installation is complete, you need to make sure that the plugin is activated. Let's go to settings.

Click “Add new Tracking Code”.

You can do without the Tracking Code Manager plugin. In this case, our option is to paste the code into the header.php file WordPress themes(more on this later).

How to install code on WordPress.com site

Website at WordPress based and WordPress.com are two different things.

A WordPress website is a website that is managed using a CMS installed on its own server. This means you paid for Domain name(myname.com) and for hosting.

WordPress.com is free. You can register a website on it without paying for hosting. In this case, the site URL will be: myname.wordpress.com. To change your domain name, you'll have to take a few extra steps.

When you run a site on WordPress.com, you are limited in the JavaScript codes and snippets you can install. The reason is security protocol - the developers do not allow users to paste codes into the platform. For example, the MySpace developers allowed it. This is obviously why the site dropped from 5th place in the world to 1967 in six years.

At the same time, WordPress.com offers users various bonuses for doing business. For example, integration with Google Analytics or the ability to add social media buttons. But in some cases this may not be enough.

How to install JavaScript codes and snippets on a self-written admin panel

Using FTP. We'll have to go back to the basics.

Nowadays many websites are self-written. A self-written website was created from scratch and does not use a CMS to manage content.

Let's imagine that we have just such a site. Putting code on it will be a little more difficult than on WordPress, because you will have to remember about FTP.

FTP managers like FileZilla allow you to drag and drop files and images from a website to a server. Using the program, you can add files to the server or download them, replace old files with new ones, delete unnecessary data, change dock names.

There is nothing complicated about the data transfer protocol: after just 20 minutes you can feel like a pro. For most people, the hardest part is simply contacting the server.

If you have any problems with the server, please contact your hosting provider. It may take an hour, but after 60 minutes you will know everything you need to know. I usually recommend recording these conversations using ScreenRecorder. Or at least make notes by hand.

After you have logged into the FTP server, be sure to do . If you make a mistake, your important files won't go anywhere. You can make a backup copy by simply dragging and dropping all files from the server to your computer.

Usually software interface The FTP manager consists of two columns. For example, in the image below in the left column you can see files that are stored on the computer. On the right are files from the server.

To change or add files, simply drag them from one column to another.

Warning

Replacing files is dangerous. Can easily be replaced new version old file and lose more current data, accidentally delete something important, or make another mistake. Backup copy allows you to insure yourself in case of such mistakes and protect yourself from unnecessary problems. Be sure to save all files in a separate folder on your computer before making changes.

How to paste code using header.php

Let's say we have a website that we use WordPress to manage. To insert the code, we need to find the header.php file or another file called header or head.
Header needed to activate a JavaScript code or fragment on all pages of the site. To open the file you can use free program like Sublime Text. With its help you need to open a file that is stored on your computer, and not similar file on server.

When we open the file in Sublime Text, we see a rainbow of color code. Don't be afraid, everything is simple there.

All pages are divided into two sections – The head (headings) and the body (body). Headings are indicated by HTML code And. Body - And

.

Most of the stuff that makes the site work and display text is located between these two sections. To find these tags, you can use the page search.
In most cases, you can insert the code right before the closing tag.

After you have inserted the code or snippet, save the file and upload it to the server. Ready.

Now about self-written admin panels. If your site was built by hand and you can't find the head or header file, you'll likely have to manually insert code into each page. Most often this does not cause much difficulty.

The image above is a hypothetical HTML site that consists of four HTML files. If we wanted to paste some code onto it, we would have to open all these files one by one and paste the code or snippet into the tags of each of them. After this, the process is standard: save the files and upload them to the server instead of the old ones.

How to Embed Code on Squarespace Website

Squarespace has made it easy for its users. To insert JavaScript code or snippet, you can use “code injection”. In the menu, go to the Settings > Advanced > Code Injection tabs. You can add code to or website footer.

Finding the settings.

Scroll down and click on “Advanced”.

Select "Code Injection".

Paste the code and save the changes.

How to embed code on Shopify

In the control panel, find the Online Store section and select Themes.

To access the desired tab, click on “Actions” and select “Edit HTML/CSS”.

We need fragments.

Clicking on the option will open a dialog box with the option to add a new fragment.

Give the snippet a name and click on the Create snippet button.

Enter the code for the snippet in the area for "tracking snippet.liquid". Click "save".

How to insert code if the site is on Wix

Wix has quickly become a popular website builder. Like WordPress.com, it places restrictions on its users' ability to paste codes.

But Wix offers a solution in the form of the Wix App Market. There you can find a variety of widgets for marketing, social media and analytics. By the way, you can also vote for the most needed widget there.