Skip to content

Optimization Issue: JavaScript Enqueuing Fails Programmatically Added Forms #137

@erikyo

Description

@erikyo

In version 0.7.0, a new optimization was introduced to prevent unnecessary enqueuing of the JavaScript script and related assets, implemented in the following code block:

private function cf7_shortcode_exists() {
global $post;
return is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'contact-form-7' );
}
public function load_scripts() {
if ( ! $this->cf7_shortcode_exists() ) {
return;
}
/* adds the javascript script to frontend */
add_action( 'wp_footer', array( $this, 'enqueue_scripts' ) );
}

However, this optimization appears to fail in certain scenarios, specifically when the contact form is added programmatically (i.e., not directly through the editor/shortcode).

To address this compatibility issue, the script loading optimization should be made optional. This would allow users to manually enable or disable the feature, ensuring stability for non-standard implementations while maintaining the performance benefit for others.

Otherwise I need to check in other way whether the form is present on the page. (I used the code from the first example on this page https://developer.wordpress.org/reference/functions/has_shortcode/)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions