0

i am using a plugin (download monitor) to display downloads via shortcodes. the shortcode has a single parameter, which is the download's ID: [download id="123"]

i'd like to be able to modify the download ID parameter within the shortcode via a custom field value.

i've created a custom field - ur-single-form-id - and also a shortcode for it [ur_single_form_id] via the following function:

function ur_single_form_id() {
    $cf = get_post_meta( get_the_ID(), 'ur-single-form-id', true );
    $var = '<p class="your-class">' . $cf . '</p>';
    return $var;
}
add_shortcode( 'ur_single_form_id', 'ur_single_form_id' )

this was in the hopes of being able to do something like this: [download id="[ur_single_form_id]"]

unfortunately things aren't parsed as expected, and i get an error saying "download not found".

how can i implement this such that the download ID is populated within the download monitor shortcode using the custom field value?

thanks

6
  • I think it easier to take original [download] shortcode and make your own with it. First of all, you send shortcode as a string (its always a string) and download shortcode don't fire do_shortcode() function or other filter to retrieve your value. Second thing: [download id="[ur_single_form_id]"] this construction will never work, because you use square brackets inside square brakets. Wordpress will find starting [ of download shortcode and will find first ] which is the end of your shortcode. Just paste a code of download shortcode if you need help with it)
    – anton
    Commented May 5, 2021 at 18:23
  • @anton thanks. the default download monitor code looks like this: [download id="123"] Commented May 5, 2021 at 18:48
  • Is your goal to change id value dinamically using your own shortcode? Just asking, because I see some <p> tags inside your shortcode, just like you want to format it.
    – anton
    Commented May 5, 2021 at 19:55
  • that was just for testing purposes. intended goal to change download id dynamically via the custom field value Commented May 5, 2021 at 20:20
  • I will add it as answer, but it's not tested, so write a comment if it will not work.
    – anton
    Commented May 5, 2021 at 20:22

1 Answer 1

1

I never worked with this plugin, just downloaded it from repository and found that they provide a filter for id value.

Add this filter callback function to your functions.php file.
[download id="auto"] - will use id from your post meta (hardcoded meta name)
[download id="123"] - will use id 123

function download_shortcode_custom_id($id, $atts = []){
     if(!is_numeric($id) && $id = 'auto' ):
        //change 'temp' post meta name with your own
        $post_meta = get_post_meta(get_queried_object_id(), 'temp', true);
        $post_meta ? $id = $post_meta : null;
     endif;
     return $id;
}

add_filter('dlm_shortcode_download_id', 'download_shortcode_custom_id', 10, 2);

As a second option you can set your post meta name as id value
[download id="temp"] - will use this post meta name to retrieve an id
[download id="123"] - will use id 123

function download_shortcode_custom_id($id, $atts = []){
     if( !is_numeric($id) ):
        $post_meta = get_post_meta(get_queried_object_id(), $id, true);
        $post_meta ? $id = $post_meta : null;
     endif;
     return $id;
}

add_filter('dlm_shortcode_download_id', 'download_shortcode_custom_id', 10, 2);
12
  • added both functions exactly as your demo, but dd not work. used following short codes to test: [download id="auto"] [download id="ur_single_form_id"] [ur_single_form_id] results: [Download not found] [Download not found] 72555 included my custom field shortcode to ensure printout correctly, which it does. Commented May 5, 2021 at 21:59
  • note: i renamed the second latter function of course Commented May 5, 2021 at 22:09
  • 1
    I installed this plugin to understand how it's actually works. I updated my answer with 2 option. Both work and retrieves download link by post_meta value as id, I already tested it. I preffer second option, because post_meta name there is not hardcoded.
    – anton
    Commented May 6, 2021 at 7:22
  • thanks for the extra legwork! will test again and report. Commented May 6, 2021 at 15:35
  • 1
    If you have several post meta and you want to use them both with [download] shortcode, second option is just right for you. It will get a value from any post meta, as you pass post meta name as id attribute. [download id="POST-META-NAME-1"], [download id="POST-META-NAME-2"] etc. Of course any post meta should return a valid download id, otherwise you will see "download not found"
    – anton
    Commented May 6, 2021 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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