如何在Wordpress中创建一个简单的自定义短代码?

时间:2017-03-07 10:18:56

标签: php wordpress

我在/wp-content/plugins目录中创建了一个名为customshortcode的文件夹。 add_shortcode command应该将短代码与函数链接起来。

/wp-content/plugins/customshorcode/我创建了2个文件:main.phptest.php

主要php的内容是:

<?php require_once('../../../wp-includes/shortcodes.php'); ?>

<?php function custom_shortcode_func() {
  include_once('test.php');
}

add_shortcode('customshortcode','custom_shortcode_func');

?>

test.php只显示一个分区块:

<style>.testdiv{border:1px solid black; width:300px; heigh:300px;}</style>

<div class="testdiv"></div>

我首先运行/wp-content/plugins/customshorcode/main.php文件,看不出错误和简单的白屏。我假设应该保存短代码。

但是当我在页面中输入[customshortcode]时,我不会显示分区,而是显示文本[customshortcode]。

我认为我需要将页面类型链接到插件或类似的东西。你能救我一下吗?

1 个答案:

答案 0 :(得分:1)

在/wp-content/plugins/customshorcode/main.php

<?php
/*
* Plugin Name: Custom Short Code
* Description: Create your WordPress shortcode.
* Version: 1.0
* Author: samuelj90@gmail.com 
*/

// Example 1 : WP Shortcode to display form on any page or post.
function custom_shortcode_func() {
?>
<style>.testdiv{border:1px solid black; width:300px; heigh:300px;}</style>

<div class="testdiv"></div>
<?php
}
add_shortcode('customshortcode','custom_shortcode_func');

?>

然后启用插件

Custom Short Code