Plugins - Webasyst Documentation. Why PHP code doesn't work by default

CKEditor is a WYSIWYG editor, i.e. “what I see is what I get.” This editor can be used, for example, in WordPress (CKEditor for WordPress plugin), as well as in your own scripts. “At the input” you work in the editor as, for example, you would work in LibreOffice or MS Word, and “at the output "You get the HTML code. This is what the editor looks like in WordPress:

Initially, CKEditor comes in three packages, the difference between them is as follows:



Those. these packages contain different functionality (Basic and Standard - truncated functionality). But even the Full package can be supplemented with additional, very useful plugins, which is what I’ll talk about now. For your purposes, you can select the right set of plugins on a very convenient

Hidden from guests

(for WordPress, plugins can be added in the Site Control Panel, in the “Plugins” section, select “Add new”).

1. Base64 Image

Hidden from guests

Anyone who has created posts on a forum, blog, WordPress or a self-written CMS knows that pictures must first be uploaded to the server in order to be able to insert them into your post. If you write your own CMS and use CKEditor, then you You need to write scripts separately that will upload your images to the server. The Base64 Image plugin changes everything, now there is no need to do this - just add your images, which will be translated into base64 strings and saved among the HTML code. You can also add external links. to download file:

If you go to view the HTML code, you will see the following:

2. Spoiler

Hidden from guests

Allows you to add a spoiler that will hide the text.

3. Word Count & Char Count Plugin

Hidden from guests

This plugin counts words and characters that you have written in the editor. It can be useful when you need to keep within a certain number of characters, or when you need to write an article consisting of a given number of words.

4. Media (oEmbed) Plugin

Hidden from guests

To insert, for example, a video, you have to go into the source HTML code, look for the right place there and insert it there. This plugin allows you to significantly simplify the process of inserting videos, photos, audio and more. It supports an incredible number of different sites. Go to the office. site, you will be amazed at the variety of sources supported.

5. Code Snippet

Hidden from guests

Allows you to insert source code into your message, which will have syntax highlighting - a lot of highlighting options. Convenient and beautiful.

There are worthy alternatives for this plugin

Hidden from guests

,

Hidden from guests

.

6. Font Awesome

Hidden from guests

Contains a huge number of pictures (pictograms) that can be inserted into the text, and you can choose any color.

7.Insert Symbol

Hidden from guests

Allows you to insert special characters into text. Contains a very large number of special characters, divided into groups. Easy and convenient to use.

8.Keep TextSelection

Hidden from guests

The plugin that shocked me! Quite often there is a desire to look at or correct HTML code. What are we doing for this? We switch to source code mode and spend a long time looking for the place we need. This amazing plugin makes life a lot easier: in WYSIWYG mode we select the fragment we need, switch to source code mode and immediately get to the place we need - and the source code is also highlighted:

9. BBCode Output Format

Hidden from guests

How to simplify the formatting of messages on forums, on torrent trackers, on sites where there is no WYSIWYG mode, but only BBCode? There is a very elegant solution for this: thanks to the BBCode Output Format plugin, we can format our message in CKEditor and easily transfer the resulting BBCode to a forum or torrent tracker.

By the way, a popular website creates a significant load on hosting. It is important that your hoster ensures stable operation in all situations, so that the site does not suffer from power outages in the data center, frequent software changes, frequent DDoS attacks or simply from site loads, etc. After all, users will quickly get bored and go to a competing site. I went through quite a few solutions and found a very good option - 100 rubles per month! For this money you get professional hosting, with excellent uptime, with a free second-level domain as a gift (!), with 2 gigabytes of space on an SSD disk, with an unlimited number of databases, with the ability to connect an unlimited number of new domains (you only have to pay for each new domain - 139 rubles). In general, there are so many good things that the easiest way is to watch this

Hidden from guests

.

Look, I’m sure this offer will be of interest to any site administrator (be it a beginner or an advanced one), since this is hosting with real quality from professionals. By the way, I have

Hidden from guests

Giving a free month (in addition to the 2 weeks that are already given to everyone), if you want, you can take advantage.

Do you want to know, ? or ? All this and much more on the site. Subscribe to our e-mail newsletter (at the bottom of the page) or to and you will be the first to know about new articles! Also join our

Greetings, friends. Today we will figure out how to make any PHP code work in widgets, articles and on WordPress pages. By default, such a function is not available in this CMS and the maximum that a user can count on is the implementation of HTML code.

Why PHP code doesn't work by default

It would seem why the developers did not reward such a popular and convenient engine with useful capabilities for automatic execution of PHP code. At first I thought about this, but I came to the conclusion that this policy is being pursued from a security standpoint, because inept use of PHP in widgets or inside posts can lead to irreparable consequences - something will break in the database and the entire site will crash.

Therefore, working with PHP is left to programmers or people more or less advanced in this matter - any scripts are executed directly in the files.

For public sites (where there are several authors), running PHP in the body of an article increases the risk of deliberate sabotage, since any author can gain full access to the site through the article editing window.

According to the degree of danger, I would divide this whole situation into 3 levels:

  • Leaving everything as the developers intended is difficult to damage the site safely, accidentally or intentionally.
  • Allow PHP execution in widgets – medium level of danger, only the site administrator has access.
  • Using code everywhere is dangerous, since anyone who is allowed to edit articles and pages (moderators, authors) can manage the site.
  • Why do you need PHP in widgets?

    The question is individual, since anything can be implemented using this programming language. Personally, I was prompted to write this post by an order from a client whose website I am currently building. It was necessary to display in the sidebar in a separate widget a list of news from one category - “News”. Standard WordPress widgets do not have this feature.

    The question was between finding a plugin with the appropriate capabilities or using simple PHP code. The script for such a task is really small and I didn’t want to load the site with an extra plugin that wouldn’t be used anywhere else.

    In general, by allowing PHP to execute, we can solve 2 problems:

    • Replace some of the site’s plugins with scripts and in this way reduce the load on hosting;
    • Implement functions for which plugins do not yet exist.

    At the first stage, I will talk about widgets, and then, in a separate block, about the output of code in the content.

    PHP plugins in widgets

    The advantage of plugins: When changing the WordPress theme, the functions added to the template are not disrupted; everything continues to work without requiring the intervention of the webmaster. Hooks added to the template when changing the theme will have to be transferred to the new one manually.

    In my example above, I’m making a website for a client and if he wants to change the design on his own, then the settings made through functions.php files and others will disappear, so I make it as easy as possible for him to manage the site, relying on plugins (especially in its niche traffic is small and there will not be much load).

    PHP Code Widget

    I have been using this plugin in my work for a long time; it adds to the list of available widgets, similar to a regular text one, only capable of processing PHP in addition to text and HTML.

    PHP Code Widget is present in the official WordPress repository and can be easily found by name. How to install such plugins.

    No settings are required; the widget will appear in the list immediately after installing and activating the plugin. Drag “PHP Code” to the sidebar and add any script there.

    PHP in WordPress widget without plugin

    In this case, we will need a hook that will replace the standard function that allows the WordPress text widget to work with a function that allows PHP to be processed.

    This hook is added to the functions.php file of the current theme (at the very end before the closing bracket?>). After this, no new widget options appear, as is the case with the plugin; PHP codes begin to work in standard text.

    Ready code:

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

    The first line is a filter that replaces the standard text widget function with a new one - text_html_php_widget - each time it is called, and then there is a description of the new function itself.

    Why PHP code in WordPress articles and posts

    Inside posts, executable PHP code is required much less frequently than in widgets, so in the first part of the article I separately looked at the sidebar so that you can avoid connecting potentially dangerous functions unnecessarily.

    But even in posts there is a need to use scripts. The most common example is displaying a sitemap without a plugin. True, you can use another technique for the map - make a special page template (this is a separate topic).

    I once needed PHP output for the following purpose:

    I made a video website. Episodes of the series were displayed in playlists by season, and under each playlist of the season it was necessary to display a list of episodes with a link to your page. It looks like a site map, only more complicated - the conclusion had to be drawn from lists of individual sections. It was possible to manually enter each link using HTML code, but there are several hundred episodes and the procedure is tedious. Moreover, if a new series appeared, a link to it would have to be added manually - inconvenient. So I decided to use PHP functions for implementation.

    Plugin for PHP execution in Exec-PHP content

    Despite the fact that this plugin has not been updated for 7 years, it does its job perfectly. And I chose it for a reason - it does not use any shortcodes, like its competitors, but makes it possible to insert pure code into WordPress posts, starting with .

    The Exec-PHP plugin is in the repository and is installed through the menu in the engine admin panel.

    Of the settings, there is only one - permission/prohibition of code execution in a text widget, there is no option to disable work in posts and on pages, if you need to remove it, deactivate the plugin.

    To insert PHP code into an article, it must be switched to HTML mode (Text tab). The visual mode will most likely spoil the code.

    Executing PHP code in WordPress articles without a plugin

    By analogy with widgets, we can replace the plugin with a special function that allows the execution of PHP scripts in the content. Unfortunately, I was unable to find an option in which the code would work automatically as in the case of a plugin, reacting to tags. Requires the use of a shortcode. My knowledge of PHP was not enough to write the required function on my own, so I present it as it is. Working with a shortcode has a significant drawback that complicates the work, I will discuss it below.

    How to use the function below

  • We paste it into the theme’s functions.php file;
  • In the right place in the article we insert the construction - executable code without
  • Function:

    /* Running php in WordPress articles and pages: code */ function start_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)*?)\[\/startphp\]/", "start_php", $content); $content = preg_replace( "/\((.|\n)*?)\[\/startphp\]/", "$1", $content); return $content; ) add_filter("the_content", "inline_php");

    Flaw

    If the inserted PHP code contains HTML inserts or text, it will not work. Any text or tags will have to be inserted using the echo command, which is not always convenient. That is, the code must be purely PHP with 100 correct formats.

    Right

    Echo "This is how it will work";

    Wrong

    Echo "This line is correct"; It won't work like that

    There is no such problem in the Exec-PHP plugin - both text and HTML will be executed, but all elements of PHP code must be framed in the appropriate tags.

    For many webmasters, the topic described in this article opens up endless possibilities for creativity - you can implement something that could not be done before, and also get rid of a lot of plugins, replacing them with different filters, functions and hooks.

    Almost everyone who runs a blog on WordPress and maintains it themselves knows about the existence of a magical functions.php file. It is often used completely for other purposes, which can lead to significant problems. Let's look at this issue together in more detail.

    I assure you, after reading this article, you will change your attitude towards plugins and stop adding another code snippet to your functions.php.

    Plugins and functions.php

    Many WordPress site owners are firmly convinced that plugins will certainly load and slow down the blog. And if you just add the code to functions.php, this will not affect the load in any way. Alas, this is not entirely true...

    The fact is that the load is caused not specifically by the plugin, but by its incorrectly written code, which can easily end up in functions.php from the next manual.

    Let's look at the main differences between the plugin and functions.php.

    The fundamental difference between a plugin and the functions.php file is its purpose and execution order.

    It is also unfounded to believe that the code in functions.php will execute faster than in the plugin. The same code in the plugin and in functions.php will be executed with the same speed and load.

    Very often, problems with high load arise due to the additional functionality of plugins, and site loading speed suffers from scripts connecting their own styles, scripts and other content. Therefore, it is important to pay attention to the choice of plugin in order to avoid problems in the future.

    If you are an ordinary blogger and are far from web development, then when choosing a plugin, be sure to pay attention to the reviews in the WordPress repository and on the blogs of authors or web developers.

    If you still have doubts, make a cup of coffee and be sure to read the article by Konstantin Kovshenin on WP Magazine - “The whole truth about functions.php”. The first part simply and clearly explains why you should not believe in myths about functions.php.

    Plugin as an alternative to the functions.php file

    All additions that you make to the template files (scripts, counters, functions) may be lost when you change the active theme or apply updates to it. To avoid this and not create chaos in functions.php, I suggest you use your own personal plugin.

    Let's create our own plugin - an alternative to the functions.php file. Don’t be scared, it will look exactly like your favorite functions.php :) All you need to do is simply add an empty plugin to your site. And then you can insert the necessary code into it, as you used to do with functions.php.

    First of all, we need to create a file on our computer called functionsphp.php and add the following code to it:

    // ...Instead of this line, insert the counter code...