繁体   English   中英

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

[英]How to create a simple custom shortcode in Wordpress?

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

/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]。

我想我需要以某种方式将页面类型链接到插件或类似的东西。 你能帮我吗?

在/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
   <?php
/*
* Plugin Name: Custom ShortCodes
* Description: Create your WordPress plugin shortcodes.
* Version: 1.0
* Author: fatir031624@gmail.com
*/

function wpb_test_shortcode() { 
$message = 'Hello world!'; 
return $message;
} 
add_shortcode('testing', 'wpb_test_shortcode');
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM