Uploaded image for project: 'CiviCRM'
  1. CiviCRM
  2. CRM-21084

Give themers and plugin devs ability to test if CiviCRM is currently being displayed in a WordPress shortcode

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: 4.7.23
    • Fix Version/s: None
    • Component/s: WordPress Integration
    • Labels:
    • Versioning Impact:
      Minor (add functionality in backwards-compatible manner)
    • Documentation Required?:
      Developer Doc
    • Funding Source:
      Contributed Code
    • Verified?:
      No

      Description

      Overview

      Presently, there is no convenient way for a WordPress developer to determine whether a CiviCRM shortcode is in use on the current page.

      There have been a few reports of WordPress filters such as wpautop, which replaces double line-breaks with paragraph elements, needing to be tamed on CiviCRM pages. In the case of wpautop run amok, the CiviCRM code itself gets wrapped in paragraph tags – this is most obvious when wrapped JavaScript and throws parse errors. Often these problems can be addressed by adding something like the following to the theme's functions.php file:

      if(civicrm_wp_in_civicrm()) {
        remove_filter('the_content', 'problematicFilter');
      }
      

      However, civicrm_wp_in_civicrm() does not return true when CiviCRM content is displayed via shortcode, and there isn't a good way to prevent an aggressive filter from being applied to CiviCRM shortcode content.

      Proposed Solution

      The proposed solution is to expose information about the active shortcodes through the CiviCRM_For_WordPress singleton. The linked pull request adds a global function civicrm_wp_is_shortcode() which returns a boolean. With this utility, a developer can do something like this in the theme's functions.php:

      // listen for the civicrm_shortcodes_parsed event, then determine
      // what kind of page we're on and manage filters accordingly
      add_action('civicrm_shortcodes_parsed', 'manageCiviFilters');
      
      function manageCiviFilters () {
        if (civicrm_wp_is_shortcode()) {
          add_filter('the_content', 'fancyShortcodeFilter');
        }
      
        if (civicrm_wp_is_shortcode() || civicrm_wp_in_civicrm()) {
          remove_filter('the_content', 'civiUnawareFilter');
        }
      }
      
      function fancyShortcodeFilter($content) {
        // remove some text we don't want to display in shortcode mode
        return $content;
      }
      

       

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              ginkgofjg Frank Gómez
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated: