redandblue / wordpress-tools

一套辅助WordPress开发的工具包。

安装: 20

依赖者: 0

建议者: 0

安全性: 0

星标: 2

关注者: 4

分支: 1

开放问题: 5

类型:wordpress-muplugin

dev-master 2022-06-02 09:43 UTC

README

一套减少WordPress开发痛苦的工具包。存在文档,可能会随着时间的推移而改进。

它是模块化的,因此您可以禁用不需要的部分。

要求

PHP 7。

composer.json 说 5.6,但这只是临时的,所以Composer在安装标准macOS PHP(5.6)时不会抱怨,这是不合理的,因为升级非常困难。

有趣的部分

有很多事情在进行中,有些事情并不像其他事情那么有用。它给模板设计带来了新的气息。

首先,我们“注册”一个模板目录,将其添加到主题的 functions.php 中

\rnb\template\load_glob(dirname(__FILE__) . '/templates/*');

然后,来自 templates/ 的所有PHP文件都会通过 \rnb\template\output()\rnb\template\to_string() 加载和可用。如果您想的话,可以拥有多个模板目录。

模板与“传统”模板略有不同:所有模板都是接受数据作为参数的函数,而传统的模板通常只是直接调用一个函数。例如,位于 templates/single-item.php 的模板

function single_item($props = []) {
  // We can also let the template populate itself with data if required.
  $content = $props['content'] ?? get_the_content();
  $image = $props['image'] ?? \rnb\media\image(get_post_thumbnail_id());
  $injectorfn = !is_null($data['injectorfn']) ? $data['injectorfn'] : function() {
    return '<!-- You can put just about anything here -->';
  }; ?>

  <article>
    <?=$image?>
    <p><?=$content?></p>
    <?=$injectorfn()?> <!-- You could make the function echo data instead, choice is yours. -->
  </article>
}

然后您可以在The Loop中使用该模板,或者以任何其他方式提供数据。

while (have_posts()) { the_post();
  \rnb\template\output('single_item');
}

// or

\rnb\template\output('single_item', [ // notice array inside array
  [
    'content' => 'Hello world!',
    'image' => '<img src="/path/to/image.jpg">',
    'injectorfn' => function() {
      $link = get_permalink();
      return \rnb\core\tag([
        "<a href='$link'>",
        "Read the whole thing",
        "</a>"
      ]);
    }
  ]
]);

这一切都是在GitHub编辑器中编写的,实际上并没有测试代码,所以如果它能正常工作,那就太好了!如果它不能正常工作,请修复它并提交PR。