动漫风景图
教程

如何引用 设置-模板-管理模块

作者头像
4 19


Leantasy 模块系统支持高度灵活的扩展开发方式,你可以为每一个模块配置自己的功能逻辑、管理界面、资源文件和文档说明。

目前在设置页面方面,系统支持以下两种挂载方式,适配不同开发者的需求和项目复杂度:

✅ 方式一:挂载到主题本体的设置页面(推荐)

你可以将扩展模块的设置表单直接挂载到 Leantasy 主题的全局设置界面中,例如“扩展模块”栏目下。这种方式便于统一管理,界面一致,适合轻量级模块或希望与主题配置融合的功能模块。

实现方式:

  • 在模块 Main.php 中实现 Extend_Settings​ 方法;
  • 使用 模块入口文件​ 将模块配置项追加到主题设置中;
  • 系统将在加载主题设置界面时自动调用并挂载。

设置文件:
此文件需要和 入口结构文件 同一目录下

设置文件
<?php
namespace LF\Extend\LF_Example\Modules\Settings;
class Example
{
  //本体主题设置的sulg
  public static $Module = 'Leantasy';
  //模块初始化入口方法
  public function init()
  {
    $this->Example_Options_Modules();
  }
  //创建菜单设置
  public function Example_Options_Modules()
  {
    \CSF::createSection(self::$Module, array(
      'title'  => '用户模块',
      'icon' => 'icon-yonghu',
      'id'  => 'User',
      'fields'      => array()
    ));
  }
}
UTF-8柠萌主题PHP

入口结构文件【Modules​下每个文件的Main.php文件】:
四个文件的结构相差无疑,请依据当已展示的代码来前适配其他的入口文件

Main.php
//////////////////////////////////////////////////Settings
//命名空间
namespace LF\Extend\LF_Example\Modules\Settings;
//要引用的设置文件
use LF\Extend\LF_Example\Modules\Settings\Example;
class Main
{
  public function Extend_Settings()
  {
    add_action('Extend_Settings', function () {
      //需要创建实列的后台设置
      $Example = new Example;
      $Example->init();
    });
  }
}
UTF-8柠萌主题PHP

挂载 入口结构文件 【 模块入口文件,由 module.json 中的 "entry" 字段指定​】:

module.json
<?php
// 注意:一定要正确命名空间,和 module.json 配对!
namespace LF\Extend\LF_Example;
use LF\Extend\LF_User\Modules\Settings\Main as SettingsLoader;
// 主模块类
class Example
{
  private static ?self $instance = null;
  private function __construct()
  {
    //使用主题自带的钩子挂载
    $settings = new SettingsLoader;
    $settings->Extend_Settings();
  }
  //必须存在否则引用不到此扩展
  public static function instance(): self
  {
    if (self::$instance === null) {
      self::$instance = new self();
      self::$instance->init();
    }
    return self::$instance;
  }
  // 激活时执行
  public static function activate() {}
  // 停用时执行
  public static function deactivate() {}
  // 卸载时执行
  public static function uninstall() {}
  // 更新时执行
  public static function update() {}
}
UTF-8柠萌主题JSON

优点:

  • UI 一致,无需创建新页面;
  • 插件化体验更自然;
  • 设置项可直接在主题配置中被统一保存与读取。

✅ 方式二:独立创建一个专属设置页面

对于功能复杂、配置项较多或需要独立管理权限的模块,也支持创建完全独立的设置页面,挂载在 WordPress 后台的侧边栏中,或作为子菜单添加至主题菜单下。

示例入口结构【Modules​下每个文件的Main.php文件】:

Main.php
//////////////////////////////////////////////////Settings
namespace LF\Extend\LF_Example\Modules\Settings;
use LF\Extend\LF_Example\Modules\Settings\Example;
class Main
{
  public function Extend_Settings()
  {
    add_action('Extend_Settings', function () {
      //需要创建实列的后台设置
      $Example = new Example;
      $Example->init();
    });
  }
}
UTF-8柠萌主题PHP

挂载 示例入口结构 【 模块入口文件,由 module.json 中的 "entry" 字段指定​】:

module.json
<?php
// 注意:一定要正确命名空间,和 module.json 配对!
namespace LF\Extend\LF_Example;
use LF\Extend\LF_User\Modules\Settings\Main as SettingsLoader;
// 主模块类
class Example
{
  private static ?self $instance = null;
  private function __construct()
  {
    //使用主题自带的钩子挂载
    $settings = new SettingsLoader;
    $settings->Extend_Settings();
  }
  //必须存在否则引用不到此扩展
  public static function instance(): self
  {
    if (self::$instance === null) {
      self::$instance = new self();
      self::$instance->init();
    }
    return self::$instance;
  }
  // 激活时执行
  public static function activate() {}
  // 停用时执行
  public static function deactivate() {}
  // 卸载时执行
  public static function uninstall() {}
  // 更新时执行
  public static function update() {}
}
UTF-8柠萌主题JSON

优点:

  • 配置界面更灵活,适合复杂模块;
  • 可独立控制权限;
  • 支持定制化样式和交互逻辑(如分步设置、表格操作等)。

🧠 使用建议

场景推荐挂载方式
小型工具型模块挂载到主题设置页
与主题功能强耦合的模块挂载到主题设置页
复杂系统模块(如会员系统)独立设置页面
需要单独权限控制的模块独立设置页面
min.php — limoe-studio
<?php

namespace LF\Extend\LF_Write\Modules\Settings;

class Main
{
  public static $Module = 'Leantasy';
  public function init()
  {
    $this->Extend_Settings();

    add_action('Loader_Admin_Setup', [$this, 'setup_admin_scripts']);
  }

  public function Extend_Settings()
  {
    add_action('Extend_Settings', function () {

      \CSF::createSection(self::$Module, array(
        'title'  => '编辑器模块',
        'icon' => '',
        'id'  => 'Write',
      ));

      $Admin = new Admin();
      $Admin->init();
    }, 3);
  }

  public function setup_admin_scripts()
  {

    $allowed_post_types = ['post', 'page', 'product'];

    $screen = get_current_screen();
    if (!$screen || $screen->base !== 'post') {
      return;
    }

    if (!in_array($screen->post_type, $allowed_post_types, true)) {
      return;
    }


    wp_enqueue_script('editor_meta', LF_THEME_URI . '/Extend/LF_Write/Assets/admin/meta_box.js', array(), LF_VERSION, true,);
    wp_enqueue_style('editor_meta', LF_THEME_URI . '/Extend/LF_Write/Assets/admin/meta_box.css', array(), LF_VERSION, 'all');

    wp_enqueue_script('enlighter', LF_THEME_URI . '/Extend/LF_Write/Assets/fontend/library/enlighter/enlighter.min.js', array(), LF_VERSION, true,);
    wp_enqueue_style('enlighter', LF_THEME_URI . '/Extend/LF_Write/Assets/fontend/library/enlighter/enlighter.min.css', array(), LF_VERSION, 'all');
  }
}
UTF-8柠萌主题PHP

本文为 柠萌社 原创文章,转载请注明出处:

文章标题:如何引用 设置-模板-管理模块

文章作者:我不想写代码啊

文章链接:https://www.limoe-studio.com/79.html

版权声明:版权所有 © 2025 柠萌社 保留所有权利。

未分类

Lentasy:专属于二次元爱好者的 WordPress 主题

2025年6月29日 上一篇

评论

点击我,就可以参与评论了

你对这个文章的意见是什么

@{{reply_to_author}}表情图片Markdown 评论
  • 暂时还没有评论哦
  • 1