Support

Account

Home Forums ACF PRO has_shortcode not detected? Reply To: has_shortcode not detected?

  • 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.