• Hello,

    I use a theme which strips down all shortcodes from ‘single portflio’ pages (posts), yet it uses gallery id shortcode to turn the gallery into a slider (if entered into post).
    When I put additional (in this case Ninja Form plugin) shortcode into the post, it will ignore it, because it strips shortcodes out. Only the image slider will be shown.

    I need to target one specific page (post) to filter Ninja Form’s shortcode to display, while keeping all the other shortcodes stripped down according to the theme’s requisites.

    The theme’s function.php is:

    function remove_shortcode_from_index($content) {
      if ( is_singular('portfolio') ) {
        $content = strip_shortcodes( $content );
      }
      return $content;
    }
    add_filter('the_content', 'remove_shortcode_from_index');

    Thank you kindly for any advice in the right direction.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi,

    Simpliest way would be to exclude that post/page from stripping:

    function remove_shortcode_from_index($content) {
      global $post;
      if ( is_singular('portfolio') && 99 != $post->ID ) {
        $content = strip_shortcodes( $content );
      }
      return $content;
    }
    add_filter('the_content', 'remove_shortcode_from_index');

    Where ’99’ is ID of that specific post.
    But that will allow all shortcodes on that specific post/page.

    Is that fit Your requirements?

    M

    Thread Starter 528491

    (@528491-1)

    Thanks for replying so quickly. I will try this. But yes, I believe that this will cause the theme to show gallery items not as slider but as attachments therefore it might break the layout. (I already tried this by simply adding ‘do_shortcodes’ in function.php and it broke the slider along with displaying the Ninja Form. I will post when I test it out.

    I do not think that this could break the layout. strip_shortcodes should only remove shordcodes from content.

    If however this ovverride cause problems, that i think You should check the code that turnes gallery into a slider.

    Maybe it strip shortcodes, because it works on all shortcodes that is present in given content, therefore if it find shortcode that is not a gallery it stops.

    Can You provide more information about that gallery to slider changing functionality?

    M

    Thread Starter 528491

    (@528491-1)

    Hello,
    I used wrong phrase ‘broken layout’, I ment more that the page won’t show as expected by the theme. I made a screenshot example of what happens when I test the page with edited functions.php file as proposed, if possible please see at:

    https://dl.dropboxusercontent.com/u/62424385/Screenshot-Montaza-2016-03-23.jpg

    I will look inside the code and try to find where does this slider occurrence appear, couldn’t find it so far. But basically it simply uses the native WordPress [gallery] shortcode and turns it into slider and removes from body of the post.

    So my next move would be to customize function strip shortcode.

    function remove_shortcode_from_index($content) {
      global $post;
      if ( is_singular('portfolio') && 99 != $post->ID ) {
        $content = strip_shortcodes( $content );
      } elseif (99 == $post->ID){
        $content = strip_shortcodes_but_protect_ninja( $content );
      }
      return $content;
    }
    add_filter('the_content', 'remove_shortcode_from_index');

    And customized strip_shortcodes (im sorry, but its not complete and i need to leave for a while and cannot test it, it require You to alter it a little bit in selected place):

    function strip_shortcodes_but_protect_ninja( $content ) {
        global $shortcode_tags;
    
        if ( false === strpos( $content, '[' ) ) {
            return $content;
        }
    
        if (empty($shortcode_tags) || !is_array($shortcode_tags))
            return $content;
    
        // Find all registered tag names in $content.
        preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches );
        $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
    
        if ( empty( $tagnames ) ) {
            return $content;
        }
    
        // here You should remove from $tagnames ninja form tag
        // via var_dump( $tagnames ) You can find ninja tag
        // via $tagnames = array_diff( $tagnames, array('ninja_tag_here')) You can remove it from $tagnames
    
        $content = do_shortcodes_in_html_tags( $content, true, $tagnames );
    
        $pattern = get_shortcode_regex( $tagnames );
        $content = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $content );
    
        // Always restore square braces so we don't break things like <!--[if IE ]>
        $content = unescape_invalid_shortcodes( $content );
    
        return $content;
    }

    This should remove all shortcodes from page/post except ninja shortcode.
    Once again i’m sorry for not be able to give You exact code…

    Good luck
    M

    Thread Starter 528491

    (@528491-1)

    Hello, I apologise for not replying right away, thank you kindly for your hard work, I will test your code as soon as I get to it and post my follow up here, since I am working on number of simultaneous sites at once. Best,

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Theme requires stripped down shortcodes. How to keep certain shortcode visible?’ is closed to new replies.