簡體   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