wordpress插件开发_如何为WordPress开发PHP文件包含插件

wordpress插件开发

WordPress can save developers weeks of effort. It’s difficult to justify writing your own CMS when it offers so many features. That said, there are times when you want to inject your own PHP code; perhaps a custom form or a report generated by another system.

WordPress可以节省开发人员数周的工作量。 当提供许多功能时,很难证明编写自己的CMS是合理的。 就是说,有时候您想注入自己PHP代码。 也许是另一个系统生成的自定义表格或报告。

To insert your code on every WordPress page or post, you can simply add a PHP include() function to page.php or single.php accordingly, e.g.

要在每个WordPress页面或帖子上插入代码,您只需将PHP include()函数添加到page.php或single.php中即可,例如

include('mycode.php');

Assuming mycode.php resides in your theme folder (wp-content/themes/theme-name), it will be included and executed at that point.

假设mycode.php位于您的主题文件夹(wp-content / themes / theme-name )中,那么它将包括在内并在该位置执行。

But what if you need to include a file on a specific post/page or within the middle of a content block? We need a WordPress plugin…

但是,如果您需要在特定帖子/页面上或内容块中间添加文件,该怎么办? 我们需要一个WordPress插件…

创建插件 (Creating the Plugin)

Create a new file named php-include.php in your plugins folder (wp-content/plugins) and add a header so it can be identified by WordPress:

在您的plugins文件夹(wp-content / plugins)中创建一个名为php-include.php的新文件,并添加标题,以便WordPress可以识别它:

<?php
/*
Plugin Name: PHP File Includer
Plugin URI: https://www.sitepoint.com/
Description: Include PHP files using a shortcode
Version: 1.0
Author: Craig Buckler
Author URI: http://optimalworks.net/
License: Use this how you like!
*/

This is followed by our primary function, PHP_Include(). It expects an array of parameters although we’re just using one — file. If it’s not passed, file set to ‘default’:

接下来是我们的主要功能PHP_Include()。 尽管我们只使用一个文件,但它期望有一组参数。 如果未通过,则将文件设置为“默认”:

// include PHP file
function PHP_Include($params = array()) {

	extract(shortcode_atts(array(
	    'file' => 'default'
	), $params));
	
	ob_start();
	include(get_theme_root() . '/' . get_template() . "/$file.php");
	return ob_get_clean();
}

The function assumes you’re requesting a file which resides in the theme folder. The last three lines include the file and return its executed contents using PHP’s output buffering functions.

该函数假定您正在请求驻留在主题文件夹中的文件。 最后三行包括文件,并使用PHP的输出缓冲函数返回其执行的内容。

warning: With great power… 警告:威力强大…

…comes great responsibility. This plugin may be small but it allows anyone to execute arbitrary code. That’s not be a problem if you’re the sole editor of your own blog, but you should be wary of other users.

……承担着巨大的责任。 这个插件可能很小,但它允许任何人执行任意代码。 如果您是自己博客的唯一编辑者,那不是问题,但是您应该警惕其他用户。

Optionally, you could modify the include statement’s location or only permit files with a specific name pattern, e.g. “/include/mycode-$file.php”. This should safeguard against users including any PHP file.

(可选)您可以修改include语句的位置,或仅允许使用特定名称模式的文件,例如“ /include/mycode-$file.php”。 这应该防止包括任何PHP文件的用户。

Finally, we’ll register our function as a shortcode handler:

最后,我们将函数注册为简码处理程序:

// register shortcode
add_shortcode('phpinclude', 'PHP_Include');

Save the file and activate the plugin in your WordPress control panel.

保存文件并在WordPress控制面板中激活插件。

包括一个PHP文件 (Including a PHP File)

The following shortcode can now be added to any page or post:

现在可以将以下短代码添加到任何页面或帖子中:

[phpinclude file='mycode']

Assuming mycode.php exists in your theme’s folder, it will be inserted at that point in the content.

假设主题文件夹中存在mycode.php,它将在该位置插入内容中。

I hope you find it useful.

希望对你有帮助。

翻译自: https://www.sitepoint.com/how-to-develop-a-php-file-include-plugin-for-wordpress/

wordpress插件开发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值