mcguffin/acf-wp-objects

整合WordPress和ACF

安装: 19

依赖: 0

建议: 0

安全: 0

星标: 21

关注者: 6

分支: 3

开放问题: 4

类型:wordpress-plugin


README

填补WordPress和ACF之间的空白。

功能

将WordPress对象整合到ACF中。

  • 使用ACF字段编辑WP-Post属性
    • 设置:blogname
    • 设置:blogdescription
    • 帖子:post_title
    • 帖子:post_excerpt
    • 帖子:post_content
    • 帖子:post_thumbnail
    • 帖子:attachments
    • 术语:term_name
    • 术语:term_description
    • 主题-模:custom_logo
  • 新的 字段类型
    • 帖子类型
    • 分类法
    • 图片大小
    • Includer(包含来自不同字段组的所有字段)
    • 用户角色
    • Sweet Spot(与媒体一起使用)
    • 插件模板选择(从插件中选择模板文件)
  • 新的 位置规则
    • 帖子类型/分类法是公开的/内置的/显示UI/显示在菜单中/显示在导航菜单中
    • 帖子类型支持
    • 编辑器是经典/块编辑器
    • WP选项页面是通用/写作/阅读/讨论/媒体/永久链接
    • 插件模板设置
    • 到处
  • 选择字段:从重复器中获取选择,例如从ACF选项页面
  • 多站点:
    • 使用 acf_add_options_(sub_)page() 添加网络管理员页面。将 "network" => true, 添加到页面参数中(ACF Pro仅限 – 两个插件都必须启用网络功能)
  • 样式:
    • 在块编辑器侧边栏中提供更紧凑的样式
    • 将类 no-headno-sort 添加到重复器中
    • 将类 seamlessno-label 添加到字段中
    • 将类 button-group 添加到复选框中(使它们看起来像按钮组)
  • 页面布局:提供位置规则的通用灵活内容字段。使用 acf_add_page_layout('foobar') 初始化,使用 acf_page_layouts('foobar') 渲染
  • JSON-Paths:在自定义位置(如插件)中保存字段组JSON
  • 本地化:通过po文件本地化ACF字段标签
  • ACF选项页面:可选地,在后台和从WPCLI中进行重置、导出和导入。
  • 标签字段通过URL打开标签。只需追加 #Tab Label

安装

生产(使用Github Updater – 建议用于多站点)

  • 转到 版本发布
  • 下载 'acf-wp-objects.zip'
  • 上传并激活它,就像其他任何WordPress插件一样
  • 只要插件处于活动状态,就会运行自动更新

开发

  • 进入您的插件目录
  • $ git clone git@github.com:mcguffin/acf-wp-objects.git
  • $ cd acf-wp-objects
  • $ npm install
  • $ npm run dev

JSON-Paths

考虑以下场景:您正在使用主题中的本地json字段组。您想在子主题中覆盖它们。或者,您有一个具有ACF依赖的插件,包含作为本地json的字段组。

这将从主题和子主题目录中的子目录 path/to/json-files 加载并保存ACF JSON,但仅当字段组键为 group_my_fieldgroup_key 时。

acf_register_local_json(
	'path/to/json-files', // e.g. 'acf-json' in a theme
	function( $field_group ) { 
		// callback 
		// return true, if the field group JSON
		// should be saved at the given location
		return $field_group['key'] === 'group_my_fieldgroup_key';
	},
	[ // parent paths to check
		get_template_directory(),
		get_stylesheet_directory(),
	]
);

Sweet Spot 字段

为具有 object-fit: cover 的图像选择合适的裁剪方式的一种简单方法。

要为所有图像启用“甜点”功能,以下是一段 PHP 代码,位于您的主题的 functions.php 或 mu-plugin 中。

add_filter( 'acf_image_sweetspot_enable', '__return_true' );

插件将注册一个包含甜点字段和在前端为图像属性添加内联样式规则的图像附件字段组,例如 object-position: 12.3% 81.4%;

Sweet Spot Field UI

JSON I18n

ACF 提供对 WPML 的支持以本地化字段组。ACF WP Objects 通过 .po 文件提供不同的方法。

acf_localize_field_groups( 
	'my-textdomain', 
	function( $field_group ) { 
		// callback which should return true, if the field group 
		// localization is available under the given textdomain
		return $field_group['key'] === 'group_my_fieldgroup_key';
	});

如果您使用本地 JSON,以下是一个节点脚本,允许您提取字符串 src/run/json-i18n.js 并将它们添加到 pot 文件中

安装 WP CLI

src/run/json-i18n.jssrc/run/lib/json-extract.js 放在您的包目录中。

从 JSON 文件中提取字符串并将它们添加到 PHP 文件中

node ./src/run/json-i18n.js 'my-texdomain' ./path/to/json ./php-output.php

使用 WP CLI 生成 pot

wp i18n make-pot . languages/my-textdomain.pot --domain=my-textdomain

模板文件(仅限 ACF Pro)

  1. 过滤模板类型
add_filter('acf_wp_objects_template_types', function( $types ) {
	$slug = 'foo-plugin';
	$key = 'Items Template';
	$theme_location = 'foo-plugin';
		// will point to wp-content/themes/<current-theme>/foo-plugin/
		// default: $slug
	$plugin_location = 'templates';
		// will point to wp-content/plugins/foo-plugin/templates/
		// null: use default, false: no plugin location, string: custom location inside plugin

	$types[ $slug ] = [
		'header_key' => $key,
		'theme_location' => $theme_location,
		'plugin_location' => $plugin_location,
	];
	return $types;
});

WP Objects 将在主题和插件位置中搜索具有标题键的模板文件。

  1. 创建一个名为 my_fabulous_template 的模板选择字段。使用方式如下: get_template_part( get_field('my_fabulous_template') );
  2. 将一些模板文件放在位置
/*
Items Template: List
*/
$settings = get_field('my_fabulous_template_settings');

页面布局(仅限 ACF Pro)

生成一个灵活的内容字段并将字段组转换为布局。如果您需要可扩展的布局集来选择,则此功能非常理想。

  1. 添加一个布局部分
acf_add_page_layout([
	'title'	=> 'My Layout',
	'name'	=> 'my-layout',
]);
  1. 创建字段组。将“页面布局”设置为“我的布局”作为位置,并在底部输入行布局别名。
  2. 在您的主题中创建与上述别名对应的模板文件。文件名应匹配 acf/layout-<row_layout_slug>.php。别忘了使用 get_sub_field(),您正在灵活内容字段内部!
  3. 在您的页面.php 模板文件中,在循环内部调用此代码
acf_page_layouts( 'my-layouts' );

重复选择与重复行返回

ACF 默认不加载前端字段组。要在前端检索重复选择字段的值,ACF 必须知道这一点。

您可以通过添加 do_action( 'acf_wpo_load_fields' ); 来实现这一点

ACF 选项页面

acf_add_options_page()acf_add_options_sub_page() 提供了一些新选项。

acf_add_options_page([
	'import' => false,
	'import_message' => __( 'Options Imported', 'acf-wp-objects' ),
	'import_error_message' => __( 'Invalid Import Data', 'acf-wp-objects' ),
	'import_button' => __( 'Import', 'acf-wp-objects' ),
	'import_select_file' => __( 'Select File…', 'acf-wp-objects' ),

	'export' => false,
	'export_references' => false,
	'export_button' => __( 'Export Settings', 'acf-wp-objects' ),

	'reset' => false,
	'reset_button' => __( 'Restore defaults', 'acf-wp-objects' ),
	'reset_message' => __( 'Options Reset to Defaults', 'acf-wp-objects' ),
]);

参数

  • import Boolean 启用导入功能
  • import_message String 成功导入后的消息
  • import_error_message String 导入失败后的消息
  • import_button String 导入按钮标签
  • import_select_file String 导入文件输入标签
  • export Boolean|Array 启用导出功能。 true 将简单地从当前选项页导出值。传递一个选项页 ID 数组将一次性从多个选项页导出
  • export_references Boolean 是否导出引用的内容,如帖子、图像或术语
  • export_button String 导出按钮标签
  • reset Boolen|String 启用重置功能。传递有效导入文件的路径将导入它。
  • reset_message String 成功重置后的消息
  • reset_button String 重置按钮标签

示例

启用导入和导出。

acf_add_options_page([
	'page_title' => 'Configure Foobar Options',
	'menu_title' => 'Foobar Options',
	'post_id' => 'foobar_options',
	'parent_slug' => 'themes.php',
	'menu_slug'	=> 'foobar-options',
	'import' => true,
	'export' => true,
]);

同时启用重置。

acf_add_options_page([
	'page_title' => 'Configure Foobar Options',
	'menu_title' => 'Foobar Options',
	'post_id' => 'foobar_options',
	'parent_slug' => 'themes.php',
	'menu_slug'	=> 'foobar-options',
	'import' => true,
	'export' => true,
	'reset' => true,
]);

启用重置并从导出文件获取值。

acf_add_options_page([
	'page_title' => 'Configure Foobar Options',
	'menu_title' => 'Foobar Options',
	'post_id' => 'foobar_options',
	'parent_slug' => 'themes.php',
	'menu_slug'	=> 'foobar-options',
	'import' => true,
	'export' => true,
	// pass file path to reset
	'reset' => get_template_directory().'/foobar-defaults.json',
]);

WP-CLI

创建一个导出文件

wp acf-options-page export foobar-options --pretty > wp-content/themes/my-theme/foobar-defaults.json

从文件导入选项

wp acf-options-page import wp-content/themes/my-theme/foobar-defaults.json

重置选项页面。
无论传递给 acf_add_options_page() 的配置如何,此操作都不会从文件中导入值。使用 wp acf-options-page import 来执行此操作。

wp acf-options-page reset foobar-options