• Watching this:

    <?php
    $string = "This is a long text that contains some links like http://www.wordpress.org and http://www.wordpress.com .";
    echo make_clickable($string);
    ?>

    Made this:

    function url_auto( $atts, $content = null ) {
    $string = '<a href="/external/?link=' . get_the_content() . '" target="_blank">okokok</a>';
    echo make_clickable($string);
    }

    It is taking all the content from post.

    If I use this:

    function url_auto( $atts, $content = null ) {
    $string = "<a href="/external/?link=' . get_the_content() . '" target="_blank">okokok</a>";
    echo make_clickable($string);
    }

    I get this:

    Parse error: syntax error, unexpected '?'

Viewing 13 replies - 1 through 13 (of 13 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Looks like you need to sort out your speech marks.

    By doing this:

    "<a href="

    You are ending the string.

    This might help http://stackoverflow.com/questions/7999148/escaping-quotation-marks-in-php

    the difference being a double quote at the start and end, but not followed through. If you really want to use double quotes then since you started with ” you need to put a \ before the second ” to stop php thinking it is the end of the first string set, something like (but not tested)

    $string= "<a href=\"/external/?link=" . get_the_content() . "\" target=\"_blank\">okokok</a>";

    Thread Starter mayaboni

    (@mayaboni)

    I am newbie.
    I kinda sort it out now with quotes.
    With double quotes i have to add \ before any quote that shouldn’t be the end of an string.

    Problem here is this, if I use function like this:

    function url_auto( $atts, $content = null ) {
    $string = get_the_content();
    echo make_clickable($string);
    }

    This will output nice.
    But I need to customize that to output url into my html form (link holder page).

    Tried:

    $string= "<a href=\"/external/?link=" . get_the_content() . "\" target=\"_blank\">okokok</a>";

    and

    $string = '<a href="/external/?link=' . get_the_content() . '">okokok</a>';

    Both outputing everything inside post.

    For example, output is like:

    <a href="http://wordpress.orgThis is wordpress website, ....">okokok</a>

    This is confusing.

    So what do you want the result to look like?

    Thread Starter mayaboni

    (@mayaboni)

    Result should be:

    <a href="http://wordpress.org">okokok</a>This is wordpress website, ....

    And not all content inside href.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    You may need to read up on get_the_content.

    Thread Starter mayaboni

    (@mayaboni)

    I know, the content is everything inside post.
    But, still it is weird.

    function url_auto( $atts, $content = null ) {
    $string = get_the_content();
    echo make_clickable($string);
    }
    
    add_filter( 'the_content', 'url_auto' );

    This outputs everything nice and links are clickable. Than logic would be that mine function. I will try with another way if not possible I ‘ll quit. I am very limited with php. Even what I knew I am confused more now.

    Thread Starter mayaboni

    (@mayaboni)

    Ok.

    I have new function that I made before I discovered make_clickable().

    Here it is:

    function url_auto( $atts, $content = null ) {
    $url_pattern = "/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/";
    $text = get_the_content();
    $cnt = substr($text, 0, strpos($text, '/', strpos($text, '/')+2));
    $cnt = str_replace('http://www.', '', $cnt);
    $cnt = str_replace('http://', '', $cnt);
    $cnt = str_replace('www.', '', $cnt);
    $replace = '<a href="$1" target="_blank">$2</a>';
    $text= preg_replace($url_pattern, $replace, $text);
    echo $text;
    }
    
    add_filter( 'the_content', 'url_auto' );

    Is it possible to remove this filter in shortcodes and html, because it broke my embeds and my shortcodes aswell.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Why are you making your own “make clickable” function?

    Not weird at all I’m afraid.

    Make clickable will look for any urls, and do just that within a string

    But you’re trying to make a specific piece of code, and then ask php to execute that including a target=”_blank”, NOT to make it clickable.

    so in effect you want to “echo $string” not “echo make_clickable ($string)”

    Presuming the url is not always the first in the post content, then without some really fancy code you’ll not do it easily…unless someone else knows different !

    edit – oops looks like you’ve been down that route !

    Thread Starter mayaboni

    (@mayaboni)

    I need to customize urls opening in one page that will warn user he is leaving my site, so i have link holder also, and output link, that same after he proced will go into iframe document(page).

    Yes, I know I can make warning on another way, but this is the only way I want to do it.

    I saw it somewhere and I am in love with this way of openning external urls.

    Thread Starter mayaboni

    (@mayaboni)

    Can I at least make all external links open in new tab using that make_clickable() function?

    Suggest you look at how the original function is made and make a new function using/editing this code within your functions.php

    The original function is in

    http://core.trac.wordpress.org/browser/tags/3.5.2/wp-includes/formatting.php#L0

    starts at line 1561

Viewing 13 replies - 1 through 13 (of 13 total)

The topic ‘Make Clickable URLs’ is closed to new replies.