Support

Account

Home Forums ACF PRO has_shortcode not detected?

Solved

has_shortcode not detected?

  • Hi,

    I’m using a simple custom function to load “contact form 7” javascript, only when the shortcode is in the content. This to prevent loading it on every page…

    In function.php

    // CF7 prevent loading cf7 css and js
    add_filter( 'wpcf7_load_js', '__return_false' );
    add_filter( 'wpcf7_load_css', '__return_false' );
    
    // Load only if shortcode
    function vanois_load_cf7() {
        global $post;
        if( has_shortcode( $post->post_content, 'contact-form-7')) {
            wpcf7_enqueue_scripts(); 
        }
    }
    add_action( 'wp_enqueue_scripts', 'vanois_load_cf7' );

    The shortcode

    [contact-form-7 id="140" title="Contact"]

    This works like a charm when the shortcode is in the genuine wordpress editor (classic editor).

    This doesn’t work when the shortcode is inserted in a ACF wysiwyg field :
    – the shortcode is taken into account and the form is displayed.
    – but the shortcode is not detected and my function is not fired (and so the javascript is not loaded).

    Is the global variable “$post” working in such case ?
    Is the shortcode “processed” by ACF before updating the $post variable ?

    I’ve been searching for hours in this forum, without success.
    Thank for your precious help.

  • When you use an ACF field and get the value of the field it is already formatted and any shortcodes are already run when you get the value.

    If you want to test for the shortcode in the content then you need to get the unformatted value get_field('field_name', false, false) The 2nd parameter is the post ID, setting to false uses the current post ID and the 3rd parameter tells ACF to not format the value.

    Another option would be to test the formatted content to see if it contains a form.

  • Hi John,

    Thank you for your answer.
    I change my function and it works well.

    For the records :

    
    <?php
    // CF7 Kill all
    add_filter( 'wpcf7_load_js', '__return_false' );
    add_filter( 'wpcf7_load_css', '__return_false' );
    // Load only if shortcode
    function vanois_load_cf7() {
        // if shortcode in acf fields flexible content
        if(have_rows('flexible_content')) {
            while (have_rows('flexible_content')) : 
                the_row();
                $raw_data = get_row();
                $cf7_shortcode = preg_grep("/contact-form-7/", $raw_data);
            endwhile;
            if ($cf7_shortcode) {
                wpcf7_enqueue_scripts(); 
            }
        // else if no flexible-content, check shortcode in wp default editor    
        } else {
            global $post;
            if( has_shortcode( $post->post_content, 'contact-form-7')) {
                wpcf7_enqueue_scripts(); 
            } 
        }
    }
    add_action( 'wp_enqueue_scripts', 'vanois_load_cf7' );
    ?>
    
  • For anyone who might be stuck on a similar problem, after a lot of searching, and trial-erroring, I came up with a bodged solution that deals with this, which uses $wpdb, so there’s no hardcoding a specific meta_key. Originally I bound this to ACF being active, but realised it can work for any post meta.

    global $post;
    
    //	using $wpdb, grab all post_meta for the current post_id
    //	then ignore all meta_keys beginning with an underscore
    //	then search the all remaining ones for the shortcode
    //	if the post's meta_values has the shortcode anywhere, do stuff
    global $wpdb;
    $post_id = $post->ID;
    $post_meta_sql = "select * from $wpdb->postmeta where post_id = {$post_id} and meta_key not like '\_%' and meta_value like '%[shortcode]%'";
    $post_meta_results = $wpdb->get_results( $post_meta_sql );
    
    //	run the has_shortcode() as usual, works for all the_content() cases
    
    if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'shortcode' ) ) {
    
    	//	do stuff
    	return required_shortcode_js();
    
    }
    
    //	for shortcodes in post_meta
    
    else if ( is_a( $post, 'WP_Post' ) && ! empty( $post_meta_results ) ) {
    
    	//	do stuff
    	return required_shortcode_js();
    
    }

    No idea what sort of performance hit doing this might cause, and can think of a number of cases where it could fail. There is a Core ticket open for this issue here: https://core.trac.wordpress.org/ticket/36958, which might be another way of doing it.

  • This works for shortcodes that do not have any attributes. i.e, [the_shortcode]
    But when it has an attribute i.e, [the_shortcode attr1=”aaa”] it will not work. Do you have any suggestions for that? Thanks!

  • This worked for me

    $post_meta_sql = "select * from $wpdb->postmeta where post_id = {$post_id} and meta_key not like '\_%' and meta_value like '%[shortcode%]'";

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘has_shortcode not detected?’ is closed to new replies.