Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm looking for a plugin that allows me to upload a file and provide me a shortcode link that I can edit manually. I tried Wordpress Download Manager that create a shortcode like

[wpdm_file id="1"]

but when I try to edit it, the plugin crashes. The reason is that I create a custom theme with a custom button in the header, with a do_shortcode in the link tag.

<a href="<?php echo do_shortcode(''); ?>" target=_blank>

Although the button and the shortcode remains the same, I want to change the file associated with it from time to time without change the href link every time. Is there a plugin that makes me do it?

share|improve this question

closed as off topic by toscho Apr 15 at 5:02

Questions on WordPress Answers are expected to relate to WordPress within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

2 Answers

This might be better handled by creating an option page for your theme which allows you to select a file (even possibly from the media manager).

Here is a good explanation of how to create an options page.

share|improve this answer

How to

It's as easy as adding a shortcode with add_shortcode(). The following Plugin parses the input arguments using shortcode_atts(), which is quite similar to wp_parse_args() and cares about the shortcode defaults and your - user defined - arguments.

The Plugin

I guess the shortcode attributes and their defaults are quite self explanatory. As you can see, I showed you how to add classes, which you can also use to add other things to the wrapper. If you need to add things to the link itself, just do it like this:

'alt' => 'Download me" class="some-class-for-the-link'

To make it a "little safer", I run esc_attr() on each of the input arguments.

<?php
! defined( 'ABSPATH' ) AND exit;
/** Plugin Name: (#48078) »kaiser« Shortcode to link to a download */

function wpse48078_file_download( $attr )
{
    $attr = array_map( 'esc_attr', $attr );

    extract( shortcode_atts( array(
         'href'   => ''
        ,'target' => '_blank'
        ,'alt'    => 'Download me'
        ,'title'  => 'Download me'
        ,'text'   => 'Download'
        ,'wrap'   => 'div class="file-download"'
    ), $attr ), EXTR_SKIP );

    $html = "<a href='{$href}' target='{$target}' alt='{$alt}' title='{$title}'>{$text}</a>";

    $wrap AND $html = "<{$wrap}>{$html}</{$wrap}>";

    return $html;
}
add_shortcode( 'file_download', 'wpse48078_file_download' );
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.