Magento 2 WordPress Integration ACF Documentation
-
Installation
You can install the module for Magento 2 using Composer or you can manually install it using FTP.
ComposerManualRun the following commands in your Magento 2 root directory to install the module using Composer.
# Add the FishPig Composer repo composer config repositories.fishpig composer https://repo.fishpig.com/ # Install the module using Composer composer require fishpig/magento2-wordpress-integration-acf:* # Enable the module in Magento 2 php bin/magento module:enable FishPig_WordPress_ACF # Run the Magento upgrade system php bin/magento setup:upgrade
You can download the latest version of the module by logging in to your account and selecting Account > Projects.
Extract the ZIP file and upload the files to your Magento site at the directory below:
app/code/FishPig/WordPress_ACF
When creating the folders, ensure you use the correct capitalisation.
To complete the installation, run the following commands in a terminal.
# Enable the module in Magento 2 bin/magento module:enable FishPig_WordPress_ACF # Run the Magento upgrade system bin/magento setup:upgrade
-
Configuration
REST API
Advanced Custom Fields now uses the WordPress REST API to publish ACF field data. You can read more on this in the official documentation.
To get this working with Magento, you need to enable this for each field group that you want to access in Magento. You can do this by toggling the 'Show in REST API' switch on the field group page in ACF in the WordPress Admin.
-
Code Examples
ACF fields can be stored against most data types in WordPress. The code below shows how to access this data in Magento for each specific data type.
Post Fields
Post fields can be retrieved using the getMetaValue of the FishPig\WordPress\Model\Post class.
// If $post is defined it's this easy echo $post->getMetaValue('my_acf_field'); // If $post is not defined check the registry for the current post $registry = \Magento\Framework\App\ObjectManager::getInstance()->get( \Magento\Framework\Registry::class ); if ($post = $registry->registry('wordpress_post')) { echo $post->getMetaValue('my_acf_field'); } // Or load a new $post and then get field value $postRepository = \Magento\Framework\App\ObjectManager::getInstance()->get( \FishPig\WordPress\Model\PostRepository::class ); $post = $postRepository->get(1234); echo $post->getMetaValue('my_acf_field');
Term Fields
Term fields can be retrieved using the getMetaValue of the FishPig\WordPress\Model\Term class.
// If $term is defined in the file echo $term->getMetaValue('my_acf_field'); // If $term is not defined, check the registry for the current term $registry = \Magento\Framework\App\ObjectManager::getInstance()->get( \Magento\Framework\Registry::class ); if ($term = $registry->registry('wordpress_term')) { echo $term->getMetaValue('my_acf_field'); } // Or load a new $term and then get field value $termRepository = \Magento\Framework\App\ObjectManager::getInstance()->get( \FishPig\WordPress\Model\TermRepository::class ); $term = $termRepository->get(1); echo $term->getMetaValue('my_acf_field');
Option Fields
Options are not tied directly to a Magento object but can be retrieved via a Helper class provided by the module.
// OM for simplicity. Use DI in your project $acfHelper = \Magento\Framework\App\ObjectManager::getInstance()->get( \FishPig\WordPress_ACF\Helper\Data::class ); // Echo out the options field value echo $acfHelper->getOptionsField('my_acf_options_field');
Widget Fields
Options are not tied directly to a Magento object but can be retrieved via a Helper class provided by the module.
// OM for simplicity. Use DI in your project $acfHelper = \Magento\Framework\App\ObjectManager::getInstance()->get( \FishPig\WordPress_ACF\Helper\Data::class ); // Echo out the options field value echo $acfHelper->getWidgetField('my_widget_field', 'archives-3'); // If you are an in a widget template (FishPig_WordPress::sidebar/widget/*.phtml) you can use: echo $block->getWidgetField( 'my_widget_field', $this->getWidgetType() . '-' . $this->getWidgetId() );