[wp-trac] [WordPress Trac] #36958: extending has_shortcode to allow searching in custom fields

WordPress Trac noreply at wordpress.org
Fri May 27 08:06:01 UTC 2016


#36958: extending has_shortcode to allow searching in custom fields
-------------------------+-----------------------------
 Reporter:  radugroza    |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Shortcodes   |    Version:
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 Seeing as has_shortcode is mainly used by plugin developers to search a
 shortcode in post_content, I'd like to open a discussion about what would
 be the best way to extend the has_shortcode functionality to also allow
 searching in custom post fields.

 Problem:
 - if one uses a post_meta field to store additional content for a post (
 content that may contain shortcodes and is processed when displaying the
 post ) - there is no way for another plugin to check if a shortcode exists
 in that post_meta field (without knowing the meta field, of course)
 - example: a plugin needs to enqueue a script only if the current post
 contains a shortcode. The way it's being handled right now is:
 {{{#!php
 if ( has_shortcode( $post->post_content, $tag ) ) {
         //enqueue script
 }
 }}}
 This of course will only work for the main post_content. If another plugin
 uses a post_meta field to add content to the frontend, there is no way to
 check if that meta field contains the shortcode.

 I think one possible solution for this would be to introduce a new
 function, something like:
 {{{#!php
 function post_has_shortcode( $tag, $post_id = null ) {
         $post = get_post( $post_id );
         if ( empty( $post ) ) {
                 return false;
         }

         if ( has_shortcode( $post->post_content, $tag ) ) {
                 return true;
         }

         return apply_filters( 'post_has_shortcode', false, $tag, $post_id
 );
 }
 }}}
 A filter implementation for this would look like:
 {{{#!php
 function custom_plugin_has_shortcode( $has_shortcode, $tag, $post_id ) {
         if ( $has_shortcode ) {
                 return true;
         }
         $custom_content = get_post_meta( $post_id,
 'custom_plugin_custom_content', true );

         return has_shortcode( $custom_content, $tag );
 }

 add_filter( 'post_has_shortcode', 'custom_plugin_has_shortcode', 10, 3 );
 }}}


 Another option (which is better in terms of performance) is to gather all
 the content before checking for shortcodes - but it's not that "clean":
 {{{#!php
 function post_has_shortcode( $tag, $post_id = null ) {
         $post = get_post( $post_id );
         if ( empty( $post ) ) {
                 return false;
         }

         $content_to_check = apply_filters(
 'get_content_for_shortcode_check', $post->post_content, $post_id );

         return has_shortcode( $content_to_check, $tag );
 }
 }}}

 Would there be any other approaches for this? Is this something that can
 go into the core?
 Thanks

--
Ticket URL: <https://core.trac.wordpress.org/ticket/36958>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list