Magento WordPress ACF DocumentationACF

« Back to Magento 2 ACF

Table of Contents
  1. Installation
  2. Code Examples
    1. Post Fields
    2. Term Fields
    3. Option Fields
    4. Widget Fields
Need Help?

info@fishpig.com

 
  • Installation

    You can install the module for Magento 2 using Composer or you can manually install it using FTP.

    • Run 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.co.uk/
          
      # Install the module using Composer
      composer require fishpig/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 from the Downloads page.

      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
      php bin/magento module:enable FishPig_WordPress_ACF
      
      # Run the Magento upgrade system
      php bin/magento setup:upgrade

    ↑ Back to Top

  • 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.

    /*
     * Assuming $post is defined
     */
    echo $post->getMetaValue('my_acf_field');
    
    /*
     * $post is not defined in template but 
     * current request is a post page so we can
     * load $post from registry
     */
    $registry = \Magento\Framework\App\ObjectManager::getInstance()
    	->get('Magento\Framework\Registry');
    
    if ($post = $registry->registry('wordpress_post')) {
    	echo $post->getMetaValue('my_acf_field');
    }
    
    /*
     * $post is not defined in template and current page 
     * is not a post page so load new $post object
     */
    $post = \Magento\Framework\App\ObjectManager::getInstance()
    	->get('FishPig\WordPress\Model\PostFactory')->create();
    
    if ($post->load(1234)->getId()) {
    	echo $post->getMetaValue('my_acf_field'])	;
    }

    Term Fields

    Term fields can be retrieved using the getMetaValue of the FishPig\WordPress\Model\Term class.

    /*
     * Assuming $term is defined
     */
    echo $term->getMetaValue('my_acf_field');
    
    /*
     * $post is not defined in template but 
     * current request is a post page so we can
     * load $post from registry
     */
    $registry = \Magento\Framework\App\ObjectManager::getInstance()
    	->get('Magento\Framework\Registry');
    
    if ($term = $registry->registry('wordpress_term')) {
    	echo $term->getMetaValue('my_acf_field');
    }
    
    /*
     * $post is not defined in template and current page 
     * is not a post page so load new $post object
     */
    $term = \Magento\Framework\App\ObjectManager::getInstance()
    	->get('FishPig\WordPress\Model\TermFactory')->create();
    
    if ($term->load(1)->getId()) {
    	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.

    /*
     * We'll use the Object Manager for simplicity but you can use
     * constructor injection to inject the Helper class
     */
    $acfHelper = \Magento\Framework\App\ObjectManager::getInstance()
    	->get('FishPig\WordPress_ACF\Helper\Data');
    
    // 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.

    /*
     * We'll use the Object Manager for simplicity but you can use
     * constructor injection to inject the Helper class
     */
    $acfHelper = \Magento\Framework\App\ObjectManager::getInstance()
    	->get('FishPig\WordPress_ACF\Helper\Data');
    
    // 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 $acfHelper->getWidgetField(
      'my_widget_field', 
      $this->getWidgetType() . '-' . $this->getWidgetId()
     );

    ↑ Back to Top