Skip to content

Instantly share code, notes, and snippets.

@wyrfel
Created March 1, 2011 10:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wyrfel/848960 to your computer and use it in GitHub Desktop.
Save wyrfel/848960 to your computer and use it in GitHub Desktop.
WP custom post type creation form with customized file upload facility
<?php
global $user_identity;
//$user_submissions = get_posts();
if ($user = wp_get_current_user()) {
$author = array('id' => $user->ID);
} else {
$commenter = wp_get_current_commenter();
$author = array(
'author' => $commenter['comment_author'],
'email' => $commenter['comment_author_email'],
'url' => $commenter['comment_author_url'],
'ip' => preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] ),
);
}
$aria_req = " aria-required='true'";
$required_text = sprintf( ' ' . __('(%s marks required fields)'), '<span class="required">*</span>' );
$defaults = array(
'must_log_in' => '<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to enter in this contest.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post->ID ) ) ) ) . '</p>',
'has_submission' => '<p class="has-submission">You have already made a submission for this contest. Only a single submission per person is permitted.</p>',
'logged_in_as' => '<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post->ID ) ) ) ) . '</p>',
'upload_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ).$required_text.'</p>',
'upload_notes_after' => '<p class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>',
'title' => __( 'submit your photo' ),
'label_submit' => __( 'Upload' ),
);
$args = apply_filters( 'photonomy_form_defaults', $defaults );
?>
<form enctype="multipart/form-data" method="post" action="<?php echo PHOTONOMY_URL."upload.php"; ?>" class="photonomy-upload-form type-form validate" id="photonomy_submission_form">
<?php
$max_upload_size = $this->convert_bytes_to_hr($this->get_max_upload_size());
?>
<h3><?php echo $args['title']; ?></h3>
<div id="photonomy-upload-notice">
<?php if (isset($errors['upload_notice']) ) { ?>
<?php echo $errors['upload_notice']; ?>
<?php } ?>
</div>
<?php
$errors = array(
__('Sorry, you must be logged in to post a comment.'), //1
__('Please fill the required fields (name, email).'), //2
__('Please enter a valid email address.'), //4
__('Error: Sorry, we could not store your photo (only JPEG, PNG and GIF files are allowed).'), //8
__('Error: Sorry, we could not store your submission.'), //16
__('Please select a photo to upload.'), //32
__('You have already made a submission for this contest. Only a single submission per person is permitted.'), //64
);
$v = (isset($_REQUEST['errors'])) ? (int)$_REQUEST['errors'] : 0;
$i = 0;
while (pow(2,$i) <= $v) {
$t = pow(2,$i);
if ((int)($t & $v) == $t) {
?>
<div class="photonomy-error"><?php echo $errors[$i]; ?></div>
<?php
}
$i++;
}
?>
<?php
$messages = array(
__('Your photo has been submitted to the contest.'), //1
__('It is pending approval and will become visible after it has been approved.'), //2
);
$v = (isset($_REQUEST['messages'])) ? (int)$_REQUEST['messages'] : 0;
$i = 0;
while (pow(2,$i) <= $v) {
$t = pow(2,$i);
if ((int)($t & $v) == $t) {
?>
<div class="photonomy-notice"><?php echo $messages[$i]; ?></div>
<?php
}
$i++;
}
if ( !is_user_logged_in() && ($this->options['require_login'] || 'private' == $post->post_status)) {
?>
<?php echo $args['must_log_in']; ?>
<?php
/* } else if ($this->options['single_submission'] && $this->author_has_submission($post->ID, $author)) {
?>
<?php echo $args['has_submission']; ?>
<?php*/
} else if ( is_multisite() && !is_upload_space_available() ) {
echo '<p>'.sprintf( __( 'Sorry, you have filled your storage quota (%s MB).' ), get_space_allowed() ).'</p>';
} else {
?>
<div id="photonomy_upload_ui">
<?php
if ( is_user_logged_in() ) {
echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $author, $user_identity );
do_action( 'comment_form_logged_in_after', $author, $user_identity );
} else {
?>
<?php echo $args['upload_notes_before']; ?>
<p class="upload-form-author">
<label for="photonomy_author"><?php _e('Name'); ?></label> <span class="required">*</span>
<input type="text" name="photonomy_author" id="photonomy_author" value="<?php echo esc_attr( $author['author'] ); ?>" <?php echo $aria_req; ?> />
</p>
<p class="upload-form-email">
<label for="photonomy_email"><?php _e('Email'); ?></label> <span class="required">*</span>
<input type="text" name="photonomy_email" id="photonomy_email" value="<?php echo esc_attr( $author['email'] ); ?>" <?php echo $aria_req; ?> />
</p>
<?php
}
?>
<p id="async-upload-wrap" class="upload-form-file">
<label for="photonomy_photo"><?php _e('Photo'); ?></label> <span class="required">*</span>
<input type="file" size="15" name="photonomy_photo" id="photonomy_photo" value="" <?php echo $aria_req; ?> />
<span class="photonomy-upload-size">
<?php echo sprintf( __( 'Maximum upload file size: %s' ), $max_upload_size.'B' ); ?>
</span>
</p>
<p class="upload-form-title">
<label for="photonomy_title"><?php _e('Title'); ?></label>
<input type="text" name="photonomy_title" id="photonomy_title" <?php echo $aria_req; ?> />
</p>
<p class="upload-form-description">
<label for="photonomy_content"><?php _e('Description'); ?></label>
<textarea name="photonomy_content" id="photonomy_content"></textarea>
</p>
<?php echo $args['upload_notes_after']; ?>
<p class="form-submit">
<input type="hidden" name="post_id" value="<?php echo $post->ID; ?>" />
<input type="hidden" name="redirect_to" value="<?php echo remove_query_arg(array('photonomy_cycle', 'errors', 'messages')); ?>" />
<input type="submit" class="button" name="photonomy_upload" value="<?php echo esc_attr( $args['label_submit'] ); ?>" />
</p>
</div>
<?php
}
?>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment