3

I need to hide the URLs of downloads in wordpress posts. I have found a great script to do this but it is not a plugin. I have installed the script and created a function to include it. I am not a pro with php at all.

However the script has a line to normally call it:

<a href="<?php downloadurl('http://yourdomainname.comdownloadables.zip','veryspecials'); ?>" >Your Downloadables</a>

I am not able to place this directly in posts so I am trying to make a shortcode for it, but I am getting stuck. The shortcode that I have is:

function secshort_func($atts, $content = null) {
extract(shortcode_atts(array(
    "linkurl" => '#Download_Does_Not_Exist',
    "linktitle" => 'Download',
), $atts));
return '<a href="<?php downloadurl(' .$linkurl. ','veryspecials'); ?>" >' .$linktitle. '</a>';
}
add_shortcode( 'secdown', 'secshort_func' );

I am getting errors when trying to run this, and through a process of elimination I know that it is from this part of the return code:

"<?php downloadurl(' .$linkurl. ','veryspecials'); ?>"

After searching the internet for solutions and trying everything that I can think of, I am completely stuck.

Any help would be very much appreciated - it is driving me crazy being stuck on such a little thing!

BarryB
  • 31
  • 1

2 Answers2

1

A few observations along with the answer:

  1. Format your code. It makes life much easier when troubleshooting. Good indenting is huge (see below for your code, formatted).
  2. Don't use cryptic / abbreviated function names. Type them out so that you create self documenting code.
  3. It's better to not use extract. There are some who say it's OK, but it can create confusing code that is hard to troubleshoot because you don't know where a variable came from. It's preferable to explicitly set the variables. (In your case, because you use them only once, it's simplest to just reference them in the array form - $atts['link_url'])
  4. You can call the function, but it has to be concatenated into the string (see below). Your code (and the other answer), are passing php into the string, rather than calling the function and passing the result into the string.

Formatted code, with answer:

// Use a clearer function name.  No need for  "func", that's implied
function download_link_shortcode($atts, $content = NULL) {  
    // Declare $defaults in a separate variable to be clear, easy to read
    $defaults = array(
        "link_url"   => '#Download_Does_Not_Exist',
        "link_title" => 'Download',
    );

    // Merge the shortcode attributes
    $atts = shortcode_atts( $defaults, $atts );

    // Concatenate in the results of the `download` function call....
    return '<a href="' . downloadurl( $atts['link_url'], 'veryspecials' ) . '">' . $atts['link_title'] . '</a>';
}

add_shortcode( 'secdown', 'download_link_shortcode' );
Community
  • 1
  • 1
random_user_name
  • 25,694
  • 7
  • 76
  • 115
0

Try using double outer quotes and escape inner single quotes like this:

return "<a href=\'<?php downloadurl(\'" . $linkurl . "\',\'veryspecials\'); ?>\' >" .$linktitle. '</a>';
atomCode
  • 842
  • 7
  • 17