• I’m working on a plugin to allow a user to post an attachment to their own page(s) in the front it. Everything is working, but I want to also allow the form to have a “Title” field and store that in the DB. I have the field to fill in, but not exactly sure how to save the attachment Title.

    I hope that makes since. Here is the code and the form:

    echo '<form name="s8-wpfs-form" id="s8-wpfs-form" method="POST" action="" enctype="multipart/form-data">';
    echo '<label for="title">Title</label>';
    echo '<input type="text" name="title" id="title">';
    echo '<label for="uploaded_attachment">Attachment</label>';
    echo '<input type="file" name="uploaded_attachment">';
    echo '<input type="submit" value="Upload" id="submit" name="submit" />';
    wp_nonce_field('client-file-upload', 'client-file-upload');
    echo '</form>';

    Here is the code that works and uploads the file, but does not store the Title of the attachment:

    function insert_attachment($file_handler,$post_id,$setthumb='false') {
    	// check to make sure its a successful upload
    	if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
    
    	require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    	require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    	require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    
    	$attach_id = media_handle_upload($file_handler, $post_id);
    
    	if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
    
    	return $attach_id;
    }

    Any direction would help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Aaron Reimann

    (@areimann)

    bump

    Thread Starter Aaron Reimann

    (@areimann)

    Ok. I have it figured out.

    ‘function insert_attachment($file_handler,$post_id,$setthumb=’false’) {
    // check to make sure its a successful upload
    if ($_FILES[$file_handler][‘error’] !== UPLOAD_ERR_OK) __return_false();

    require_once(ABSPATH . “wp-admin” . ‘/includes/image.php’);
    require_once(ABSPATH . “wp-admin” . ‘/includes/file.php’);
    require_once(ABSPATH . “wp-admin” . ‘/includes/media.php’);

    $attach_id = media_handle_upload($file_handler, $post_id);

    wp_update_post( array(
    ‘ID’ => $attach_id,
    ‘post_title’ => $_POST[‘title’],
    ‘post_content’ => $_POST[‘description’] )
    );

    return $attach_id;
    }’

    And the HTML form:
    ‘ echo ‘<form method=”POST” action=”” enctype=”multipart/form-data”>’;
    echo ‘<fieldset>’;
    echo ‘<legend>Upload a File</legend>’;
    echo ‘<label for=”title”>Title</label>’;
    echo ‘<input type=”text” name=”title” id=”title”>’;
    echo ‘
    ‘;
    echo ‘<label for=”title”>Description</label>’;
    echo ‘<textarea name=”description” id=”description”></textarea>’;
    echo ‘
    ‘;
    echo ‘<label for=”uploaded_attachment”>Attachment</label>’;
    echo ‘<input type=”file” name=”uploaded_attachment” id=”uploaded_attachment”>’;
    echo ‘
    ‘;
    echo ‘<input type=”submit” value=”Upload” id=”submit” name=”submit” />’;
    echo ‘</fieldset>’;
    wp_nonce_field(‘client-file-upload’, ‘client-file-upload’);
    echo ‘</form>’;

    I am also putting this stuff in a plugin called “WP Client File Share”. should be up soon.

    I hope this helps someone.

    Great plugin.

    im looking for something similiar
    but where a user can post from front end as admin or regular user. in a tinymce editor and also upload documents and image and videos.

    do you have any idea regarding this or if its possible at all ?

    Thread Starter Aaron Reimann

    (@areimann)

    Hi
    that is a great link. have check taht and it would be good if they have it for post posting. thus user can add post to the blog for such “private” site .

    . however its seams hard to find a solution/tutorial/files for a site template that lets user post with tinymce editor and upload file/documents/image without ever needed to go to backend, and also attach it all to a post that can be displayed.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Front end upload works but needs field for Title’ is closed to new replies.