Support » Plugins » Hacks » How to filter( and render ) a perticular shortcode only ?

  • Resolved sau108


    what’s needed ::
    $content = “some text [code]
    [/code] some other text[button3] here”;

    I want to filter only [code] shortcode. Hooks for buttons and code are already registered. So when I call do_shortcode( $content ); both [code] and [button3] are filtered but i do not want shortcode other than [code] to get filtered. And also I cannot unregister any shortcode.

    I think do_shortcode_tag() or regular expression will come handy to find all [code][/code] tags and then filter them using preg_replace_callback, but i am not able to form correct regular expression. I also do not know how to use do_shortcode_tag to filter only [code][/code] shortcode.
    please help !!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz


    I’m not entirely sure either, but a couple thoughts. One is simply find the [code] in content using normal php search functions and feed the the results to do_shortcode().

    Two, experimentally run get_shortcode_regex() and manually extract the regexp for [code] and then only feed that portion to do_shortcode_tag() each time.

    Thanks for the reply bcwprkz.

    I want to filter [code][/code] before others, mainly before wp replaces new lines with <br.> and <p> tags, otherwise $content(given above) is rendered as

    some text <pre.>
    </pre.> some other text[button3] here

    but it should be like

    some text <pre.>
    </pre.> some other text[button3] here

    The solution cannot be to search all <br.> and <p> tags and remove them as this will remove user written tags too.

    I figured regxp to find [code ]….[/code] as
    $reg_code = ‘/(\n|\s){0,1}\[code(\s)*([A-Za-z0-9\.\-_\s\’\”=<>\[\]\/:#&`~!@$%\^\(\)\+\-\\\|\{\}\?;\,\*])*\/code\](\n|\s){0,1}/’;

    and added [code] filter as
    “add_filter( ‘the_content’, ‘find_code’, 1 );”
    find_code function uses regxp and finds all [code] in content and sends one by one to render_code function
    where render_code is [code]…[/code] filter function.

    This helped me to get expected output as written above without <br.> tags however, I dont know why, wp is now adding extra and unwanted <p> and <br.> tags to other parts( other than [code]…[/code] ) of post_content.

    Moderator bcworkz


    Annoying little “feature”, that. Not sure it’s a total solution, but this should help: Function_Reference/wpautop#Disabling_the_filter

    Thanks for the help.

    Well I feel happy, as this is my first query to wordpress support forum and people( thanks to bcworkz ) came up to help.

    I figured a way to get things work correctly. I believe that this is not an elegant way to do it however I could not find a better solution.

    Probable SOLUTION::

    plan :: add_shortcode( ‘code’, ‘render_code’ ); Here render_code is filter function for code shortcode. Now obviously render_code is called each time wp finds [code] in post_content. The trick is that render_code determines how many times( first time or second time or so on ) it is called by wp, using a global variable( this variable stores the number of times render_code function is called ).

    If function is called first time then find first occurrence of [code]….[/code] from the $post->post_content and pass it to do_shortcode. Similarly if function is called second time then find second occurrence of [code]….[/code] from the $post->post_content and pass it to do_shortcode.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to filter( and render ) a perticular shortcode only ?’ is closed to new replies.