Optimizing library loading with Pro

Hello,
We are attempting to conditionally load certain libraries (CF7’s in this context) only when specific shortcodes are being used. We are detecting the codes properly when looking at the post’s content, but not when Pro headers (or footers) contain it (like putting a contact form inside a popup hidden in the footer).

How can we access the current header’s content? (so we can look for the shortcode in it)

See code below.

function wpcf7o_dequeue_scripts() {
    $load_scripts = false;
    if ( is_singular() ) {
        $post = get_post();
        if( has_shortcode( $post->post_content, 'contact-form-7' ) ) {
            $load_scripts = true;
        }
    }
    if ( ! $load_scripts ) {
        wp_dequeue_script( 'contact-form-7' );
        wp_dequeue_style( 'contact-form-7' );
    } else {
        add_action( 'wp_head', 'wpcf7o_track_mailsent' );
    }
}
if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
    add_action( 'wp_enqueue_scripts', 'wpcf7o_dequeue_scripts', 99 );
}

Any hint or help would be appreciated! :slight_smile:

Hello @MagikWeb,

Thank you for the very detailed post information. Be advised that the custom header or footer has been stored in the post meta as _cornerstone_data . If you can access the post meta and inspect it, you should be able to find it. Meanwhile, please also check out the file \cornerstone\includes\classes\components\class-shortcode-finder.php so that you will have an understanding of how it accessed in the theme.

Best Regards.

1 Like

Thank you @ruenel,
Sadly, the _cornerstone_data meta doesn’t contain the page’s header content. We are looking for the pro header content of a specific page. According to the database, it’s stored as a separate post. It’s a global header in this case, but we wanna make sure we look at the right header when rendering a specific page.

For extra clarification, we are trying to find this shortcode programmatically by using the previous code (by using get_post()).

(Hopefully you can zoom)

Thank you for any help! :slight_smile:

Hi @MagikWeb,

The assigned custom header data stored in the wp_options table at the cornerstone_header_assignments option name. You can find the assigned header id from the wp_options table for the specific page, and the header content can be found from wp_post table.
Just for your information, the header content the almost same shortcode as page content, so it will be a bit difficult to recognize the header specifically by any shortcode.

Hope it helps.
Thanks

1 Like

Thank you! That’s exactly what we were looking for.
I have a last question to avoid coding pre-existing functions, do you have a helper function you use when binding the right headers to the page’s post?

If yes, we’d like to know where it’s located, because we will reuse it (100% of our sites use your theme anyway).
Otherwise, we will fetch the option separately with get_option and validate which headers are bound to the current page (when not global) manually.

This optimization will benefit 100+ sites, because we use your theme on large WPMUs. :slight_smile:
We thank you for the advanced support you’re providing us.

Hi Magik,

Regretfully, there’s no pre-existing helper function to use when binding the right headers to the page’s post. These are the only filters and hooks available in our themes.

Hope that helps and thank you for understanding.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.