How to add ad units in wordpress. Adding a widget block to a WordPress template. Adding theme support

  • 18.02.2024

It is very difficult to find a site on the Internet that does not contain third-party advertising (Google AdSense, Yandex.Direct, Begun, etc.). It’s no secret that advertising on a website brings a certain profit to the website owner. On some sites this profit amounts to a decent amount, but on some sites this profit is barely enough to pay for hosting and a domain name.
Of course, there are sites on the Internet that do not contain advertising. Such sites are mainly selling resources. These sites are not made to generate income directly from them, but to present and sell certain products to the user.

You can also see ad units on almost all blogs running on the WordPress engine. This blog is no exception.
Another thing is that in order to insert high-quality advertising into a blog on WordPress, you need to understand at least a little about the structure of the engine itself, and have at least a general understanding of html and css.

You can also use special plugins to insert advertising blocks. Search the web and find a plugin for yourself. Personally, I haven’t used such plugins because I think that an extra plugin on WordPress increases the load on the blog. Moreover, with the appropriate knowledge, you can perfectly insert advertisements without using a plugin.

If you understand the WordPress engine, you can embed advertising directly into the code. Typically these are files in the directory: /wp-content/themes/your theme/, such as: index.php, sidebar.php, single.php, page.php.
For example, here is the Yandex Direct code inserted under the main content of articles on the main page of this blog:

1 2 3 4

By inserting advertising in this way, ad units will be shown before the beginning of each article on the blog. Similarly, you can insert advertisements into each page using the single.php file:
But to insert advertising into the content of the article itself, this trick will no longer work. The WordPress blog editor only recognizes standard HTML, and the code contains other programming languages, so the built-in editor will simply cut them off.

But this issue can also be resolved. We look for the file in your theme files: functions.php. If there is none, then create it in the Notepad++ editor. In general, it is better to work with WordPress files in this editor, since it saves all files in the required encoding.
Open the functions.php file in Notepad++ and write the following code:

Escaping in PHP is done using the backslash (\) . You must put a backslash before each single quote. This needs to be done only with those ad block codes that contain single quotes. For example, in Yandex Direct:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

In the case of Google AdSense, no escaping is required:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

And so, using Yandex Direct as an example, the name of the created function is: 'yandex', output code: 'yandex_content'. Now, when writing or editing an article, we simply insert: [yandex_content] in the right place, and advertisements appear on the blog in this place.
The same is true with the display of Google AdSense blocks. We insert it in the right place, and we see advertising on our blocks.

This way you can create multiple functions in the functions.php file. You can give any names to the functions and output code. Naturally only in Latin characters.
Earn money worthy of you from advertising.

if (function_exists("register_sidebar")) register_sidebar(array("name" => "Right sidebar", "before_widget" => "", "after_widget" => "", "before_title" => "", "after_title" => "",));

in line 3 we set the widget name (name). Our name is Right Sidebar. You can enter any name, be it in English or in Russian, but it is important to remember it, because it will need to be entered in one more place. Also in lines 4 and 5 you can write the displayed HTML code before and after the widget (before_widget, after_widget). Our default is empty. In lines 6 and 7 there is code before and after the header. That is, you can customize your title class and the title will be different in the new widget. Save our functions.php and go to the admin panel to the “Widgets” section. If everything was done correctly, you will find a new block there. You can immediately add some widget so that you can later check whether we did everything correctly.

2. Display the block in the template itself

To do this, open the required file. This could be header.php, footer.php, single.php, etc. It all depends on where you want the new block to be. Then paste the following code into the right place.

In the second line we see - Right sidebar. This is exactly the name that should match what is in the functions.php file. That is, if you named your block “Place for counters,” then this name should be both there and there. That's all. We save our file that we edited and go to the site. After refreshing the page you should see the result. All that remains is to write the styles in the style.css file to customize the new block.

That's all. Thanks for your attention :)

thank you in advance

thank you in advance

","contentType":"text/html"),"proposedBody":("source":"

thank you in advance

thank you in advance

","contentType":"text/html"),"authorId":"40638173","slug":"52","canEdit":false,"canComment":false,"isBanned":false,"canPublish" :false,"viewType":"old","isDraft":false,"isOnModeration":false,"isSubscriber":false,"commentsCount":10,"modificationDate":"Thu Jan 01 1970 03:00:00 GMT +0000 (UTC)","showPreview":true,"approvedPreview":("source":"

thank you in advance

thank you in advance

","html":"if inserted into index.php, the link only leads to the main page. if you put it in single.php, the block is not visible on the main block.","contentType":"text/html"),"titleImage":null,"tags":,"isModerator":false,"commentsEnabled":true," url":"/blog/share/52","urlTemplate":"/blog/share/%slug%","fullBlogUrl":"https://yandex.ru/blog/share","addCommentUrl":" /blog/createComment/share/52","updateCommentUrl":"/blog/updateComment/share/52","addCommentWithCaptcha":"/blog/createWithCaptcha/share/52","changeCaptchaUrl":"/blog/api/ captcha/new","putImageUrl":"/blog/image/put","urlBlog":"/blog/share","urlEditPost":"/blog/569dda94a427fd90608af952/edit","urlSlug":"/blog/ post/generateSlug","urlPublishPost":"/blog/569dda94a427fd90608af952/publish","urlUnpublishPost":"/blog/569dda94a427fd90608af952/unpublish","urlRemovePost":"/blog/569dda94a427fd90608af 952/removePost","urlDraft":"/ blog/share/52/draft","urlDraftTemplate":"/blog/share/%slug%/draft","urlRemoveDraft":"/blog/569dda94a427fd90608af952/removeDraft","urlTagSuggest":"/blog/api/suggest /share","urlAfterDelete":"/blog/share","isAuthor":false,"subscribeUrl":"/blog/api/subscribe/569dda94a427fd90608af952","unsubscribeUrl":"/blog/api/unsubscribe/569dda94a427fd90608af952" ,"urlEditPostPage":"/blog/share/569dda94a427fd90608af952/edit","urlForTranslate":"/blog/post/translate","urlRelateIssue":"/blog/post/updateIssue","urlUpdateTranslate":"/blog/ post/updateTranslate","urlLoadTranslate":"/blog/post/loadTranslate","urlTranslationStatus":"/blog/share/52/translationInfo","urlRelatedArticles":"/blog/api/relatedArticles/share/52", "author":("id":"40638173","uid":("value":"40638173","lite":false,"hosted":false),"aliases":(),"login": "reva-money","display_name":("name":"reva-money","avatar":("default":"0/0-0","empty":true)),,"address": " [email protected]","defaultAvatar":"0/0-0","imageSrc":"https://avatars.mds.yandex.net/get-yapic/0/0-0/islands-middle","isYandexStaff": false),"originalModificationDate":"1970-01-01T00:00:00.000Z","socialImage":("orig":("fullPath":"http://avatars.yandex.net/get-yablog/4611686018427442682 /normal")))))">

Chances are you have blog posts that you'd like to highlight. These are usually called "Featured Posts" or "Featured Content". If you're using WordPress, displaying these featured posts can be achieved in a number of ways, one of them is by using a plugin like Jetpack.

Jetpack is a set of features for your WordPress site. At the time of writing this guide, there are about 30 such features, including WordPress.com Stats, Photon, Infinite Scroll, and what we'll be focusing on today - the Featured Content block. Let's get started. Adding theme support

Update: In Jetpack 3.7, the form for featured content is under Appearance → Menu.

The first thing you need to do is add the add_theme_support function to your functions.php file:

Add_theme_support("featured-content", array("featured_content_filter" => "mytheme_get_featured_content",));

Once added, you'll see a new form for featured content on the Options → Reading page.

Specify a tag for featured content, set how many posts you want to show, and check the box if you want to hide this tag from blog visitors. Apply this tag to blog posts that you want to mark as favorites.

Content display

We'll add a few lines of code to display the content on the blog. I'll be using the TwentyTwelve theme as an example in this tutorial.

Typically, featured content is displayed on the home page. If your theme follows the standard WordPress theme structure, the file responsible for your front page is index.php , home.php , or front-page.php .

Open functions.php and add the following function (you will get the featured posts and put them in an array):

Function twentytwelve_get_featured_content() ( apply_filters("twentytwelve_featured_content", array()); )

We can extend the code like this:

function twentytwelve_get_featured_content($num = 1) ( global $featured; $featured = apply_filters("twentytwelve_featured_content", array()); if (is_array($featured) || $num >= count($featured)) return true; return false; )

The following conditional expression will show featured content if there is at least one such entry, and if the page has not been split into several.

In addition, we can also set new thumbnail sizes for featured content. In this example, I created new dimensions - 250 by 160 pixels. You can add the following code somewhere under add_theme_support("post-thumbnail") :

Add_theme_support("post-thumbnails"); add_image_size("twentytwelve-featured-thumb", 250, 160, true);

. And we make small changes to it; for clarity, we highlighted in green what needs to be added, and in black what remains unchanged in your template and does not need to be touched.

Each template may have its own nuances, the cycle codes may differ, there may be several different conditions, a bunch of additional blocks, links, different functionality, etc. But the essence remains the same, we need to insert the $count variable into the loop, assign it the value zero and then increment the counter

Having reached the post required by the account, our advertising code is triggered, to which we set the condition

This code will output your ad unit (or whatever snippet you want to inject into the loop) after the 2nd entry. You can change this number at your own discretion in this place - $count == 2 by replacing the number with the desired one.

You can also add output conditions, for example, displaying advertising blocks immediately after several post announcements in a cycle. This can be done by slightly changing the conditions in the code.

For example, it will insert two advertising blocks at once after the 1st and 3rd posts in the feed.

By the way, this feature can be used not only on the main page, but also in other loops, for example in categories, for this, look for and edit the archive.php file.

I hope this information will be useful and will bring you a lot of money)