0

I am having a content sharing site where i share themes and gadgets for windows 7.

I am uploading the file using a custom field and storing its value in a custom field named durl.

Now I want to create a separate page called "Download" and when a user visits the posts page and when he clicks the download button, he must be forwarded to a new page where the download must start.

The new page "Download" must get the custom field value "$durl" from the post.

Is it possible? I have done it in codeigniter but no idea how to achieve this using wordpress.

Please guide me on that.

regards, Rias

  • are you talking about regular post method? like $duri = $_POST["duri"]; – manny Jun 20 '12 at 13:06
  • yes... Currently i am able to get the download link in the post page using this code ID, "durl", $single = true); ?> But i want to get the durl in a separate page called "Download".. how to get the custom field value in other pages.. –  Jun 20 '12 at 13:08

2 Answers2

1

Here is another way you can do it.

Create a Downloads template by copying page.php and have this in your new template:

<?php
/*
Template Name: Downloads
*/
?>

Fill the template with header, footer, etc like page.php. Create a new page in WordPress and assign the template "Downloads" to it.

In your original post (single.php) for example, create the download link dynamically like this:

<a href="/download/?fileID=<?php echo $post->ID; ?>">Download File</a>

The user will be redirected to Downloads page which you just created. You need to put this code in your downloads template / page:

<?php

if (isset($_GET['fileID']) && is_numeric($_GET['fileID'])) { // to verify that fileID is passed
      // we now have the post ID in downloads page and can create download link
      $file = get_post_meta($_GET['fileID'], 'durl', true);
}
?>

<a href="<?php echo $file; ?>">Download now</a>

And it should print the custom field value in downloads page (which is the download link related to your post)

The idea of this code is to pass the post ID in $_GET and use the post ID to get the download link via its custom field.

Ahmed Fouad
  • 2,963
  • 10
  • 30
  • 54
  • Is it possible to customize the permalinks sir? –  Jun 21 '12 at 03:19
  • And can we have unique title to the page, like 'Download posttitle' currently i see only the same page title in all download urls.. can we make it suit the post title with prefix Download. –  Jun 21 '12 at 03:24
  • You can have a custom header title in Downloads page and in the attribute try this: <title>Download: <?php echo get_the_title($_GET['fileID']); ?> which should print Download: your post title ... – Ahmed Fouad Jun 21 '12 at 11:50
  • Thanks sir.. But can i try adding the code in present header.php itself using if condition? –  Jun 21 '12 at 12:12
  • Is it possible to hide "?fileID=" from permalinks and keep just /download/ID ? Some suggested using .htaccess, but when i use .htaccess i am not getting nay value in fileID within the post.. is there any way to sort this problem sir –  Jun 21 '12 at 12:18
  • RewriteEngine On RewriteRule ^dls/([0-9]+)$ /download/index.php?fileID=$1 that's the code i used –  Jun 21 '12 at 12:19
  • Because the fileID needs to passed to downloads template either by $_GET or $_POST see below answer. you can use if condition like See http://codex.wordpress.org/Function_Reference/is_page – Ahmed Fouad Jun 21 '12 at 20:04
0

simply create a new page template called Downloads. Copy and Paste, all the code from page.php within your theme files, then just above were it says:

<?php get_header();?>

Paste this in section of code, to create your new template file..

<?php
/*
  TEMPLATE NAME: Downloads
*/
?>

Go to pages within the admin area, and if you havent already create a page called downloads, then assign the new template to this page,

within your new template page code,

place this section of code, were you want the download link to appear

<?php 
 if(isset($_POST['durl'])){ 
    $durl = trim($_POST['durl']);
?>
<div id="download_box">
    <a href="<?php echo $durl;?>">click here to download</a>
</div>
<?php } ?>

hopefully this helps.

Marty

Marty
  • 4,619
  • 2
  • 24
  • 35