简体   繁体   English

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

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

I created a folder in the /wp-content/plugins directory called customshortcode.我在/wp-content/plugins目录中创建了一个名为 customshortcode 的文件夹。 The add_shortcode command should link the shortcode with the function.add_shortcode 命令应该将短代码与函数链接起来。

In /wp-content/plugins/customshorcode/ I created 2 files: main.php and test.php/wp-content/plugins/customshorcode/我创建了 2 个文件: main.phptest.php

Contents of main php is:主要php的内容是:

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

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

add_shortcode('customshortcode','custom_shortcode_func');

?>

And test.php simply displays a division block: test.php 只显示一个除法块:

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

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

I first run the /wp-content/plugins/customshorcode/main.php file to see no errors and simple white screen.我首先运行/wp-content/plugins/customshorcode/main.php文件以查看没有错误和简单的白屏。 I assumed the short-code should be saved.我认为应该保存短代码。

But when I enter [customshortcode] in the page, I don't get the division displayed, instead the text [customshortcode] instead.但是当我在页面中输入 [customshortcode] 时,我没有显示分区,而是显示文本 [customshortcode]。

I think I somehow need to link the page type to the plugin or something like that.我想我需要以某种方式将页面类型链接到插件或类似的东西。 Could you help me out?你能帮我吗?

In /wp-content/plugins/customshorcode/main.php 在/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');

?>

Then enable the plugin 然后启用插件

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