layered/wp-helpers

简化WordPress开发的辅助工具

v1.10.0 2019-10-24 15:32 UTC

README

WP Helpers 是一组用于简化WordPress开发的类和库。

功能和用例

  • CustomPostType - 注册新的文章类型。它可以填充或生成值,文章类型可以一行代码添加,例如:CustomPostType::add('idea');
  • MetaFields - 简单地添加元字段到文章、术语、评论和用户,例如:mf()->addPostMeta('number-field', ['name' => 'Number Field', type' => 'number'])
  • Q - 在不阻塞当前请求的情况下在后台处理WP操作。用法:queue_action('resources_hungry_action', 'data', 4, 'action')
  • PostMetaBox - 为文章类型创建美观且功能齐全的元框

如何使用

安装

在您的 composer.json 文件中将 layered/wp-helpers 添加为 require 依赖

$ composer require layered/wp-helpers

注册文章类型

添加文章类型的示例,创建一个名称为第一个参数的 CustomPostType 实例

CustomPostType::add('idea', [
	'labels'	=>	[
		'name'	=>	__('Ideas', 'my-theme-or-plugin')
	],
	'rewrite'	=>	[
		'slug'	=>	'my-ideas'
	],
	'supports'	=>	['title', 'editor', 'thumbnail', 'excerpt', 'author']
])
	->addTaxonomy('tag')
	->addColumns(['author'])
;

元字段

元字段是可以轻松添加到文章、术语、评论和用户的自定义字段。它们使用默认的WordPress流程进行注册,在列表视图中显示为列,在编辑页面、快速/批量编辑屏幕以及REST API中显示为可编辑字段

MetaFields::instance()->addPostMeta('second-heading', [
	'name'				=>	'Second Heading',
	'type'				=>	'text',
	'placeholder'		=>	'Heading for article',
	'show_in_rest'		=>	true,
	'showInMetaBox'		=>	true,
	'showInColumns'		=>	true,
	'showInQuickEdit'	=>	true,
	'showInBulkEdit'	=>	true
]);

Q

Q 为插件和主题添加了对异步操作的支持。简单使用,只需包含文件,将 do_action() 替换为 queue_action()。处理在后台进行,使Web请求更快。示例

// keeps request hanging until action is complete
do_action('resource_hungry_action', 'data', 4, 'action');

// queues the action to be handled in background
queue_action('resource_hungry_action', 'data', 4, 'action');

更多

请在此处GitHub上报告任何问题。

欢迎任何贡献