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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0