在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):wp-graphql/wp-graphql-acf开源软件地址(OpenSource Url):https://github.com/wp-graphql/wp-graphql-acf开源编程语言(OpenSource Language):PHP 85.1%开源软件介绍(OpenSource Introduction):WPGraphQL for Advanced Custom FieldsWPGraphQL for Advanced Custom Fields automatically exposes your ACF fields to the WPGraphQL Schema.
Install and ActivateWPGraphQL for Advanced Custom Fields is not currently available on the WordPress.org repository, so you must download it from Github, or Composer. Installing From GithubTo install the plugin from Github, you can download the latest release zip file, upload the Zip file to your WordPress install, and activate the plugin. Click here to learn more about installing WordPress plugins from a Zip file. Installing from Composer
DependenciesIn order to use WPGraphQL for Advanced Custom Fields, you must have WPGraphQL and Advanced Custom Fields (free or pro) installed and activated. Adding Fields to the WPGraphQL SchemaTL;DR: Here's a video showing an overview of usage. Advanced Custom Fields, or ACF for short, enables users to add Field Groups, either using a Graphical User Interface, PHP code, or local JSON to various screens in the WordPress dashboard, such as (but not limited to) the Edit Post, Edit User and Edit Term screens. Whatever method you use to register ACF fields to your WordPress site should work with WPGraphQL for Advanced Custom Fields. For the sake of simplicity, the documentation below will primarily use the Graphic User Interface for examples. Add ACF Fields to the WPGraphQL SchemaThe first step in using Advanced Custom Fields with WPGraphQL is to create an ACF Field Group. By default, field groups are not exposed to WPGraphQL. You must opt-in to expose your ACF Field Groups and fields to the WPGraphQL Schema as some information managed by your ACF fields may not be intended for exposure in a queryable API like WPGraphQL. Show in GraphQL SettingTo have your ACF Field Group show in the WPGraphQL Schema, you need to configure the Field Group to "Show in GraphQL". Using the ACF GUIWhen using the ACF Graphic User Interface for creating fields, WPGraphQL for Advanced Custom Fields adds a Show in GraphQL field to Field Groups. Setting the value of this field to "Yes" will show the field group in the WPGraphQL Schema, if a GraphQL Field Name is also set Registering Fields in PHPWhen registering ACF Fields in PHP, you need to add
Each individual field will inherit its GraphQL name from the supplied Supported FieldsIn order to document interacting with the fields in GraphQL, an example field group has been created with one field of each type. To replicate the same field group documented here you can download the example field group and import it into your environment. For the sake of documentation, this example field group has the location rule set to "Post Type is equal to Post", which will allow for the fields to be entered when creating and editing Posts in the WordPress dashboard, and will expose the fields to the Options PagesReference: https://www.advancedcustomfields.com/add-ons/options-page/ To add an option page and expose it to the graphql schema, simply add 'show_in_graphql' => true when you register an option page. Example Usage function register_acf_options_pages() {
// check function exists
if ( ! function_exists( 'acf_add_options_page' ) ) {
return;
}
// register options page
$my_options_page = acf_add_options_page(
array(
'page_title' => __( 'My Options Page' ),
'menu_title' => __( 'My Options Page' ),
'menu_slug' => 'my-options-page',
'capability' => 'edit_posts',
'show_in_graphql' => true,
)
);
}
add_action( 'acf/init', 'register_acf_options_pages' )
Example Query
query GetMyOptionsPage {
myOptionsPage {
someCustomField
}
} Alternatively, it's you can check the Fields API Reference to learn about exposing your custom fields to the Schema. Location RulesAdvanced Custom Fields field groups are added to the WordPress dashboard by being assigned "Location Rules". WPGraphQL for Advanced Custom Fields uses the Location Rules to determine where in the GraphQL Schema the field groups/fields should be added to the Schema. For example, if a Field Group were assigned to "Post Type is equal to Post", then the field group would show in the WPGraphQL Schema on the Supported Locations@todo: Document supported location rules and how they map from ACF to the WPGraphQL Schema Why aren't all location rules supported?You might notice that some location rules don't add fields to the Schema. This is because some location rules are based on context that doesn't exist when the GraphQL Schema is generated. For example, if you have a location rule to show a field group only on a specific page, how would that be exposed the the Schema? There's no Type in the Schema for just one specific page. If you're not seeing a field group in the Schema, look at the location rules, and think about how the field group would be added to a Schema that isn't aware of context like which admin page you're on, what category a Post is assigned to, etc. If you have ideas on how these specific contextual rules should be handled in WPGraphQL, submit an issue so we can consider how to best support it! |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论