[英]Exact terminology for Adding a Custom code in the footer in wordpress by creating a custom plugin
現在我在 wordpress 網站的顯示內容中使用了一個簡碼函數 add_shortcode。 現在我想通過不修改主題或子主題在頁腳中注入代碼,而是通過插件來完成。 我只是不知道在 google 中搜索的正確函數名稱。 我總是使用 add_shortcode 因為它會顯示在所有頁面上。 它只是我的自定義彈出代碼,但我必須將代碼放在頁腳中
我不確定你在問什么? 您需要知道如何實際編寫插件嗎? 如果是這種情況,那么有很多教程涵蓋了這個主題。 一些與此相關的好資源:
添加一件好事可能是檢查執行此操作的現有插件的結構,例如:
https://wordpress.org/plugins/insert-headers-and-footers/#developers
在每個頁面的頁腳中添加一些東西,你會做這樣的事情,這個文件應該被添加到wp-content/plugins
目錄下:
<?php
/**
* Plugin Name: Footer plugin
* Description: Adds text to footer
* Version: 1.0.0
* Requires at least: 3.0
* Tested up to: 6.0
* Author: Author
* Text Domain: footer-plugin
**/
class FooterPlugin
{
public function __construct()
{
// hook into wp_footer
add_action('wp_footer', [$this, 'addFooter']);
}
public function addFooter()
{
echo '<span>footer text</span>';
}
}
// initialize the plugin when everything is loaded
add_action('plugins_loaded', function () {
new FooterPlugin();
});
另一種方法是從生成的頁面輸出中替換頁腳的內容:
class FooterPlugin
{
/** @var string */
private $replacement = 'SOME TEXT';
public function __construct()
{
add_action('template_redirect', [$this, 'templateRedirect']);
}
/**
* @var string $content
**/
public function templateRedirect(string $content)
{
ob_start([$this, 'handleBuffer']);
}
/**
* Replace everything in the footer tag
* @param string $buffer
* @return string
**/
public function handleBuffer(string $buffer): string
{
return preg_replace('/(\<footer\s*.*\>)((.|\n)*)(\<\/footer\>)/', '$1' . $this->replacement . '$3', $buffer);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.