Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd another image js function
    text
    copied!<p>I'm using a WordPress plugin called <a href="http://perishablepress.com/user-submitted-posts/" rel="nofollow">User Submitted Posts</a>. The plugin allows for users to upload multiple photos. I have it set up so that users can upload 0 to a max of 3. When I try to upload more than one pic, however, only one of them gets posted.</p> <p>HTML:</p> <pre><code>&lt;div id="usp"&gt; &lt;form id="usp_form" method="post" enctype="multipart/form-data" action=""&gt; &lt;ul id="usp_list"&gt; &lt;li class="usp_title"&gt; &lt;label for="user-submitted-title" class="usp_label"&gt;Post Title&lt;/label&gt; &lt;div&gt; &lt;input class="usp_input" type="text" name="user-submitted-title" id="user-submitted-title" value="" /&gt; &lt;/div&gt; &lt;/li&gt; &lt;li class="usp_tags"&gt; &lt;label for="user-submitted-tags" class="usp_label"&gt;Post Tags &lt;small&gt;(separate tags with commas)&lt;/small&gt;&lt;/label&gt; &lt;div&gt; &lt;input class="usp_input" type="text" name="user-submitted-tags" id="user-submitted-tags" value="" /&gt; &lt;/div&gt; &lt;/li&gt; &lt;li class="usp_content"&gt; &lt;label for="user-submitted-content" class="usp_label"&gt;Post Content&lt;/label&gt; &lt;div&gt; &lt;textarea class="usp_textarea" name="user-submitted-content" id="user-submitted-content" rows="5"&gt;&lt;/textarea&gt; &lt;/div&gt; &lt;/li&gt; &lt;li class="usp_images"&gt; &lt;label for="user-submitted-image" class="usp_label"&gt;Upload an Image&lt;/label&gt; &lt;div id="usp_upload-message"&gt;&lt;/div&gt; &lt;div&gt; &lt;input class="usp_input usp_clone" type="file" size="25" id="user-submitted-image" name="user-submitted-image[]" /&gt; &lt;a href="#" id="usp_add-another"&gt;Add another image&lt;/a&gt; &lt;/div&gt; &lt;/li&gt; &lt;li class="usp_submit"&gt; &lt;input class="usp_input" type="submit" name="user-submitted-post" id="user-submitted-post" value="Submit Post" /&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/form&gt; </code></pre> <p>PHP:</p> <pre><code>if (!class_exists('Public_Submission_Form')) { class Public_Submission_Form { var $version = '1.0'; var $_post_meta_IsSubmission = 'is_submission'; var $_post_meta_Submitter = 'user_submit_name'; var $_post_meta_SubmitterUrl = 'user_submit_url'; var $_post_meta_SubmitterIp = 'user_submit_ip'; var $_post_meta_Image = 'user_submit_image'; var $_post_meta_ImageInfo = 'user_submit_image_info'; var $settings = null; function Public_Submission_Form() { register_activation_hook(__FILE__, array(&amp;$this, 'saveDefaultSettings')); add_action('admin_init', array(&amp;$this, 'checkForSettingsSave')); add_action('admin_menu', array(&amp;$this, 'addAdministrativeElements')); add_action('init', array(&amp;$this, 'enqueueResources')); add_action('parse_request', array(&amp;$this, 'checkForPublicSubmission')); add_action('parse_query', array(&amp;$this, 'addSubmittedStatusClause')); add_action('restrict_manage_posts', array(&amp;$this, 'outputUserSubmissionLink')); add_filter('the_author', array(&amp;$this, 'replaceAuthor')); add_filter('the_author_link', array(&amp;$this, 'replaceAuthorLink')); add_filter('post_stati', array(&amp;$this, 'addNewPostStatus')); add_shortcode('user-submitted-posts', array(&amp;$this, 'getPublicSubmissionForm')); } function addAdministrativeElements() { add_options_page(__('User Submitted Posts'), __('User Submitted Posts'), 'manage_options', 'user-submitted-posts', array(&amp;$this, 'displaySettingsPage')); } function addNewPostStatus($postStati) { $postStati['submitted'] = array(__('Submitted'), __('User submitted posts'), _n_noop('Submitted', 'Submitted')); return $postStati; } function addSubmittedStatusClause($wp_query) { global $pagenow; if (is_admin() &amp;&amp; $pagenow == 'edit.php' &amp;&amp; $_GET['user_submitted'] == '1') { set_query_var('meta_key', $this-&gt;_post_meta_IsSubmission); set_query_var('meta_value', 1); set_query_var('post_status', 'pending'); } } function checkForPublicSubmission() { if (isset($_POST['user-submitted-post']) &amp;&amp; ! empty($_POST['user-submitted-post'])) { $settings = $this-&gt;getSettings(); $title = stripslashes($_POST['user-submitted-title']); $content = stripslashes($_POST['user-submitted-content']); $authorName = stripslashes($_POST['user-submitted-name']); $authorUrl = stripslashes($_POST['user-submitted-url']); $tags = stripslashes($_POST['user-submitted-tags']); $category = intval($_POST['user-submitted-category']); $fileData = $_FILES['user-submitted-image']; $publicSubmission = $this-&gt;createPublicSubmission($title, $content, $authorName, $authorUrl, $tags, $category, $fileData); if (false == ($publicSubmission)) { $errorMessage = empty($settings['error-message']) ? __('An error occurred. Please go back and try again.') : $settings['error-message']; if( !empty( $_POST[ 'redirect-override' ] ) ) { $redirect = stripslashes( $_POST[ 'redirect-override' ] ); $redirect = add_query_arg( array( 'submission-error' =&gt; '1' ), $redirect ); wp_redirect( $redirect ); exit(); } wp_die($errorMessage); } else { $redirect = empty($settings['redirect-url']) ? $_SERVER['REQUEST_URI'] : $settings['redirect-url']; if (! empty($_POST['redirect-override'])) { $redirect = stripslashes($_POST['redirect-override']); } $redirect = add_query_arg(array('success'=&gt;1), $redirect); wp_redirect($redirect); exit(); } } } function checkForSettingsSave() { if (isset($_POST['save-post-submission-settings']) &amp;&amp; current_user_can('manage_options') &amp;&amp; check_admin_referer('save-post-submission-settings')) { $settings = $this-&gt;getSettings(); $settings['author'] = get_userdata($_POST['author']) ? $_POST['author'] : $settings['author']; $settings['categories'] = is_array($_POST['categories']) &amp;&amp; ! empty($_POST['categories']) ? array_unique($_POST['categories']) : array(get_option('default_category')); $settings['number-approved'] = is_numeric($_POST['number-approved']) ? intval($_POST['number-approved']) : - 1; $settings['redirect-url'] = stripslashes($_POST['redirect-url']); $settings['error-message'] = stripslashes($_POST['error-message']); $settings['min-images'] = is_numeric($_POST['min-images']) ? intval($_POST['min-images']) : $settings['max-images']; $settings['max-images'] = (is_numeric($_POST['max-images']) &amp;&amp; ($settings['min-images'] &lt;= $_POST['max-images'])) ? intval($_POST['max-images']) : $settings['max-images']; $settings['min-image-height'] = is_numeric($_POST['min-image-height']) ? intval($_POST['min-image-height']) : $settings['min-image-height']; $settings['min-image-width'] = is_numeric($_POST['min-image-width']) ? intval($_POST['min-image-width']) : $settings['min-image-width']; $settings['max-image-height'] = (is_numeric($_POST['max-image-height']) &amp;&amp; ($settings['min-image-height'] &lt;= $_POST['max-image-height'])) ? intval($_POST['max-image-height']) : $settings['max-image-height']; $settings['max-image-width'] = (is_numeric($_POST['max-image-width']) &amp;&amp; ($settings['min-image-width'] &lt;= $_POST['max-image-width'])) ? intval($_POST['max-image-width']) : $settings['max-image-width']; $settings['usp_name'] = stripslashes($_POST['usp_name']); $settings['usp_url'] = stripslashes($_POST['usp_url']); $settings['usp_title'] = stripslashes($_POST['usp_title']); $settings['usp_tags'] = stripslashes($_POST['usp_tags']); $settings['usp_category'] = stripslashes($_POST['usp_category']); $settings['usp_content'] = stripslashes($_POST['usp_content']); $settings['usp_images'] = stripslashes($_POST['usp_images']); $settings['upload-message'] = stripslashes($_POST['upload-message']); $settings['usp_form_width'] = stripslashes($_POST['usp_form_width']); $this-&gt;saveSettings($settings); wp_redirect(admin_url('options-general.php?page=user-submitted-posts&amp;updated=1')); } } function displaySettingsPage() { include ('views/settings.php'); } function enqueueResources() { wp_enqueue_script('usp_script', WP_PLUGIN_URL.'/'.basename(dirname(__FILE__)).'/resources/user-submitted-posts.js', array('jquery'), $this-&gt;version); wp_enqueue_style('usp_style', WP_PLUGIN_URL.'/'.basename(dirname(__FILE__)).'/resources/user-submitted-posts.css', false, $this-&gt;version, 'screen'); } function getPublicSubmissionForm($atts = array(), $content = null) { if ($atts === true) { $redirect = $this-&gt;currentPageURL(); } ob_start(); include (WP_PLUGIN_DIR.'/'.basename(dirname(__FILE__)).'/views/submission-form.php'); return ob_get_clean(); } function outputUserSubmissionLink() { global $pagenow; if ($pagenow == 'edit.php') { echo '&lt;a id="usp_admin_filter_posts" class="button-secondary" href="'.admin_url('edit.php?post_status=pending&amp;amp;user_submitted=1').'"&gt;'.__('User Submitted Posts').'&lt;/a&gt;'; } } function replaceAuthor($author) { global $post; $isSubmission = get_post_meta($post-&gt;ID, $this-&gt;_post_meta_IsSubmission, true); $submissionAuthor = get_post_meta($post-&gt;ID, $this-&gt;_post_meta_Submitter, true); if ($isSubmission &amp;&amp; ! empty($submissionAuthor)) { return $submissionAuthor; } else { return $author; } } function replaceAuthorLink($authorLink) { global $post; $isSubmission = get_post_meta($post-&gt;ID, $this-&gt;_post_meta_IsSubmission, true); $submissionAuthor = get_post_meta($post-&gt;ID, $this-&gt;_post_meta_Submitter, true); $submissionLink = get_post_meta($post-&gt;ID, $this-&gt;_post_meta_SubmitterUrl, true); if ($isSubmission &amp;&amp; ! empty($submissionAuthor)) { if ( empty($submissionLink)) { return $submissionAuthor; } else { return "&lt;a href='{$submissionLink}'&gt;{$submissionAuthor}&lt;/a&gt;"; } } else { return $authorLink; } } function saveDefaultSettings() { $settings = $this-&gt;getSettings(); if ( empty($settings)) { $currentUser = wp_get_current_user(); $settings = array(); $settings['author'] = $currentUser-&gt;ID; $settings['categories'] = array(get_option('default_category')); $settings['number-approved'] = -1; $settings['redirect-url'] = ''; //site_url(); $settings['error-message'] = __('There was an error. Please ensure that you have added a title, some content, and that you have uploaded only images.'); $settings['min-images'] = 0; $settings['max-images'] = 1; $settings['min-image-height'] = 0; $settings['min-image-width'] = 0; $settings['max-image-height'] = 500; $settings['max-image-width'] = 500; $settings['usp_name'] = 'show'; $settings['usp_url'] = 'show'; $settings['usp_title'] = 'show'; $settings['usp_tags'] = 'show'; $settings['usp_category'] = 'show'; $settings['usp_content'] = 'show'; $settings['usp_images'] = 'hide'; $settings['upload-message'] = ''; // 'Please select your image(s) to upload:'; $settings['usp_form_width'] = '300'; // in pixels $this-&gt;saveSettings($settings); } } function getSettings() { if ($this-&gt;settings === null) { $defaults = array(); $this-&gt;settings = get_option('User Submitted Posts Settings', array()); } return $this-&gt;settings; } function saveSettings($settings) { if (!is_array($settings)) { return; } $this-&gt;settings = $settings; update_option('User Submitted Posts Settings', $this-&gt;settings); } function createPublicSubmission($title, $content, $authorName, $authorUrl, $tags, $category, $fileData) { $settings = $this-&gt;getSettings(); $authorName = strip_tags($authorName); $authorUrl = strip_tags($authorUrl); $authorIp = $_SERVER['REMOTE_ADDR']; if (!$this-&gt;validateTitle($title)) { return false; } if (!$this-&gt;validateContent($title)) { return false; } if (!$this-&gt;validateTags($tags)) { return false; } $postData = array(); $postData['post_title'] = $title; $postData['post_content'] = $content; $postData['post_status'] = 'pending'; $postData['author'] = $settings['author']; $numberApproved = $settings['number-approved']; if ($numberApproved &lt; 0) {} elseif ($numberApproved == 0) { $postData['post_status'] = 'publish'; } else { $posts = get_posts(array('post_status'=&gt;'publish', 'meta_key'=&gt;$this-&gt;_post_meta_Submitter, 'meta_value'=&gt;$authorName)); $counter = 0; foreach ($posts as $post) { $submitterUrl = get_post_meta($post-&gt;ID, $this-&gt;_post_meta_SubmitterUrl, true); $submitterIp = get_post_meta($post-&gt;ID, $this-&gt;_post_meta_SubmitterIp, true); if ($submitterUrl == $authorUrl &amp;&amp; $submitterIp == $authorIp) { $counter++; } } if ($counter &gt;= $numberApproved) { $postData['post_status'] = 'publish'; } } $newPost = wp_insert_post($postData); if ($newPost) { wp_set_post_tags($newPost, $tags); wp_set_post_categories($newPost, array($category)); if (!function_exists('media_handle_upload')) { require_once (ABSPATH.'/wp-admin/includes/media.php'); require_once (ABSPATH.'/wp-admin/includes/file.php'); require_once (ABSPATH.'/wp-admin/includes/image.php'); } $attachmentIds = array(); $imageCounter = 0; for ($i = 0; $i &lt; count($fileData['name']); $i++) { $imageInfo = getimagesize($fileData['tmp_name'][$i]); if (false === $imageInfo || !$this-&gt;imageIsRightSize($imageInfo[0], $imageInfo[1])) { continue; } $key = "public-submission-attachment-{$i}"; $_FILES[$key] = array(); $_FILES[$key]['name'] = $fileData['name'][$i]; $_FILES[$key]['tmp_name'] = $fileData['tmp_name'][$i]; $_FILES[$key]['type'] = $fileData['type'][$i]; $_FILES[$key]['error'] = $fileData['error'][$i]; $_FILES[$key]['size'] = $fileData['size'][$i]; $attachmentId = media_handle_upload($key, $newPost); if (!is_wp_error($attachmentId) &amp;&amp; wp_attachment_is_image($attachmentId)) { $attachmentIds[] = $attachmentId; add_post_meta($newPost, $this-&gt;_post_meta_Image, wp_get_attachment_url($attachmentId)); $imageCounter++; } else { wp_delete_attachment($attachmentId); } if ($imageCounter == $settings['max-images']) { break; } } if (count($attachmentIds) &lt; $settings['min-images']) { foreach ($attachmentIds as $idToDelete) { wp_delete_attachment($idToDelete); } wp_delete_post($newPost); return false; } update_post_meta($newPost, $this-&gt;_post_meta_IsSubmission, true); update_post_meta($newPost, $this-&gt;_post_meta_Submitter, htmlentities(($authorName))); update_post_meta($newPost, $this-&gt;_post_meta_SubmitterUrl, htmlentities(($authorUrl))); update_post_meta($newPost, $this-&gt;_post_meta_SubmitterIp, $authorIp); } return $newPost; } function imageIsRightSize($width, $height) { $settings = $this-&gt;getSettings(); $widthFits = ($width &lt;= intval($settings['max-image-width'])) &amp;&amp; ($width &gt;= $settings['min-image-width']); $heightFits = ($height &lt;= $settings['max-image-height']) &amp;&amp; ($height &gt;= $settings['min-image-height']); return $widthFits &amp;&amp; $heightFits; } function validateContent($content) { return ! empty($content); } function validateTags($tags) { return true; } function validateTitle($title) { return ! empty($title); } function currentPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") { $pageURL .= "s"; } $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } } $publicSubmissionForm = new Public_Submission_Form(); include ('library/template-tags.php'); } </code></pre> <p>== Template Tags ==</p> <p>To display the images attached to user-submitted posts, use this template tag:</p> <p><code>&lt;?php post_attachments(); ?&gt;</code></p> <p>This template tag prints the URLs for all post attachments and accepts the following paramters:</p> <p><code>&lt;?php post_attachments($size, $beforeUrl, $afterUrl, $numberImages, $postId); ?&gt;</code></p> <p><code>$size = image size as thumbnail, medium, large or full -&gt; default = full</code> <code>$beforeUrl = text/markup displayed before the image URL -&gt; default = &lt;img src="</code> <code>$afterUrl = text/markup displayed after the image URL -&gt; default = " /&gt;</code> <code>$numberImages = the number of images to display for each post -&gt; default = false (display all)</code> <code>$postId = an optional post ID to use -&gt; default = uses global post</code></p> <p>Additionally, the following template tag returns an array of URLs for the specified post image:</p> <p><code>&lt;?php get_post_images(); ?&gt;</code></p> <p>This tag returns a boolean value indicating whether the specified post is a public submission: </p> <p><code>&lt;?php is_public_submission(); ?&gt;</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload