WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] i want add shortcode 'URL' with forum syntax.. (4 posts)

  1. patryk_chmura
    Member
    Posted 1 year ago #

    hi,
    sorry for my bad english.
    I read on wordpress codex the correct syntax for add a shortcode, but i have not found solution, maybe you can help me :-)

    i want add this codex with this syntax
    [url=http://example.com/]text[/url]
    know how to add the syntax if I wanted to be this
    [url url="http://example.com/"]text[/url]
    but I'm looking for a way to use the first, in order to lighten the work of many

    i try it

    function wp_shortcode_url($attr, $content){
     return '<a href="'.$attr[0].'" class="bbcode-url">'.$content.'</a>'.$content;
    };
     add_shortcode( 'url', 'wp_shortcode_url');

    but dont work, please, help me :-)

    thanks 1000 :-)

  2. patryk_chmura
    Member
    Posted 1 year ago #

    I was thinking that you could use the filter
    <? php add_filter ('the_content', 'filter_function_name')?>
    and regular expressions ..
    anyone can 'write the function with regular expressions?

  3. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it without the "equal" operator and without the trailing slash in the url (or add a space after it)

    [url http://example.com]text[/url]
    [url http://example.com/ ]text[/url]

    WordPress thinks it's a self enclosing shortcode with the closing slash:

    [myshortcode attr="attribute" /]

    Example:

    function add_my_link( $atts, $content = null ) {
    
      if((isset($atts[0]) && $atts[0]) && $content) {
    		return '<a href="'.esc_url((string) $atts[0]).'" >'.$content.'</a>';
    	}
    
    }
    add_shortcode( 'url', 'add_my_link' );
  4. patryk_chmura
    Member
    Posted 1 year ago #

    thanks, but i want use syntax forum , without space and with equal.

    i find solution

    function wp_shortcode_url($content){
     $pattern = '/\[url=(.+?)\](.+?)\[\/url\]/i' ;
     $replacement = '<a href="$1" class="bbcode-url">$2</a>';
    $content = preg_replace($pattern, $replacement, $content);
     return $content;
     };
    add_filter( 'the_content', 'wp_shortcode_url' );

    work :-)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.