Magento WordPress Shortcodes & Widgets DocumentationShortcodes & Widgets

« Back to Magento 2 Shortcodes & Widgets

Table of Contents
  1. Installation
  2. Shortcodes
  3. Product Shortcode
  4. WordPress Functions
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-pluginshortcodewidget:*
      
      # Enable the module in Magento 2
      php bin/magento module:enable FishPig_WordPress_PluginShortcodeWidget
      
      # 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_PluginShortcodeWidget

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

    ↑ Back to Top

  • Shortcodes

    In Magento 2, if you're in a WordPress block/template, the renderShortcode method will be available to you as part of the block, making it really easy to use shortcodes.

    echo $this->doShortcode('[gravityforms id="1"]');

    If the above doesn't work, you're probably in a block/template that doesn't inherit from FishPig\WordPress\Block\AbstractBlock. In this case, you will need to get the ShortcodeManager object. This example uses the Object Manager to simplify the code but the same could be achieved using constructor injection.

    // Get the Object Manager
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    
    // Get the ShortcodeManager class
    $shortcodeManager = $objectManager->get('FishPig\WordPress\Model\ShortcodeManager');
    
    // You can call doShortcode or renderShortcode, both do the same
    echo $shortcodeManager->doShortcode('[gravityforms id="1"]');

    Using a Shortcode in a Magento 2 WYSIWYG Editor

    If you're adding content to a WYSIWYG editor, you won't be able to use PHP. Instead, you can use the pre-made block to add shortcodes. {{block class="FishPig\WordPress\Block\Shortcode" shortcode="[contactform7 id=\"123\"]"}}

    One important thing to note is that any " in the shortcode must be escaped using a slash (eg. \") so id="123" becomes id=\"123\">

    ↑ Back to Top

  • Product Shortcode

    The Shortcodes & Widgets add-on module allows you to use a Product shortcode to quickly and easily add product lists directly into your WordPress posts and pages.

    Product Shortcode Options

    There are several options for the product shortcode and these can be used separately or combined.

    ids

    A comma separated list of product IDs. Products will be ordered using the order you enter the IDs.

    [product ids="123,54,76,124"]

    skus

    A comma separated list of product SKUs. Products will be ordered using the order you enter the SKUs.

    [product skus="first-sku,second-sku,another-product"]

    category_id

    A single category ID. Products will be ordered using the position in the category.

    [product category_id="3"]

    on_sale

    A 1 to include products on sale and a 0 to include products not on sale

    [product on_sale="1"]

    limit

    Entering 0 removes the limit. Any other number will limit products to this number. This must be used in conjunction with another option.

    [product on_sale="1" limit="12"]

    Combining Options

    You can combine options above into a single Product Shortcode.

    This shortcode will display 12 products that are on sale from the category with the ID or 3.

    [product category_id="3" on_sale="1" limit="12"]

    ↑ Back to Top

  • WordPress Functions

    The Magento WordPress Integration module allows you to access all WordPress data through Magento blocks, models, collections and helpers, but sometimes you might want to use actual WordPress code in Magento. While it is recommended that you use the Magento equivalent code, if you really want to use WordPress code, you need to wrap it in a special piece of code.

    // You should probably use DI
    $om = \Magento\Framework\App\ObjectManager::getInstance();
    
    // This is the class you would inject using DI
    $coreHelper = $om->get(\FishPig\WordPress\Helper\Core::class)->getHelper();
    
    // Call the WordPress code inside the anonymous function
    $returnValue = $coreHelper->simulatedCallback(function() {
        // Any WordPress code can go here
    
        if ($post = get_post()) {
            return $post->ID;
        }
    
        return 0;
    });

    ↑ Back to Top