繁体   English   中英

通过创建自定义插件在 wordpress 的页脚中添加自定义代码的确切术语

[英]Exact terminology for Adding a Custom code in the footer in wordpress by creating a custom plugin

现在我在 wordpress 网站的显示内容中使用了一个简码函数 add_shortcode。 现在我想通过不修改主题或子主题在页脚中注入代码,而是通过插件来完成。 我只是不知道在 google 中搜索的正确函数名称。 我总是使用 add_shortcode 因为它会显示在所有页面上。 它只是我的自定义弹出代码,但我必须将代码放在页脚中

我不确定你在问什么? 您需要知道如何实际编写插件吗? 如果是这种情况,那么有很多教程涵盖了这个主题。 一些与此相关的好资源:

  1. https://developer.wordpress.org/plugins/
  2. https://developer.wordpress.org/plugins/hooks/
  3. https://developer.wordpress.org/reference/hooks/wp_footer/

添加一件好事可能是检查执行此操作的现有插件的结构,例如:

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.

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