2

I'm using CiviCRM 4.7.31 on WordPress with the Members plugin. I created a new profile, then embedded that form in edit mode within a WordPress page:

[civicrm component="profile" gid="35" mode="edit" hijack="0"]

As expected, the WordPress page generates an error if a user is not logged in:

"No user/contact ID was specified, so the Profile cannot be used in edit mode."

This error persists even when I set the page's Content Permissions to limit access to users in selected roles.

The error disappears when I wrap the civicrm shortcode within the members_logged_in shortcode:

[members_logged_in]
[civicrm component="profile" gid="35" mode="edit" hijack="0"]
[/members_logged_in]

But then the profile form loses its formatting; it seems like some Javascript/CSS is not loaded on the page when the shortcode is nested.

Is there a way to make this work correctly? Do I need to switch to Caldera Forms with the Caldera Forms CiviCRM plugin?

Thanks for your help.

2 Answers 2

2

The solution was to make sure the Civi scripts loaded on the page. This answer provides guidance on how to do that.

0

Did you find a solution? This is still happening with: Wordpress version: 5.6 CiviCRM version: 5.32.2

I'm having a consulting team investigate, I'll report back if they find a solution.

UPDATE: I found a VERY hacky workaround. I added this block to civicrm.shortcodes.php inside "prerender" function:

          // default regex FAILED. Support nested shortcodes with custom regex.
      if (empty($shortcodes_array)) {
        preg_match_all('/\[civicrm component="(contribution|event|profile|user-dashboard|petition)"( action="[a-z]+"| mode="[a-z]+")?( id="[0-9]")?( gid="[0-9]")?\]/', $post->post_content, $located);
        if (!empty($located) && !empty($located[0])) {
          $shortcodes_array = $located[0];
        }
      }

Here is the entire function with my custom block included:

  public function prerender( $wp ) {

/*
 * At this point, all conditional tags are available
 * @see http://codex.wordpress.org/Conditional_Tags
 */

// Bail if this is a 404
if ( is_404() ) return;

// A counter's useful
$shortcodes_present = 0;

/*
 * Let's loop through the results
 * This also has the effect of bypassing the logic in
 * https://github.com/civicrm/civicrm-wordpress/pull/36
 */
if ( have_posts() ) {
  while ( have_posts() ) : the_post();

    global $post;

    // Check for existence of shortcode in content
    if ( has_shortcode( $post->post_content, 'civicrm' ) ) {

      // Get CiviCRM shortcodes in this post
      $shortcodes_array = $this->get_for_post( $post->post_content );

      // default regex FAILED. Support nested shortcodes with custom regex.
      if (empty($shortcodes_array)) {
        preg_match_all('/\[civicrm component="(contribution|event|profile|user-dashboard|petition)"( action="[a-z]+"| mode="[a-z]+")?( id="[0-9]")?( gid="[0-9]")?\]/', $post->post_content, $located);
        if (!empty($located) && !empty($located[0])) {
          $shortcodes_array = $located[0];
        }
      }

      // Sanity check
      if ( !empty( $shortcodes_array ) ) {

        // Add it to our property
        $this->shortcodes[$post->ID] = $shortcodes_array;

        // Bump shortcode counter
        $shortcodes_present += count( $this->shortcodes[$post->ID] );

      }

    }

  endwhile;
}

// Reset loop
rewind_posts();

// Did we get any?
if ( $shortcodes_present ) {

  // We need CiviCRM initialised prior to parsing shortcodes
  if (!$this->civi->initialize()) {
    return;
  }

  // How should we handle multiple shortcodes?
  if ( $shortcodes_present > 1 ) {

    // Add CSS resources for front end
    add_action( 'wp_enqueue_scripts', array( $this->civi, 'front_end_css_load' ), 100 );

    // Let's add dummy markup
    foreach( $this->shortcodes AS $post_id => $shortcode_array ) {

      // Set flag if there are multple shortcodes in this post
      $multiple = ( count( $shortcode_array ) > 1 ) ? 1 : 0;

      foreach( $shortcode_array AS $shortcode ) {

        // Mimic invoke in multiple shortcode context
        $this->shortcode_markup[$post_id][] = $this->render_multiple( $post_id, $shortcode, $multiple );

      }

    }

  } else {

    // Add core resources for front end
    add_action( 'wp', array( $this->civi, 'front_end_page_load' ), 100 );

    /*
     * Since we have only one shortcode, run the_loop again
     * the DB query has already been done, so this has no significant impact
     */
    if ( have_posts() ) {
      while ( have_posts() ) : the_post();

        global $post;

        // Is this the post?
        if ( ! array_key_exists( $post->ID, $this->shortcodes ) ) {
          continue;
        }

        // The shortcode must be the first item in the shortcodes array
        $shortcode = $this->shortcodes[$post->ID][0];

        // Check to see if a shortcode component has been repeated?
        $atts = $this->get_atts( $shortcode );

        // Test for hijacking
        if ( isset( $atts['hijack'] ) AND $atts['hijack'] == '1' ) {
          add_filter( 'civicrm_context', array( $this, 'get_context' ) );
        }

        // Store corresponding markup
        $this->shortcode_markup[$post->ID][] = do_shortcode( $shortcode );

        // Test for hijacking
        if ( isset( $atts['hijack'] ) AND $atts['hijack'] == '1' ) {

          // Ditch the filter
          remove_filter( 'civicrm_context', array( $this, 'get_context' ) );

          // Set title
          global $civicrm_wp_title;
          $post->post_title = $civicrm_wp_title;

          // Override page title
          add_filter( 'single_post_title', array( $this->civi, 'single_page_title' ), 50, 2 );

          // Overwrite content
          add_filter( 'the_content', array( $this, 'get_content' ) );

        }

      endwhile;
    }

    // Reset loop
    rewind_posts();

  }

}

// Flag that we have parsed shortcodes
$this->shortcodes_parsed = TRUE;

/**
 * Broadcast that shortcodes have been parsed.
 *
 * @since 4.6
 */
do_action( 'civicrm_shortcodes_parsed' );

}

Please reply if there's a better way to do this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.