The MU forums have moved to WordPress.org

Allowing a shortcode in comments (22 posts)

  1. easysleeper
    Member
    Posted 15 years ago #

    I did a search and found nothing relevant to this:

    I want to create a shortcode, and allow it to be used in comments. However I do not want to allow ALL shortcodes to be used in comments.

    Is there a command for this? like add_shortcode_comment or something.

    Thanks

  2. SteveAtty
    Member
    Posted 15 years ago #

    You mean you need to code a filter to turn a "shortcode" into a predefined block of text?

  3. easysleeper
    Member
    Posted 15 years ago #

    yes that would probably do it, if I understand what you are saying correctly.

  4. SteveAtty
    Member
    Posted 15 years ago #

    So do you want to change the short code to the long text when the comment is saved or do you want to keep the short code in the comment and expand it on display?

  5. easysleeper
    Member
    Posted 15 years ago #

    I would prefer to expand it on display, just in case of a security threat I can always disable the plugin and end it

  6. SteveAtty
    Member
    Posted 15 years ago #

    So you need to add a filter on the comment_text.

    Single code or a fixed list, do you need to maintain it through the admin back end or will editing a file be enough?

  7. easysleeper
    Member
    Posted 15 years ago #

    editing a file will work, and for now so will single code.

    I can take a simple version of this and expand it as needed.

    Thanks for all your help

  8. easysleeper
    Member
    Posted 15 years ago #

    Let me add this too:

    It needs to have parameters just like a shortcode would do.

    for instance they user would comment like this:

    [shortcode parameter="text-to-use"]

    Thanks

  9. SteveAtty
    Member
    Posted 15 years ago #

  10. easysleeper
    Member
    Posted 15 years ago #

    The issue is that won't work on comments

    it leaves the code as [whatever]

  11. easysleeper
    Member
    Posted 15 years ago #

    I have built a huge library of shortcodes but they just don't work and I don't want to just blanket allow them to work either because I want a separate library for comments

  12. SteveAtty
    Member
    Posted 15 years ago #

    yes but basically you need to take that basic idea and use

    add_filter('comment_text', 'do_shortcode');

  13. easysleeper
    Member
    Posted 15 years ago #

    you learn something new everyday!

    Thanks I'll give this a shot.

  14. easysleeper
    Member
    Posted 15 years ago #

    can you give me a quick example of what the function would look like to handle the parameters?

    to turn this:
    [shortcode parameter="http://mu.wordpress.org"]
    into this:
    <a href="$parameter"></a>

    just as an example

  15. SteveAtty
    Member
    Posted 15 years ago #

    So try this:

    <?php
    
    function myUrl($atts, $content = null) {
    	extract(shortcode_atts(array(
    		"href" => 'http://'
    	), $atts));
    	return '<a href="'.$href.'">'.$content.'</a>';
    }
    
    add_shortcode("url", "myUrl");
    add_filter('comment_text', 'do_shortcode');
    
    ?>

    That allows you to do

    [url href="http://www.wprecipes.com"]WordPress recipes[/url]

  16. easysleeper
    Member
    Posted 15 years ago #

    But would that also allow ALL shortcodes to be used?

  17. SteveAtty
    Member
    Posted 15 years ago #

    No, that would only allow url to be used

    BUT if you wait a few minutes....

  18. easysleeper
    Member
    Posted 15 years ago #

    Hmm, just a thought if I did the

    add_filter('comment_text', 'do_shortcode');
    after the shortcodes I want allowed, but before the

    add_shortcodes of the ones I don't want to be allowed would that work?

  19. SteveAtty
    Member
    Posted 15 years ago #

    function myShorty($tags, $content = null) {
    $content2="";
    $selected_tag=array_keys($tags);
    switch ($selected_tag[0]) {
        case "apple":
            $content2="Apples are ".$content." and ".$tags[$selected_tag[0]];
            break;
        case "bar":
             $content2="Bar is ".$content." and ".$tags[$selected_tag[0]];
            break;
        case "cake":
             $content2="Cake is ".$content." and ".$tags[$selected_tag[0]];
            break;
    }
    return $content2;
    }
    
    add_shortcode("shorty", "myShorty");
    add_filter('comment_text', 'do_shortcode');

    So

    [shorty apple="round fruit that grow on trees"]Possibly apples[/shorty]
    [shorty cake="yummy stuff you cook in an oven"]Something about cake[/shorty]
    [shorty bar="a place to by a drink"]Description of a bar [/shorty]

    gives

    Apples are Possibly apples and round fruit that grow on trees
    Cake is Something about cake and yummy stuff you cook in an oven
    Bar is Description of a bar and a place to by a drink

    and no other shorty tags do anything

    OK they are poor examples but it shows the use of two parts of the short code and how the switch works

  20. easysleeper
    Member
    Posted 15 years ago #

    Thanks a lot, this was a big help.

  21. SteveAtty
    Member
    Posted 15 years ago #

    I realised afterwards that of course that the whole way the shortcode filters works means that you can't really stop anyone using any shortcode anywhere, so the only logical way is to only allow a limited number anywhere.

  22. easysleeper
    Member
    Posted 15 years ago #

    well I created a do_comment_shortcode function and just used the code from do_shortcode, in this I limited the ones that could be used so then when I do

    add_filter('comment_text', 'do_comment_shortcode')

    it only allows shortcodes that I specify in the do_comment_shortcode function.

    You put me on the right path, thanks again.

About this Topic

  • Started 15 years ago by easysleeper
  • Latest reply from easysleeper