WordPress.org

Ready to get started?Download WordPress

Forums

Form Manager
Post Form Data to Custom Post Types, Taxonomies, and Meta (1 post)

  1. UaMV
    Member
    Posted 1 year ago #

    i think this is a great form plugin. However, i wanted the additional functionality of posting form data to custom post types, custom taxonomies, and custom meta fields. i have developed the following for such purposes. i would love to see something similar to this included in a future revision of WordPress Form Manager.

    Maybe not the cleanest code or implementation, but it works for our purposes. Thought i'd share. Enjoy!

    Edits to db.php:

    Add following code to $this->formSettingsKeys array in function initDefaultSettings()

    'publish_post_type' => '',
    'publish_post_meta' => '',
    'publish_post_taxonomies' => '',

    Add following code to $sql table definition in function setupFormManager()

    'publish_post_type' TEXT NOT NULL,
    'publish_post_meta' TEXT NOT NULL,
    'publish_post_taxonomies' TEXT NOT NULL,

    Edits to pages/editformadv.php:

    Add following to section Process Settings Changes

    $formInfo['publish_post_type'] 		= $_POST['publish_post_type'];
    $formInfo['publish_post_meta']		= $_POST['publish_post_meta'];
    $formInfo['publish_post_taxonomies'] = $_POST['publish_post_taxonomies'];

    Add following after table row for Post Category

    <?php
    	helper_option_field('publish_post_type', __("Publish as post type", 'wordpress-form-manager'), get_post_types( array( 'public' => true ), 'names' ), $form['publish_post_type'] );
    	helper_text_field('publish_post_taxonomies', __("Publish with these taxonomies", 'wordpress-form-manager'), $form['publish_post_taxonomies'], __("@TAX@taxname@AS@taxterm@AND@taxterm@TAX@taxname@AS@taxterm", 'wordpress-form-manager') );
    	helper_text_field('publish_post_meta', __("Publish with these custom meta values", 'wordpress-form-manager'), $form['publish_post_meta'], __("@META@metaname@AS@metavalue@AND@metavalue@META@metaname@AS@metavalue", 'wordpress-form-manager') );
    ?>

    Edits to helpers.php:

    Added to $newPost array in function fm_helper_publishPost

    'post_type' => $formInfo['publish_post_type'] == "" ? 'post' : $formInfo['publish_post_type'],

    Added before $postData['post_id'] in same function

    $postTaxonomies = $parser->parse($formInfo['publish_post_taxonomies']);
    	$postTaxonomies_raw = sprintf($postTaxonomies, $formInfo['publish_post_taxonomies']);
    	if ( '' != $postTaxonomies_raw ) {
    		$postTaxonomies_array = fm_helper_setTaxArray( preg_replace('/\s/', '', $postTaxonomies_raw) );
    		foreach ($postTaxonomies_array as $name => $terms) {
    			wp_set_object_terms( $postID, $terms, $name );
    		}
    	}
    
    	$postMeta = $parser->parse($formInfo['publish_post_meta']);
    	$postMeta_raw = sprintf($postMeta, $formInfo['publish_post_meta']);
    
    	if ( '' != $postMeta_raw ) {
    		$postMeta_array = fm_helper_setMetaArray( $postMeta_raw );
    		foreach ($postMeta_array as $meta) {
    			add_post_meta( $postID, $meta[key], $meta[value] );
    		}
    	}

    Added at end of file

    function fm_helper_setTaxArray( $formTaxData ) {
    	$taxarray = explode( "@TAX@", $formTaxData );
    
    	foreach ( $taxarray as $tax ) {
    		$nametermarray = explode( "@AS@", $tax );
    		$termstring = str_replace("@AND@", ",", $nametermarray[1]);
    		$termsarray = explode( ",", $termstring );
    		$taxarray_keyed[$nametermarray[0]] = $termsarray;
    	}
    	return $taxarray_keyed;
    }
    
    function fm_helper_setMetaArray( $formMetaData ) {
    	$metaarray_keyed = array();
    	$metaarray = explode( "@META@", $formMetaData );
    	foreach ( $metaarray as $meta ) {
    		$keyvaluearray = explode( "@AS@", $meta );
    		$valuesarray = explode( "@AND@", $keyvaluearray[1] );
    		foreach ( $valuesarray as $value ) {
    			array_push( $metaarray_keyed, array( 'key' => $keyvaluearray[0], 'value' => $value ) );
    		}
    	}
    
    	return $metaarray_keyed;
    }

    http://wordpress.org/extend/plugins/wordpress-form-manager/

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic