Auto submit wordpress admin form

dthardy
dthardy used Ask the Experts™
on
I have a wordpress form from a custom theme that I have to submit daily.  I would like to automate this somehow.

The form is a simple input type="file" with a submit button.  The form id="import-upload-form" and the action="admin.php?page=app-importer&step=1&_wpnonce=2f4b58c58b".

I would like to :

a: insert the value for input type="file" (can be static or dynamic)
b: automatically submit the form once daily

Is this possible?

I have enclosed the key element of the source for the admin page with the sitename changed for security purposes.  If you need additional information please do not hesitate to tell me what you need in the comments.

I appreciate your help in advance.

<!DOCTYPE html>
<!--[if IE 8]>
<html xmlns="http://www.w3.org/1999/xhtml" class="ie8"  dir="ltr" lang="en-US" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml">
<![endif]-->
<!--[if !(IE 8) ]><!-->
<html xmlns="http://www.w3.org/1999/xhtml"  dir="ltr" lang="en-US" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml">
<!--<![endif]-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>CSV Importer WordPress</title>
<script type="text/javascript">
addLoadEvent = function(func){if(typeof jQuery!="undefined")jQuery(document).ready(func);else if(typeof wpOnload!='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}};
var userSettings = {
		'url': '/',
		'uid': '1',
		'time':'1337807331'
	},
	ajaxurl = 'http://website.com/wp-admin/admin-ajax.php',
	pagenow = 'clipper_page_app-importer',
	typenow = '',
	adminpage = 'clipper_page_app-importer',
	thousandsSeparator = ',',
	decimalPoint = '.',
	isRtl = 0;
</script>

<script type="text/javascript">
var loadAjaxUpload = function() {
jQuery('.upload_button').each(function(){
        var clickedObject = jQuery(this);

        var clickedID = jQuery(this).attr('id');
        new AjaxUpload(clickedID, {
                  action: 'http://website.com/wp-admin/admin-ajax.php',
                  name: clickedID, // File upload name
                  data: { // Additional data to send
                                action: 'smart_ajax_post_action',
                                type: 'upload',
                                data: clickedID
                                                                , page: 'admin.php' },
                  autoSubmit: true, // Submit file after selection
                  responseType: 'json',
                  onChange: function(file, extension){},
                  onSubmit: function(file, extension){
                          if (! (extension && /^(jpg|png|jpeg|gif)$/i.test(extension))){
              // extension is not allowed
              alert('Error: invalid file extension');
              // cancel upload
              return false;
                } 
                                jQuery('#uploadtext_'+clickedID).text('Uploading'); // change button text, when user selects file	
                                this.disable(); // If you want to allow uploading only 1 file at time, you can disable upload button
                                interval = window.setInterval(function(){
                                        var text = jQuery('#uploadtext_'+clickedID).text();
                                        if (text.length < 13){	
                                                jQuery('#uploadtext_'+clickedID).text(text + '.'); 
                                        }
                                        else { 
                                                jQuery('#uploadtext_'+clickedID).text('Uploading'); 
                                        } 
                                }, 200); 
                  },
                  onComplete: function(file, response) {
                        window.clearInterval(interval);
                        jQuery('#uploadtext_'+clickedID).text('');	
                        this.enable(); // enable upload button

                        // If there was an error
                        if(response.error ){
                                var buildReturn = '<span class="upload-error">' + response.error + '</span>';
                                jQuery(".upload-error").remove();
                                clickedObject.parent().after(buildReturn);
                        }
                        else{ 
                                var isSlider = jQuery("#" + clickedID + "_type").val();
                                if(isSlider != 'upload') { 
                                        var buildReturn = '<img class="smartBreadcrumbsPreviewThumb" id="image_'+clickedID+'" src="http://website.com/wp-content/plugins/smart-breadcrumbs//thumb.php?src='+response.url+'&w=120&h=60&zc=1" />';
                                        jQuery("#reset_" + clickedID).show();
                                        jQuery("#uploaded_image_" + clickedID).attr("href",response.url);
                                        jQuery("#uploaded_image_" + clickedID).html(buildReturn);
                                        jQuery("#image_" + clickedID).fadeIn();
                                        jQuery("#uploaded_image_" + clickedID).fadeIn();
                                        clickedObject.next('span').fadeIn();
                                }
                                clickedObject.parent().prev('input').val(response.url);
                                jQuery(".upload-error").remove();


                                if(isSlider == 'upload') 
                                {
                                        //multi file add
                                        // add file to the list

                                        var CounFiles = jQuery("." + clickedID + "_img_count").val();
                                        CounFiles++;
                                        jQuery("." + clickedID + "_img_count").val(CounFiles);

                                        var ImgDetails = '<input type="hidden" name="'+clickedID+'_sliderdata['+CounFiles+'][img]" value="'+response.url+'" />';
                                        ImgDetails += '<table class="added_image"><tr><td width="60px" valign="top">';
                                        ImgDetails += '<img src="http://website.com/wp-content/plugins/smart-breadcrumbs//thumb.php?src='+response.url+'&w=120&h=60&zc=1" target="_blank" alt="" />';
                                        ImgDetails += '</td><td valign="top">';
                                        ImgDetails += '<div>' + file + '</div>';

                                        for ( var i in response.fields.fields )
                                        { 
                                                var field = response.fields.fields[i]; 
                                                var field_id = field['id'];
                                                var field_std = field['std'];
                                                var field_desc = field['desc'];

                                                if(field['type'] == 'text') {
                                                        ImgDetails += '<div><input type="text" name="'+clickedID+'_sliderdata['+CounFiles+']['+field_id+']" value="' + field_std + '" class="slider_text slider_'+field_id+'" title="'+field_desc+'"  /></div>';  
                                                } else if (field['type'] == 'textarea') {
                                                        ImgDetails += '<div><textarea name="'+clickedID+'_sliderdata['+CounFiles+']['+field_id+']" class="slider_textarea slider_'+field_id+'" title="'+field_desc+'" >' + field_std + '</textarea></div>';  
                                                } else if (field['type'] == 'select') {
                                                        ImgDetails += '<div><select name="'+clickedID+'_sliderdata['+CounFiles+']['+field_id+']" class="slider_select slider_'+field_id+'" >';

                                                        for ( var key in field['options'] )
                                                        {
                                                                var selected = '';
                                                                if(key == field_std) selected = ' selected="selected"';
                                                                ImgDetails += '<option '+ selected +' value="' + key + '" >' + field["options"][key] + '</option>';
                                                        }
                                                        ImgDetails += '</select></div>';  
                                                }
                                        }

                                        ImgDetails += '</td></tr></table>';

                                        var FilesList = jQuery('#files_' + clickedID );
                                        jQuery('<li class="ui-state-default"></li>').prependTo(FilesList).html(ImgDetails);

                                    }

                            }		  	
                      }			  
            });		
    });


    jQuery('.reset_button').click(function(){
        var clickedObject = jQuery(this);
        var clickedID = jQuery(this).attr('id');
        var theID = jQuery(this).attr('title');				

        var ajax_url = 'http://website.com/wp-admin/admin-ajax.php';

        var data = {
                action: 'smart_ajax_post_action',
                type: 'image_reset',
                data: theID
                        };

        jQuery.post(ajax_url, data, function(response) {
                var image_to_remove = jQuery('#image_' + theID);
                var button_to_hide = jQuery('#reset_' + theID);
                image_to_remove.fadeOut(500,function(){ jQuery(this).remove(); });
                button_to_hide.fadeOut();
                clickedObject.parent().prev('input').val('');	
        });

        return false; 
    });
}

jQuery(document).ready(function(){
    loadAjaxUpload();
});
</script>
    
</head>
<body class="wp-admin no-js  clipper_page_app-importer admin-bar branch-3-3 version-3-3-1 admin-color-fresh">
<script type="text/javascript">document.body.className = document.body.className.replace('no-js','js');</script>

<div id="wpwrap">

<div id="adminmenuback"></div>
<div id="adminmenuwrap">

</div>
<div id="wpcontent">


<div id="wpbody">

<div id="wpbody-content">
		<div id="screen-meta" class="metabox-prefs">

			<div id="contextual-help-wrap" class="hidden no-sidebar">
				<div id="contextual-help-back"></div>
				<div id="contextual-help-columns">
					<div class="contextual-help-tabs">
						<ul>
												</ul>
					</div>

					
					<div class="contextual-help-tabs-wrap">
											</div>
				</div>
			</div>
				</div>
		<div class='update-nag'><a href="http://codex.wordpress.org/Version_3.3.2">WordPress 3.3.2</a> is available! <a href="http://website.com/wp-admin/update-core.php">Please update now</a>.</div><div class="updated fade"><p>One or more plugins have been activated or deactivated, please <input type="button" class="button " value="empty the page cache" onclick="document.location.href='admin.php?page=w3tc_general&amp;w3tc_flush_pgcache&amp;_wpnonce=5e5bdecc2a';" /> and check the <input type="button" class="button " value="minify settings" onclick="document.location.href='admin.php?page=w3tc_general&amp;w3tc_hide_note&amp;note=plugins_updated&amp;redirect=admin.php%3Fpage%3Dw3tc_minify&amp;_wpnonce=5e5bdecc2a';" /> to maintain the desired user experience. <input type="button" class="button " value="Hide this message" onclick="document.location.href='admin.php?page=w3tc_general&amp;w3tc_hide_note&amp;note=plugins_updated&amp;_wpnonce=5e5bdecc2a';" /></p></div><div class='wrap'>
<div id="icon-tools" class="icon32"><br /></div><h2>CSV Importer</h2>
<div class="narrow"><p>Below you will find a tool which allows you to import content from other systems via a CSV (comma-separated values) file, which can be edited using a program like Excel. Note that the file must be in the correct format for the import tool to work. You will find an example .csv file in the "examples" theme folder.</p><p>Choose a CSV file to upload, then click "Upload file and import".</p><form enctype="multipart/form-data" id="import-upload-form" method="post" action="admin.php?page=app-importer&amp;step=1&amp;_wpnonce=2f4b58c58b">
<p>
<label for="upload">Choose a file from your computer:</label> (Maximum size: 8MB)
<input type="file" id="upload" name="import" size="25" />
<input type="hidden" name="action" value="save" />
<input type="hidden" name="max_file_size" value="8388608" />
</p>
<p class="submit"><input type="submit" name="submit" id="submit" class="button" value="Upload file and import"  /></p></form>
</div></div>

<div class="clear"></div></div><!-- wpbody-content -->
<div class="clear"></div></div><!-- wpbody -->
<div class="clear"></div></div><!-- wpcontent -->

<div id="footer">
<p id="footer-left" class="alignleft"><span id="footer-thankyou">Thank you for creating with <a href="http://wordpress.org/">WordPress</a>.</span></p>
<p id="footer-upgrade" class="alignright"><strong><a href="http://website.com/wp-admin/update-core.php">Get Version 3.3.2</a></strong></p>
<div class="clear"></div>
</div>

		
<script type='text/javascript'>
/* <![CDATA[ */
var commonL10n = {"warnDelete":"You are about to permanently delete the selected items.\n  'Cancel' to stop, 'OK' to delete."};/* ]]> */
</script>
<script type='text/javascript' src='http://website.com/wp-admin/load-scripts.php?c=0&amp;load=admin-bar,hoverIntent,common,jquery-color,jquery-ui-core,jquery-ui-widget,jquery-ui-mouse,jquery-ui-sortable,postbox,jquery-ui-tabs,jquery-form&amp;ver=a0633d29cb77c0c521f0e7006128a7d1'></script>

<div class="clear"></div></div><!-- wpwrap -->
<script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Information Systems Manager
Top Expert 2012
Commented:
Your problem is that for security reasons javascript is not allowed to set the value of an input of type file, so I think that you are out of luck.

Author

Commented:
Is there a way to do this with out the use of the form?

Possibly have the file stored on the server locally and run the installer via a script?

Any ideas how to do this?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial