• Hello there,

    I would like to load additional scripts to cf7. Particularly, I have two kind of scripts: CondAll.js, to be loaded for every form; CondXXX.js, to be loaded for form XXX only if the file CondXXX is found in a certain directory.

    I wrote down (and added to functions.php in my child theme) the following code:

    function wpcf7_loadConditions($wpcf7_data) {
      $formId = $wpcf7_data->id();
      if (is_null($formId) ) {return;}
    
      /* Conditions for all forms */
        wp_enqueue_script('CondAll', get_stylesheet_directory_uri() . '/js/CondAll.js', array('jquery'));
      /* END of conditions for all forms */
    
      /* Conditions for multiple forms selected by their id */
        if ( in_array($formId, array(1,2,3)) ) {
    
        }
      /*  END of conditions for multiple forms selected by their id */
    
      /* Conditions for single form based on its id */
        if ( $formId == 1 ) ) {
    
        }
      /*  END of conditions for single form based on its id */
    
      /* Conditions for forms based on the presence of a CondXXX.js file where XXX = form id */
        $myBaseURL = get_stylesheet_directory_uri() . '/js/';
        $hookName = 'Cond' . $formId;
        $condFile = $myBaseURL . $hookName . '.js';
        if (file_exists($condFile)) {
          wp_enqueue_script($hookName, $condFile, array('jquery'));
        }
      /* END of conditions for forms based on the presence of a CondXXX.js file where XXX = form id*/
    
    }
    
    add_action('wp_enqueue_scripts', 'wpcf7_loadConditions');

    The code makes WP crash; what am I doing wrong?

    https://ww.wp.xz.cn/plugins/contact-form-7/

Viewing 1 replies (of 1 total)
  • Thread Starter p.castaldi

    (@pcastaldi)

    I noticed a couple of typos, and fixed them.

    Now my code is:

    function wpcf7_loadConditions($wpcf7_data) {
      $formId = $wpcf7_data->id();
      if (is_null($formId) ) {return;}
      $myBaseURL = get_stylesheet_directory_uri() . '/js/form/';
    
      /* Conditions for all forms */
        wp_enqueue_script('CondAll', $myBaseURL . 'CondAll.js', array('jquery'));
      /* END of conditions for all forms */
    
      /* Conditions for multiple forms selected by their id */
        if ( in_array($formId, array(290,295,316,319)) ) {
        wp_enqueue_script('CondShipping', $myBaseURL . 'CondShipping.js', array('jquery'));
        }
      /*  END of conditions for multiple forms selected by their id */
    
      /* Conditions for single form based on its id */
        if ( $formId == 1 ) {
    
        }
      /*  END of conditions for single form based on its id */
    
      /* Conditions for forms based on the presence of a CondXXX.js file where XXX = form id */
        $hookName = 'Cond' . $formId;
        $condFile = $myBaseURL . $hookName . '.js';
        if (file_exists($condFile)) {
          wp_enqueue_script($hookName, $condFile, array('jquery'));
        }
      /* END of conditions for forms based on the presence of a CondXXX.js file where XXX = form id */
    
    }
    
    add_action('wp_enqueue_scripts', 'wpcf7_loadConditions');

    But I only get a “Fatal error: Call to a member function id() on a non-object” for the line $formId = $wpcf7_data->id();

    Any idea or help, anybody?

Viewing 1 replies (of 1 total)

The topic ‘Loading additional scripts for CF7’ is closed to new replies.