Inserting PHP code into WordPress via widgets. Inserting PHP Code in WordPress via Widgets PHP Code Widget Plugin


We can't seem to write some PHP code into the widget? ...that is, it’s possible to register it, but making it work is not possible! - although we managed to get such a cool gadget in the web waves of interneting))

A tragedy, we think! But that's not true! For this purpose there is a FILTER-PERMISSION for processing php functions in sidebar widgets, but! first things first...

(After the WordPress 4.8 update, this article seemed to become not very relevant, because the functionality of WP widgets was also updated somewhat! It became not very convenient to work with PHP code in a text widget, which allows you to manage content more finely!

How to return the old text widget? ... just below there will be a link to the post in which the solution to the problem)


Sections of the article:

Extending PHP widget capabilities

Attention!!

...for administrators who don’t want to delve into the code personally, I dare to offer my plugin, which creates a classic text widget.

The plugin can:

immediately after installation and activation, a new one will appear in the “widgets” section RealMik - html, php, shortcode...— in the text window of which you can enter any PHP codes or shortcodes, which will immediately work on the front-end side!

The plugin causes no load on the server... and no script conflicts!

Download the widget plugin easily!! in the WordPress depository - WordPress.org or on the page at the link above.

But, for those who want to understand the mechanics of working out PHP in a widget personally!! - below is a detailed article:

For example, you wanted without using plugins!!! expand the capabilities of your blog, namely, write code (a useful thing for optimization)) well, or something else, it doesn’t matter... And when you come to the main process of choosing a place for the “lotion”, then, naturally, many prefer It’s convenient to write this code in a sidebar widget, but here’s the problem: for some reason, the WordPress system initially prohibited this feature by the TEAM to prohibit the use of php in website/blog widgets.

And what!?

...to give up everything!? ...when the code of the TOP commentators is already screwed into the function file, and even in CSS file Do you already have some nice ideas??

In no case. To implement PHP processing in a widget, there are many possibilities and different ways(), which give the command WordPress system“allow” user (that is, our) settings, to put it simply. OK…

...we will implement the possibility of registering and testing PHP code in our widgets.

In the following articles:

Let's look at how to set an introductory text expression directly in html document required color.

...city of web masters Mihalika.ru© - easily with WordPress

Good afternoon, dear bloggers! If you are not already one, I recommend that you join our ranks! Just start doing it and so on. I'm sure you will succeed!

So today I will talk about PHP output via widget in WordPress.The lesson is not mandatory, but it may be useful for many beginners.

Let me remind you that our blog has all sorts of codes, lists, voting, etc. in the sidebar. can be output in two ways:

  1. By editing the sidebar.php file using text editor, For example, .
  2. Through WordPress admin using widgets.

With the first case, perhaps everything is clear. On this basis, I told almost all of my stories. We took the plugins, added the necessary code to sidebar.php and enjoyed life. But I remember when I first started creating blogs, I didn’t like this method, I loved working with widgets.

Why? Yes, because using widgets is a very simple method. No need to delve into the code, just insert the required widget into the sidebar, select the necessary parameters and that’s it. But when working with widgets, I noticed one unpleasant thing: not all plugins had the ability to display content through widgets and only codes could be used. It was this reason that forced me to constantly use only 1 method.

But while working, many customers asked me to display what they needed through widgets, since it was difficult for them to insert the code themselves. I had to look for a way out. I just found a plugin that displays PHP in the widget.


Examination:

Let's do a check and try to insert a regular one into this widget PHP code. For example, I entered this code:

I click on the “Save” button and look at the result on the blog:


As you can see everything works great! I think many people, especially beginners, will find this lesson very useful! After all, now you can easily display any plugin through PHP, simply by using an easy-to-use widget.

See you in the next lessons!

Hello, dear friends!

This time there will be, perhaps, one of the shortest and easiest articles.

In it we will talk about how to insert PHP code into a WordPress widget so that it works, because by default the engine does not provide this function.

And often you want to place some PHP code there that displays a list of popular or most commented posts. Take the same block of TOP commentators that is on every blog. If you do these things without a plugin, you will need to place the PHP code in the widget.

By the way, very often beginners encounter a problem when they read some article on blogs about previously discussed functions, I do them, but for some reason they don’t work. The code itself is simply displayed in the widget, but the function itself does not work. But it turns out that the author of the article simply did not mention this point.

Therefore, you need to make PHP support in the widget so that the code is not just placed in it, but actually displays a specific function.

Everything is done very simply, both with and without a plugin. I will show 2 methods, but I strongly recommend doing it without a plugin, since everything is incredibly simple.

PHP support in widget without plugin

To implement PHP support in widgets without a plugin, you just need to place the following code in the functions.php file of your template.

function php_in_widgets($widget_content) ( if (strpos($widget_content, "<" . "?") !== false) { ob_start(); eval("?" . ">" . $widget_content); $widget_content = ob_get_contents(); ob_end_clean(); ) return $widget_content; ) add_filter("widget_text", "php_in_widgets", 99);

function php_in_widgets ($widget_content) (

if (strpos($widget_content, "<" . "?" ) !== false ) {

ob_start();

eval ("?" . ">" . $widget_content ) ;

$widget_content = ob_get_contents () ;

ob_end_clean();

return $widget_content ;

add_filter ("widget_text" , "php_in_widgets" , 99 ) ;

Is it easier to place it at the very end before the closing tag?>.

There may be a situation where in your file of this tag will not be. Then you will have to place it somewhere between other codes. But, if you do not understand the contents of this file at all, then do not do this.

Let's do it simpler - place the code at the very beginning of the file before the opening tag



Next, save the file and replace it with the original file on the hosting. Your WordPress site should now support PHP code in widgets. We place such codes through a regular text widget.

We looked at the method without a plugin. I hope you choose it. Well, if you are not satisfied with it in some way, then move on.

PHP Code Widget Plugin

The second method is called PHP Code Widget, after which a new widget called “PHP Code” will appear.

You just need to move it to the required area, for example, to the sidebar and paste the required code into it.

In fact, this method is similar to the first one, when a special code was used. Only instead of placing the code in a template file, we installed a plugin that adds a new widget to the admin panel.

If I choose between a plugin and code, I will of course choose the option without using a plugin to reduce the load on the database and .

If you are interested in other materials on implementing various functions without plugins, here is a list:

This list is constantly updated. Therefore, stay tuned for new articles and make your site better. I'll wrap this up. The article, as promised, was very simple and short.

I wrote it, so to speak, as an addition to large detailed manuals, since I don’t always have enough energy and time to devote attention to considering extensive topics. But I will continue to try to please you with detailed materials and video lessons.

If you want to see something specific, there is always a comment form. Write whatever you want. Except for spam, of course. And this is where I end.

Best regards, Konstantin Khmelev!

Hello, dear friends and blog guests! Today’s post will be useful for both beginners and seasoned webmasters who don’t yet know how to insert PHP code into Wordpress on pages, posts and sidebars so that it can be executed by the WordPress engine.

I want to tell you in this article about this and how you can insert php code into wordpress without using a plugin and using a plugin. You will also learn all the nuances and subtleties, pros and cons that may await you when performing this function on WordPress.

Sometimes there is a simple need when creating a website to insert PHP code into WordPress in posts, pages or sidebars. I encountered this myself once and when I tried to simply paste the php code into the sidebar, it was displayed as plain text. That is, it was not executed as a function.

Why is execution necessary? php code on WordPress in posts, pages and sidebar? Every webmaster decides for himself! For example, there was a need to insert some kind of script created in PHP code into your posts, or you need to display some unusual menu in the sidebar, you never know.

So, in order to display executable php code in WordPress, you need to do some actions, and we will now find out which ones...

How to display executable PHP code in WordPress without a plugin?

This method of outputting PHP code to WordPress will be relevant for those who are ardent opponents of installing unnecessary plugins on their resources. And now you will find out how to do this.

To do this you will need to make some changes to the file functions.php your active website theme. Just add this code at the very end before the " ?> " :

/* to insert executable php code in WordPress articles/pages, use this shortcode: code */ function exec_php($matches)( eval("ob_start();".$matches."$inline_execute_output = ob_get_contents();ob_end_clean() ;"); return $inline_execute_output; ) function inline_php($content)( $content = preg_replace_callback("/\((.|\n)*?)\[\/exec\]/", "exec_php", $content ); $content = preg_replace("/\((.|\n)*?)\[\/exec\]/", "$1", $content); return $content; ) add_filter("the_content", " inline_php", 0);

Then use this shortcode in your posts and articles to insert executable php code:

Here is the php code

  • A small clarification, which is a very important point for the correct display and output of php code in your posts on the site!

Be sure, when you embed your PHP code into a shortcode, do not use this: “"! That is, your executable php code inserted into the shortcode should look like not this way " " , A like this "echo date("Y");". And ultimately, when you paste it into the shortcode, it will look like this:

Echo date("Y");

  • Another warning for novice webmasters!

Executing PHP code in your WordPress posts in this way makes your engine vulnerable to all sorts of hackers - crackers who can use it for their own selfish purposes. To do this you must change the value " exec" in the code that is inserted into the file functions.php to your own, which is known only to you! And accordingly use the same value in shortcodes to insert into posts.

By this action you can avoid the fact that someone will be able to take advantage of the potential “hole” created as a result of using this function - insert PHP code into Wordpress.

This is all about inserting php code into WordPress without using a plugin.

Now let's find out how you can also insert php code into WordPress, but using a plugin...

How to embed executable PHP code in WordPress with the Exec-PHP plugin?

There are many plugins for inserting PHP code into WordPress for execution. But not all of them are safe, since when they are installed and used, vulnerabilities appear in the engine that unscrupulous hackers can exploit.

I will now tell you about the safest and most functional plugin for php execution code in WordPress is Exec-PHP. First you need to download it or from official WordPress website or simply install from your admin panel on the site. I hope you know how to do this?! If you don't know, then take a look. There is also a video clip in which you can see everything with your own eyes. At least there we're talking about about installing a different plugin, but the principle is the same.

After installing and activating the plugin, the following will appear in your main admin menu:

Click on it and you will be taken to:

Select here to execute/ignore PHP code in widgets and that’s it.

Then, in order for you to be able to insert executable PHP code into WordPress in posts and pages, you must check the box in your profile - :

All. Now you can add to your pages and posts any php code in wordpress and it will be executed!

Thank you for your attention! Good luck to all! See you again!

Good afternoon

In this article I want to capture a very useful piece of code that allows you to insert php code into WordPress widget without using a plugin. Read on to learn how to do this and why you need it.

For those who don't know, let's first understand what is a widget?

Widget(from the English widget - thing, pop =)) is a block or container in which you can place various content: text, links, html code, Images.

This code needs to be added to the functions.php file of the theme used:

add_filter("widget_text", "my_widget_execute_php", 100); function my_widget_execute_php($text) ( if(strpos($text,"".$text); $text = ob_get_contents(); ob_end_clean(); ) return $text; )

add_filter ("widget_text" , "my_widget_execute_php" , 100 ) ;

function my_widget_execute_php($text) (

if (strpos($text, "

Ob_start();

Eval(" ?> ". $ text ) ;

$text = ob_get_contents();

ob_end_clean();

return $text;

A little clarification for those who want to understand how this code works.

Line 2: add_filter – attaches additional code to the widget_test function. Here we use a hook system. I mentioned this when. That is, now standard WordPress function widget_text will “hook” the code of our my_widget_execute_php function.

Line 3: Declaration of the function my_widget_execute_php. It takes one parameter $text which contains the text from the widget.

Line 5: If PHP code is detected, then enable output buffering using the ob_start() function. Now all information intended for output will be temporarily placed in a buffer.

Line 6: The eval function runs the PHP code contained in the received $text string.

The only thing is that we add ‘?>’ before the $text line so that the text that may be present before the php code is not perceived as code.

Error:

eval("Popular articles:");

The string “Popular articles” is perceived by the eval function as code to be executed.

Right:

eval("?> Popular articles:");

Now the line is located between the closing ‘?>’ and the opening ‘

Line 7: Get the contents of the buffer into the $text variable

Line 8: ob_end_clean() – clear the buffer and disable buffering

Line 10: Return the result of the function to the widget.

That's all! You can now insert any code into a WordPress widget!

For example, you can write (or find a ready-made) function that will return the top most commented posts. And then to display this list in the sidebar widget, simply write a call to your function in the text widget:

Write in the comments your ideas about what else can be displayed in the widget? It would be cool if the proposed option is not implemented in existing plugins and scripts. At least those that are known and widely distributed. If I like the idea, I will try to implement it. Thank you for participating!

Bye! See you again!

Original widgets!







2024 gtavrl.ru.