0

I use wordpress as a cms. I wanted to insert custom data via a form from the frontend in my wordpress site and then echo it on a page somewhere. After learning from here and there I am able to put together a codes below which works. Here is a code of what my form looks like currently.

<form id="costing_submit_form" action="http://somesite.com/..../process-costing.php" method="post">
<input type="hidden" name="costing_submit_date" value="<?php echo $costing_submit_date;?>"/>        
<input type="text" id="costing_submit_title" name="costing_submit_title" size="90%" maxlength="150" /><br>
<textarea id="costing_submit_description" name="costing_submit_description" size="90%" cols="60" rows="10 tabindex="15" maxlength="2000" rows="20" required="" /></textarea>    
<input type="submit" class="costing_submit_button" name="costing_submit_button" value="SUBMIT COSTING" />
</form>

At this point I am able to insert the data and also display it in the frontend using $wpdb. This part of the code is in process-costing.php which inserts the data in the database table.

global $wpdb; 
    require_once('../../../wp-load.php');
    $costing_submit_date        = strip_tags($_POST['costing_submit_date']);
    $costing_submit_title       = strip_tags($_POST['costing_submit_title']);
    $costing_submit_description = strip_tags($_POST['costing_submit_description']);
    $table_name = $wpdb->prefix . "costing";
    $wpdb->insert( $table_name, array(
    'costing_date' => $costing_submit_date,
    'costing_title' => $costing_submit_title,
    'costing_description' => $costing_submit_description,
    ));

Now, I would like to add an image to every entry made, so how do I go about it. If not a complete solution kindly give me a road-map as to what I should be doing or following, what specific functions should I be using ? Even if I am able to upload the image. My main concern is how do I associate the uploaded image to that particular entry...something like post-thumbnail perhaps ? If possible I would also like to assign a particular folder to these images. Thanks in advance.

2

For Image uploads you can use wp_handle_upload(). Adding attachments can be done through wp_insert_attachment(). The association between attachment and your costing item can be achieved through update_post_meta(). Aside: Meta keys starting with an underscore _ are not showing up in WordPress UI.

You code could be looking like this:

<?php
$my_costing_id; /* contains the id of the corresponding item in your costing table */

// create attachment
$attachment_data = array(
    'post_title' => 'An Image', 
    'post_content' => 'An Image', 
    'post_status' => 'publish', 
    'post_mime_type' => 'image/jpeg', 
);
$attachment_id = wp_insert_attachment($attachment_data , 'path/to/uploaded/file.jpg' );

update_post_meta( $attachment_id , '_costing_id' , $my_costing_id );

To get the $costing_id corresponding to a known $attachment_id simply use get_post_meta. To get the $attachment_id of a corresponding $costing_id, you will need to write your own SQL query and pass it to $wpdb:

<?php
$attachment_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_value=%d AND meta_key=%s") , $costing_id , '_costing_id' );

Hope this helps,

regards j.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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