• Resolved alucidwake

    (@alucidwake)


    To start, thank you for such a great plugin – it’s perfect for my needs and relatively easy to figure out how to use for a beginner. For reference, I’m using Contact Form 7. That said, I was hoping to get an answer about a problem I have been trying to figure out for the past day without luck:

    Is it possible to insert a default string of text at the beginning of the post?

    For example, if I have the form set up so it asks a question, I would like to structure the way the answer is written. So if I ask “What is a cow” (with a hidden post_title), my answer box in the form is “A cow is _____________”. When the user hits submit, what will be posted is just what was typed into the “___________”, but I want the full “A cow is _____________” to be posted.

    I’ve messed around quite a great deal within the plugin.php and as well classes.php and functions.php in CF7. Any assistance that could be offer would be immensely appreciated.

    Thank you,
    Nick

    http://wordpress.org/extend/plugins/form-to-post/

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Michael Simpson

    (@msimpson)

    I suggest to try to manage this on the front end with Javascript:
    – add a new field called “answer” where the user types in an answer.
    – Make post_content a hidden field (I think you need Contact Form 7 Modules installed to have hidden fields available)
    – In CF7, give those fields their own id (like “answer” and “post_content”)
    – Add jQuery to the page that listens to change in “answer” and populates “post_content” with the full text. I think it would look like this (haven’t tested it):

    <script type="text/javascript">
        jQuery('#answer').change(function() {
            var answer = jQuery('#answer').val();
            jQuery('#post_content').val('A cow is ' + answer);
        });
    </script>
    Thread Starter alucidwake

    (@alucidwake)

    Hi Michael, thanks for such a quick response. So, I edited my form to have a hidden post_content and to have the text box input ‘answer’. When I put the script in my index.php and input a new answer, it outputs the last answer I put in before I made the changes. So it seems like the $post_content value in the CF7/FtP script is not being updated by the new ‘answer’ inputs… Am I placing the script in the wrong place?

    Plugin Author Michael Simpson

    (@msimpson)

    I’m not suggesting you many any changes to any .php file. This goes into the post via the WordPress editor in “Text” edit mode.

    Thread Starter alucidwake

    (@alucidwake)

    Aah, ok. So, my “question” isn’t in a post, but in a text widget. When I put the script in the same widget with the question, the same problem persists… Here’s a link to the website I’m working on if it would help. Thank you.

    Plugin Author Michael Simpson

    (@msimpson)

    I suppose you should put it in the same place you put the form short code. I would put it right after it.

    Thread Starter alucidwake

    (@alucidwake)

    While there is a chance I’m defining my id’s wrong ( [hidden hidden-666 id:post_content “post_content”] [text text-257 id:answer] ), I’ve been unable to make this solution work. I may be taking a huge shot in the dark saying this, but it seems like the problem with it may be that the post_content needs a default value when submitted, not when posted.

    So, as it wouldn’t be a problem to do so logistically, it will be the only question asked, I’m looking to find a way to insert the string in the back-end, either in the CF7 or FtP files, though I am finding that theoretically easy but practically difficult as I am unfamiliar with php. I’ll keep tinkering, but if you have any suggestions they would be very welcome.

    Thanks again for all the help you’ve provided thus far!

    Plugin Author Michael Simpson

    (@msimpson)

    I played with this and got a working example.

    Notes:
    1. I found the easiest place to put the javascript is in the CF7 form definition. Then all you have to do is put the CF7 short code in a post and you get the Javascript with it.
    2. I found the CF7 Modules Hidden fields NOT useful, so I got rid of that. I let the Javascript dynamically create the hidden field
    3. I did not test it in a widget, but I think it should work.

    This is my CF7 form definition:

    Title [text* post_title] <br/>
    
    A Cow is: (required) <br/>
    [textarea* answer id:answer] <br/>
    
    [submit "Post"] <br/>
    
    <script type="text/javascript">
        (function($) {
            $(document).ready(function() {
                $('#answer').change(function() {
                    var answer = 'A cow is '+ $('#answer').val();
                    if ($('#post_content').length > 0) {
                        $('#post_content').val(answer);
                    }
                    else {
                        var postContentInput = $("<input>").attr("type", "hidden").attr("name", "post_content").val(answer);
                        $(this).parents('form:first').append($(postContentInput));
                    }
                });
            })
        })(jQuery);
    </script>
    Thread Starter alucidwake

    (@alucidwake)

    Hi Michael,

    Great news! Putting the script inside the CF7 code absolutely did the trick. Thank you so much for all of your help regarding this issue, which is now resolved! Best, Nick

    Hello Michael, this is on posting to WordPress from a CF7 form, with skip_mail = true.

    My question to you is concerned with how to post all the form fields from a custom, say, resume or job vacancy form rather than only your two post_title and post_content fields, with an optional post_category_name?

    Plugin Author Michael Simpson

    (@msimpson)

    I suggest to adapt the code above. To do so, let’s assume you have fields named field1, field2, field3. And let’s assume you want to put the info into your post like:

    Answer for Field 1 is: <field1-value>
    Answer for Field 2 is: <field2-value>
    Answer for Field 3 is: <field3-value>

    I would code it like this (haven’t tested) in your CF7 Definition:

    First, set up your CF7 form fields. Might be different that this. Key thing is to have an “id” for each field.

    Title [text* post_title] <br/>
    Field1: [text field1 id:field1] <br/>
    Field2: [text field2 id:field2] <br/>
    Field3: [text field3 id:field3] <br/>
    [submit "Post"] <br/>

    Now add in the JavaScript

    <script type="text/javascript">
        (function($) {
            $(document).ready(function() {
    
                // CHANGE: list all field id's here
                $('#field1, #field2, #field3').change(function() {  
    
                  // CHANGE: Format how you want the fields to come out in the post
                   var content =
                         "Answer for Field 1 is: "+ $('#field1').val() + "<br/>" +
                         "Answer for Field 2 is: "+ $('#field2').val() + "<br/>" +
                         "Answer for Field 3 is: "+ $('#field3').val() + "<br/>";
    
                    if ($('#post_content').length > 0) {
                        $('#post_content').val(content);
                    }
                    else {
                        var postContentInput = $("<input>").attr("type", "hidden").attr("name", "post_content").val(answer);
                        $(this).parents('form:first').append($(postContentInput));
                    }
                });
            })
        })(jQuery);
    </script>

    Hello Michael, this works OK except that it appears in Flamingo as an inbound message (as yet retaining skip_email=false including all-fields) not as a blog post, which is what I want to achieve.

    It seems to me there still needs to be an explicit post_content field to enable the post, into which all-fields are aggregated.

    Thanks again for your help to this point. Getting there . . .

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Append text string to post?’ is closed to new replies.