As we all know, WordPress is a very popular CMS. Its ease of use, user interface and powerful functions allow us to build various types of websites, which are characteristic of developers and programmers, but as a user, there is no difference.
As a PHP developer and blogger, I once wanted to execute a line of PHP code in an article. By default, you cannot execute PHP in WordPress articles or webpages (although you can do this in HTML, CSS, and JavaScript ).
Since WordPress does not parse PHP articles and single pages, I usually create a PHP file and link it to my article.
In the series composed of these two parts, we will look at how to execute PHP from WordPress articles and web pages.
In the first part of this series, I will explain the process and need to execute PHP on The Post page. In the second part of this series, we will demonstrate how the code can actually do this.
Working Principle
You know, include () or require () is used to introduce existing templates for arbitrary execution of PHP code.
Similarly, to execute PHP in WordPress articles and on a single page, we will create a folder under the WordPress installation directory that contains the PHP code file to be executed, then we will use theshortcodes plugin for articles or webpages that contain files. We will use this series of tutorials.
Simple code plug-in
This is not possible to use the include () or require () features including and post-execution PHP file types in WordPress because the code is not parsed in PHP, the default TinyMCE editor in WordPress is customized. However, with short code, we can do this.
Demonstrate how to do this. Let's create a plug-in.
1. Introduce the plug-in header
Plug-in header. This is a PHP comment block, which allows WordPress to recognize and read our plug-in.
<? Php
/*
Plugin Name: Run PHP code in Post & Page
Plugin URI: http://code.tutsplus.com
Description: Run PHP code in WordPress
Author: Agbonghama Collins
Version: 1.0
Author URI: http://tech4sky.com/
*/
Next, we call the theadd_encode code () function. This function registers our short code in the actual WordPress.
It accepts two parameters: short code tag and callback function.
Add_response code ('phpcode', 'php _ include ');
Finally, the simple code callback function.
Function php_include ($ attr ){
$ File = $ attr ['file'];
$ Upload_dir = wp_upload_dir ();
$ Folder = $ upload_dir ['basedir']. '/php-content'. "/$ file. php ";
Ob_start ();
Include ($ folder );
Return ob_get_clean ();
}
Looking at the short code function php_include, you can see that the file PHP content is in wp-content/uploads.
We can program to obtain the wordpress wp_upload_dir () auxiliary function.
The Merchant's syntax is [phpcode file = "dance"] Here the value attribute file is a php file that will include the files in the/wp-content/uploads/php-content folder.
Use the simple code plug-in to execute PHP
To demonstrate how to use the short code function, we will create a simple tool to change the writing mode and supply power to strrev.
The following is the WordPress article tool code.
<Form method = "post">
<Textarea name = "string">
<? Php echo (isset ($ _ POST ['string'])? $ _ POST ['string']: null;?>
</Textarea>
<Br/>
<Input type = "submit" name = "submit" value = "Reverse"/>
</Form>
<? Php if (isset ($ _ POST ['submit ']) & empty ($ _ POST ['string']) {
Echo "Field shoshould not be left empty ";
} Elseif (isset ($ _ POST ['submit ']) & ($ _ POST ['string']) {
Echo '<div> Copy Result </div> ';
Echo '<textarea> ';
Echo htmlspecialchars (strrev ($ _ POST ['string']);
Echo '</textarea> ';
}
Create a php file/wp-content/uploads/php-content and save the code snippet on it. Let's put the file wordsbackward. php.
Execute the php file and add the article or page with the short code [phpcode file = "wordsbackward.
After previewing in the browser, you should see the executed PHP code, as shown in the following figure.
Conclusion
It points out that if you run a multi-author blog, it is very important to give the author the privilege to execute PHP after the security risks, so you must ensure that there are in place security measures.
For many people, it is much more complicated to create a plug-in using a simple code and a separate file. In other words, add the code directly to the WordPress editor. In the next part of this series, we will create a more advanced plug-in that allows us to manage all the files in the WordPress dashboard.