Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

display input form if custom field is not set WordPress

  • SOLVED

I have a number of custom fields on posts that will be filled in by amazon mechanical Turk workers, instead of giving them dashboard access I would like them to be able to submit the results on the page

Please can you put together the code (not link me to other websites) to display a field on the page with a submit button if a custom field (name specified in code) is not set. If the field is set, it should display the contents.

Answers (4)

2012-12-07

Kyle answers:

There is obviously more work required if you want to actually submit the entry, and update the post based on the submission. Here is the part you are looking for though:

function display_form(){

global $post;

$your_field = get_post_meta($post->ID, 'your-custom-field', TRUE);

if (empty($your_field)){

echo '<form><input class="input-text"><br><input type="button" value="submit" /></form>' ;

}else{

echo $your_field;

}}

2012-12-07

Sébastien | French WordpressDesigner answers:

the name of your custom field is "setting" for example

if (get_post_meta($post->ID, "settings", true) == 0) {
echo "here is my content";
} else {
echo "no content here";
}

2012-12-07

phppoet answers:

Put this code in main output file . try it and tell me if there is any error. I m here to help you.




global $post;

$custom_field= get_post_meta($post->ID, 'your-custom-field', TRUE);

if (!isset($custom_field)) {

?> <form>

//Enter the form code here //

</form>
<?php
}else { the_content();
}

2012-12-07

Arnav Joy answers:

try this

<?php

$custom_fields = array('field1','field2','field3');
$current_page_id = get_the_ID();

if( $_POST['submit']){
foreach( $custom_fields as $cf ) {
if( $_POST[$cf] != '' )
update_post_meta( $current_page_id , $cf , $_POST[$cf] );
}

}

?>


<form method="post" action="<?php echo get_permalink($current_page_id);?>">
<?php
foreach( $custom_fields as $cf ) {
$custom_field_value = '';

$custom_field_value = get_post_meta($current_page_id,$cf,true);

if( !empty($custom_field_value) )
echo 'Field '.$cf.' has value = '.$custom_field_value.'<br>';
else
echo '<input type="text" name="'.$cf.'" >' ;
}

?>

<input type="submit" name="submit" />
</form>