Inserting PHP code into HTML. How to do this correctly? Html embed code what does it give

💖 Do you like it? Share the link with your friends

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 what is inside it program code, and the pre tag preserves line breaks, line tabs (indents), 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 characters< , >, & , " , ` , 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

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 style of 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. current state, I constantly monitor updates and, if necessary, make adjustments to already written articles.

If you would like to receive up-to-date information on your 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

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.

Sales Generator

Reading time: 14 minutes

We will send the material to you:

From this article you will learn:

  • How to insert html code into website pages
  • How to Add Custom HTML Code in WordPress
  • How can I insert html code into Joomla
  • How to Add Any HTML Fragment to a Wix Website
  • How to make beautiful html emails in Yandex
  • Is it possible to change the html code of the VKontakte page?

Often inexperienced users are confused simple task– how to insert html code, for example, into a blog, page, or letter. There are several options for how to perform the operation, it all depends on the specific tasks facing the user. Html code is a broad concept that allows you to place a lot on a page useful elements, add a picture and video, ticker, counter, banner. Using a programming language, you can create a unique product yourself, you just need to understand which html code to insert and what actions to perform.

What is needed to insert HTML code into a page: a prerequisite



It is unlikely that visitors to a website or blog will be attracted by plain text on a white field, even if it is at least three times useful. Every detail - font color, visual effect, arrangement of elements - is described in a certain way. Only an administrator (owner) with editing rights can create the structure of a website or blog page. Therefore, a person who does not have access to the admin panel will not be able to insert the html code.

The administrator's responsibilities include full control over the operation of the resource. He creates themes, includes new plugins in the structure, adds pages, etc.

To enter the site for management and editing, there is an administrator page, a kind of control panel, where the administrator coordinates any process, adds, deletes, and creates new elements.

Let's say you decide that for some purpose you need to create your own portal. A website will not appear out of nowhere; you need to place it on a platform or engine.

The most popular engines are:

  • Joomla,
  • Bitrix.

In addition to paid platforms, there are also free ones, such as Wix.

Regardless of the choice of management system, each has an administrator section for filling the site with content.

Let's say you are the administrator of a portal that runs on WordPress. You can log in to the administrative service by adding the link http://resource name/wp-login.php in the address bar of your browser.


In the field that opens, you need to insert a unique login and password, click on the enter button, and the administration page will open. If you enter incorrect data, the system will block the entrance, displaying a notification about an identification error, and ask you to re-enter your data.

It’s just as easy to get into the content management system on the Joomla platform. In principle, logging into the administrative section is identical to what was just described for the WordPress engine. The link for the address bar is http://resource name/administrator, then press Enter and fill out similar windows with your registration data. The only difference is that Joomla administrators have one password – admin. The user receives identification data from system administrator provider who manages the hosting of the resource. If entered incorrectly, the password is reset and re-identification is required.

So, no matter what platform your Internet site is based on, a user will not be able to insert html code without administrative rights.

How to insert html code into a WordPress site

1. Installation of meters and services

The structure of any site implies the addition or removal of any plugins, html codes of popular services, such as visitor counters, or services that analyze Internet resources, or other applications for web analytics ( Google Analytics or, for example, “Yandex.Metrica”), a meta tag confirming rights to the Yandex.Webmaster site, or any derivative code.

There are several ways to insert HTML code into a page in WordPress:

  • manually;
  • add a ready-made plugin.

Sometimes you have to enter HTML code manually into the theme file - a problematic method, but in certain cases there is no alternative. When refreshing the page, be prepared to have to do the procedure again, so keep copies of the files that you had to change on hand.

More often, users use convenient and practical plugins.


Many plugins are actually added using the control panel settings. Some place it in the header, that is, placing the html code inside the container …, others need to be inserted into the so-called footer, the content area, before the tag

(or closing).

For convenience, it is worth adopting the very effective LuckyWP Scripts Control software module. The add-on with a user-friendly interface allows you to insert blocks of HTML code in any order, placing them in the desired area using drag and drop. The plugin can be found by following the link https://ru.wordpress.org/plugins/luckywp-scripts-control/ to the official WordPress engine repository, it is officially distributed, installing it does not require any time or special knowledge. Activate the program and the Scripts button will appear in Settings in the Control Panel.

Keep in mind that it is possible to insert the html code before (closing tag), other situations require just such a step.

What html codes can be entered:

The LuckyWP Scripts Control plugin copes well with its functions; its operating algorithm is accessible even to an untrained user. The program is free and has a Russian-language interface. With its help, plugins can be edited, deleted, hidden, or, conversely, shown.



Even more simple steps are implied when it comes to embedding arbitrary HTML code in a sidebar or placing text on the home page. No third-party tools, such as plugins, are needed. WordPress Platforms latest versions are equipped with a built-in widget for inserting arbitrary HTML code.

Adding occurs when you go to the menu “ Appearance» to the “Settings” tab (optionally – to the “Widgets” tab).

We determine the area where to insert the html code on the site.

When inserting, for example, a counter into a sidebar, we will need the “Sidebar” or Sidebar element; when we click the “Add Widget” button, we find in the drop-down list required code html.

In the new widget, all you have to do is paste the code and save the changes. A similar algorithm is suitable in any case when widgets are used.

3. Inserting an HTML fragment on a separate page



It’s even easier to insert html code into a page, for example, into any content - through the control panel, we find the article you need to edit and open it. Then we go into editing mode and simply place the html code in the desired sector. The visual editor may interfere with your changes and delete them because it does not read the insert as html code. This action is intended to exclude malicious codes. Therefore, when saving, you must disable the visual editor.

How to show html code examples on a page in WordPress

Typically, HTML code is not output in WordPress. The exception is specialized resources that discuss programming, website promotion, web design, etc.

Advanced authors, instead of simply adding code or its individual fragments to the site, use syntax highlighting or implement a simple action in other ways. For guests of such resources, lighting supposedly makes it easier to perceive.

If you insert html code into the text, then in WordPress it will be distorted. To avoid code interpretation, processing is applied - ready-made plugins are used or necessary tags are written manually using an option in the editor.

  • How to add code to an article without a plugin

In WordPress, the resource administrator is equipped with a special assistant built into the content management system. If the user adds one or more code snippets in visual mode, then going to text editor WordPress changes characters in tags with<текст>on< текст >.

In order for the content management system (CMS) to understand that this block should be reflected in the same form on the page, it is processed in text mode - highlighted and framed on both sides with a tag . There is a button of the same name on the toolbar for such an operation.

On the page we will see the following picture:

Name 1

Name 2

Value 1

Value 2

Insert html code with certain indentations into WordPress page tag helps

Which, when changing a visual editor to a text one, retains all the spaces that are used to form the desired indentation.  Browsers default to displaying tag contents And 
Monospace font.

An example of how to correctly insert HTML code into a page in WordPress without plugins using a tag

Name 1

Name 2

Value 1

Value 2


Expand tag functions

You can use universal attributes, for example, for text alignment (align) or style definition (style).

Let's look at an example of how to use the values ​​of the style attribute to assign the color of the text and background of a block, the thickness, color and radius of the block border:

Attributes are widely used to format blocks that appear on a page, but their potential also has limits.

If you insert small blocks of HTML code or one line into the page as examples, then the previously discussed tags are the best fit for this. As a result, the insert looks unpretentious, like the rest of the design, but for some purposes this is enough.

Complex formatting, such as syntax highlighting, requires plugins, which will be covered later.

  • 2 Handy Code Insertion Plugins for WordPress

For beautiful design of codes - html, java, css, php - some WordPress plugins, available in the official repository. Using them, you can highlight the necessary elements, display line numbering, etc.


How to insert html code into a Joomla website



When building and setting up a site based on Joomla 3, the master is faced with the need to insert third-party codes - php, JavaScript or html. Let's look at how to insert the html code of the LiveInternet hit counter into a module or article on Joomla.

To implement the required code, we will use a plugin for code processing that works directly with sites on the Joomla engine - Sourcerer, with its help you can add any common code, be it html code or css, php, javascript, in any sector - in the body of the article, into a category, meta tag, component, module, etc.

Please note that in free version The functionality of the application has been reduced, but all the main functions have not been affected.

After downloading the plugin, install it. Sequencing:

  1. In the administrative section, find the “Extensions” section and go to the “Extensions Manager” tab.
  2. Without changing the preset “Upload package file” setting, click on the “Select file” option.
  3. In the saved files we find the archive with the program and open it.
  4. Download and install the plugin.
  5. Upon completion of the process, the system will notify you that the module has been installed successfully.

The plugin does not require any special settings; by default, the optimal parameters are already set. Now you can paste the html code into the selected page.

  • How to insert code into a website using a module

In the “Administrator Extensions” section we will use the “Modules Manager”. Using the corresponding button, we create a new module. From the proposed options on the “Select module type” tab, find the HTML code and click on the link.


In the new module, fill out the “Title” window, write the opening and closing tags (source) and (/source), all that remains is to paste the copied html code between the tags.


We check whether the new module works by going to the desired page of the site.

  • How to insert code into an article in the Joomla editor

The operating procedure is identical to inserting code into a module. Having installed the Sourcerer application, we write the tags (source)(/source) in the material editor and place the required code between them.

How to insert HTML code into a Wix website

Working with the Wix online designer has its own nuances. Adding extraneous code or plugin script to your site requires an HTML application.

External code or its components are referred to as iframes. Using the HTML application you can customize external form site or install, for example, a calendar or any widget that Wix does not have.

Brief instructions on how to insert html code into Wix:

  1. On the left side of the editor, click the “Add” button.
  2. Click on the “More” button.
  3. Drag the HTML code application onto the page or click on it.
  4. The next step is “Insert code”.
  5. Add the required HTTPS code.
  6. We complete the work with the “Apply” button.

The Wix application works flawlessly if certain conditions are met:

  1. The embedded code must have the HTTPS extension. Code with HTTP simply does not appear in published form.
  2. The html code is defined by paired tags, for example opening

    And closing

    . Following this tag format is necessary to maintain the structure of the page you are formatting.
  3. The HTML code contains the basic elements of the page; other codes are written for design settings and some functions. If any elements do not display or work correctly, then these options are not included in the HTML code. An example is CSS or JS files.
  4. Before embedding, make sure it is updated and compatible with HTML5. Some browsers incorrectly display scripts and pages written in some outdated versions HTML.
  5. The HTML application is an iFrame. In other cases, there is incompatibility between the embedded code and the site, although this has not happened before.
  6. Some sites are equipped with protection against embedding on other platforms (Wix is ​​one of them). Such a site may not appear in the editor or on your resource after publishing a link or inserting code in an HTML application.

How to insert counter code on the Bitrix website

Bitrix is ​​a success among developers of commercial resources. Installing Bitrix counters on a website is easy - open a general template and paste the html code copied to Yandex.Metrica or Google Analytics.

User actions - the administrative panel expands on the page, the “Site Template” tab opens, go to the “In the control panel” section, where we click on the “Edit template” option.


You can insert the html code of the counter directly in the editor.


One of the important conditions is that the same template is applied to all pages.

How to insert html code into a letter from Mail.ru or Yandex.ru

Sometimes you need to insert a code into an email message to give it a unique style, for example, for corporate correspondence or mailings. Majority mail servers do not have formatting tools.

But it is still possible to change the design of letters on free platforms like Mail.ru or Yandex. Design a unique letter, for example a newsletter with visual elements, is a labor-intensive, but completely doable operation.

Let's look at how to insert html code into a Yandex or Mail.ru letter.

First you need to create a letter template. Its width should not exceed 640 pixels for comfortable viewing on small monitors so that the letter looks like a narrow page. Layout of letters is a fairly common technique, and there are many instructions on the Internet for creating a beautiful interactive letter. We check how the selected email service will react to the embedded template.

First of all we test email clients, which we most often use ourselves:

  • Mail.ru;
  • Yandex;
  • Mozilla Thunderbird;
  • The Bat;
  • Gmail;
  • Microsoft Outlook.

If the template passes the test, we move on.

It is believed that only two clients allow you to insert HTML code correctly - Microsoft Outlook and Mozilla Thunderbird. But since most users are accustomed to using only Yandex or Mail.ru, we try to outwit their native mail programs.

We install and Mozilla setup Thunderbird on account user.


In the settings, we must note that IMAP, the protocol for incoming correspondence, and SMTP, the protocol for outgoing correspondence, will be used for data transfer. Such an operation will open the possibility of downloading mail in Mozilla Thunderbird so that Yandex servers do not delete it and automatically update all messages from Mozilla Thunderbird.

With this manipulation we will be able to create email templates in Mozilla Thunderbird in HTML format and automatically download them to the Yandex mail folder.

We place the html code in the letter and save it as a template in Thunderbird.

After a few minutes, we fix the required “template” folder in the Yandex program and find there a typed letter with html codes. You can modify the created template and save it in the desired form in Yandex mail.

When creating a new letter, we will use our template, for which we need to click on the green “Template” link and load the required template. Yandex Mail has a special folder in the “Drafts” section for storing templates.

The problem with letters with html code is the following: the recipient does not see pictures in the received letter. It often happens that a user downloads images from third-party sites and the receiving party does not display the images.

The ways to fix the error are as follows:

  • We upload the images that we have chosen for layout to the Yandex disk (when sending mail from a Yandex server), to the Mail.ru cloud (when sending a letter from mail.ru). We provide direct links to the uploaded image in the service. Make sure that the selected link opens only the image and nothing else.
  • Try loading an image into the body of the letter, converting it to base 64. All that remains is to place the image code in base 64 format in the tags

The most effective option is to re-encode the picture into base 64 code - this is both more reliable and takes less time (your picture will no longer be deleted from the Yandex disk, since it is placed in the letter).

The goal of creating a beautiful letter has been achieved. If you want, you can uninstall Thunderbird. Now your emails are a truly unique product, and you are a master who knows how to insert html code anywhere.

How to insert html code into VK

It is quite possible to change the VKontakte page by adding HTML elements, but the final result will please only the account owner, since only he himself can see the fruits of formatting.

User social network can only use the tools that are available in the online service settings. In order for a page you made yourself to appear on the Internet, you need to upload it through hosting, and access to ftp is denied to an ordinary user.

Sometimes, for personal purposes, they change a page on VK, as a joke or for some kind of manipulation, but, we repeat, you can only see the result from your device.

First, open the page where you intend to insert the html code, and activate the option to view its code combination Ctrl+ U. Next, copy the opened code into an html editor, for example Notepad++.


Let's go to context menu, select the Edit with Notepad ++ option and start editing. You can save the created file to your computer by using the key combination Ctrl + S or “Save to computer”. The new web page will be loaded on HDD, including all its constituent elements - pictures, scripts, etc. In offline mode, you can view your version on your computer, you can even take a screenshot of it. But you cannot upload the file to the online service.

How to insert HTML, CSS and JS into PHP code

When developing his own module, the webmaster uses additional scripts and layout (CSS, HTML). Each element and html code is included separately, in the body of the page or any selected file. But there is a special category of add-ons that need to be inserted directly into the PHP file.

Let's look at two ways to correctly insert html code, JavaScript or CSS into PHP code.

  • The first option for inserting elements into PHP code

When working with a page written in PHP, the webmaster operates with the echo tag (which helps display the message on the screen).

Using this tag, we also display one of the previously mentioned codes.


Please note the specific use of quotation marks. When using outer quotes like " ", the inner quotes surrounding elements must look like " ", or the reverse, otherwise an error occurs. A user who wants to use the same external and internal quotation marks must put a screen mark in the internal ones.

With this approach, changes will be displayed correctly.

  • The second option for inserting elements into PHP code

A more common and popular option is using the echo tag, but unlike previous method, additional html code is applied.


You can insert HTML code, JavaScript or CSS into such a design. Quotes are no longer given key meaning; they can be anything. It is possible to implement output variables if desired.

The shown option is very convenient for implementing many ideas.

Before insert code into site you need to know the structure of the site. You shouldn't paste the code anywhere either. For certain elements site, there are places designated for them. For example, they try to place advertising banners at the top of the site, and traffic counters at the bottom.

What code do we want to insert?

  • For example, we want to insert (connect) some javascript, as usual, scripts are either placed in a separate file or placed at the top of the site.
  • If you want to insert a banner with advertising, then it is preferable to select the top part of the site (site header, navigation menu, space above the post), but in some cases advertising banners inserted inside the text or at the end.
  • The code for social bookmarks (icons) is placed before the post (at the beginning of the page) and at the end.
  • Attendance counters, if placed at all, are located at the bottom of the screen.

This is just a small part of the examples.

In each specific case, one or another part of the site is selected based on its specifics.

We are looking for (determining) a file to place the code

As a rule, several files are responsible for displaying a site. Here are the most popular ones in WordPress: index.php; single.php; page.php; header.php; footer.php; sidebar.php; style.css.

Top of the site

The file is usually responsible for outputting the upper part header.php (site header). The beginning of this file usually looks like this:

> You can paste the script here

Between tags place scripts that work on all pages of the site.

The content of these tags may vary for each topic.

Site's home page

Per display home page The site responds with the file index.php, most often using the PHP language, this file combines code from other files, for example (header.php, footer.php), thereby the page displayed in the browser is assembled from several files.

Site Side Menu

The file sidebar.php is usually responsible for displaying a sitebar (side block) on a website; here you can insert a block with advertising.

Outputting records

If you want to change the display of blog posts, use the single.php file.

Site pages

To change site pages, use the page.php file.

Site footer

Another file that is found in almost every template is footer.php; you can also place scripts in it; it is often used to insert traffic counters.

Page appearance

If you want to modify the display of the site, then you will need a style.css file.

Now you know approximately which files are responsible for the output of certain parts of the site.

Determining where to insert the code

To understand where to insert the code, you can use a standard tool Google Chrome To do this, right-click on a free part of the page and select the View Code tab.

With its help, we can determine the name of the block above, below or in which we need paste your code.

To select the desired block, click on the button highlighted in red and point it at the desired area of ​​the site.

The picture at the top shows a panel in which you can see the code of the site or the selected block.

Please contact class name that block above, below, into which you can insert the code.

Now insert the code you need and check the changes.



tell friends