douliang6563
2017-03-07 10:18
浏览 25
已采纳

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

I created a folder in the /wp-content/plugins directory called customshortcode. The add_shortcode command should link the shortcode with the function.

In /wp-content/plugins/customshorcode/ I created 2 files: main.php and test.php

Contents of main php is:

<?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:

<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. 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.

I think I somehow need to link the page type to the plugin or something like that. Could you help me out?

图片转代码服务由CSDN问答提供 功能建议

我在 / wp-content / plugins 目录中创建了一个名为customshortcode的文件夹。 add_shortcode命令应将短代码与功能链接起来。 < p>在 / wp-content / plugins / customshorcode / 中创建了2个文件: main.php test.php \ n

主要php的内容是:

 &lt;?php require_once('../../../ wp-includes / shortcodes.php'  );  ?&gt; 
 
&lt;?php function custom_shortcode_func(){
 include_once('test.php'); 
} 
 
add_shortcode('customshortcode','custom_shortcode_func'); 
 
?n?&gt;  
   
 
 

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

 &lt; style&gt; .testdiv {border:  1px纯黑色; 宽度:300像素; 高度:300px;}&lt; / style&gt; 
 
&lt; div class =“testdiv”&gt;&lt; / div&gt; 
   
 
 

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

但是当我在页面中输入[customshortcode]时,我没有显示分区,而是显示文本[customshortcode]。 / p>

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyu1918 2017-03-07 10:45
    已采纳

    In /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
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题