Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working with submitting posts from fronted which has some custom fields. I used shortcode API to use the form in any page/post. The code should be found bellow. But after submitting the form, nothing happened and the form returns. I need your kind help.

<?php
//Shortcode API
function apl_video(){
global $current_user;
get_currentuserinfo();
if (!isset($_POST['submit'])) {

?>
<form name="new_post" method="post" action="">

    <table border="0" align="center">
        <tr><td><label for="id_meta">Video Title</label></td><td><input id="post_title" tabindex="1" type="text" name="post_title"/></td></tr>

        <tr><td><label for="vid_wid">Video Width</label></td><td><input id="vid_wid" tabindex="2" type="text" name="vid_wid"/></td></tr>

        <tr><td><label for="vid_hei">Video Height</label></td><td><input id="vid_hei" tabindex="3" type="text" name="vid_hei"/></td></tr>

        <tr><td><label for="vid_ply">Autoplay</label></td><td><select tabindex="4" name="vid_ply" id="vid_ply">
        <option value="Yes">Yes</option>
        <option value="No">No</option>
        </select>
      </td></tr>

        <tr><td><label for="vid_ski">Skin</label></td><td><select tabindex="5" name="vid_ski" id="vid_ski">
        <option value="player1">Player 1</option>
        <option value="player2">Player 2</option>
        </select>
      </td></tr>


        <tr>
          <td><label for="vid_you">Youtube Video Link</label></td>
          <td><input id="vid_you" tabindex="6" type="text" name="vid_you"/></td>
        </tr>
        <tr>
          <td><label for="vid_mp4">MP4 Video Link</label></td>
          <td><input id="vid_mp4" tabindex="7" type="text" name="vid_mp4"/></td>
        </tr>
        <tr>
          <td><label for="vid_ogg">OGG Video Link</label></td>
          <td><input id="vid_hei" tabindex="8" type="text" name="vid_ogg"/></td>
        </tr>
        <tr>
          <td><label for="vid_web">WEBM Video Link</label></td>
          <td><input id="vid_hei" tabindex="9" type="text" name="vid_web"/></td>
        </tr>
        <tr>
          <td><label for="vid_pst">Poster Image Link</label></td>
          <td><input id="vid_hei" tabindex="10" type="text" name="vid_pst"/></td>
        </tr>
        <tr>

            <td>&nbsp;</td>

            <?php wp_nonce_field( 'new-post' ); ?>

            <td><input type="submit" value="Generate" name="submit"/></td>

        </tr>

    </table>



</form>

<?php }

else {
    $title      =   esc_attr($_POST['post_title']);
    $vid_wid    =   esc_attr($_POST['vid_wid']);
    $vid_hei    =   esc_attr($_POST['vid_hei']);
    $vid_ply    =   esc_attr($_POST['vid_ply']);
    $vid_ski    =   esc_attr($_POST['vid_ski']);
    $vid_you    =   esc_attr($_POST['vid_you']);
    $vid_mp4    =   esc_attr($_POST['vid_mp4']);
    $vid_web    =   esc_attr($_POST['vid_web']);
    $vid_ogg    =   esc_attr($_POST['vid_ogg']);
    $vid_pst    =   esc_attr($_POST['vid_pst']);


    global $user_ID;

    $new_post = array(
            'post_title'    => $title,
            'post_status'   => 'publish',
            'post_type'     => 'video',
            'post_date'     => date('Y-m-d H:i:s'),
            'post_author'   => $user_ID,
    );

    //save the new post
    $pid    =   wp_insert_post($new_post);

    // Do the wp_insert_post action to insert it
    do_action('wp_insert_post', 'wp_insert_post'); 

    add_post_meta($pid, 'vid_wid', $vid_pst, true);
    add_post_meta($pid, 'vid_hei', $vid_hei, true);
    add_post_meta($pid, 'vid_ply', $vid_ply, true);
    add_post_meta($pid, 'vid_ski', $vid_ski, true);
    add_post_meta($pid, 'vid_you', $vid_you, true);
    add_post_meta($pid, 'vid_ogg', $vid_ogg, true);
    add_post_meta($pid, 'vid_web', $vid_web, true);
    add_post_meta($pid, 'vid_mp4', $vid_mp4, true);
    add_post_meta($pid, 'vid_pst', $vid_pst, true);



    //Result
    echo 'Done';



        }
}


add_shortcode( 'wpvideogen', 'apl_video' );
share|improve this question
    
Can you explain how this form submission works? Is it using wp_ajax ? –  ifdion Aug 12 '12 at 22:18
add comment

1 Answer

up vote 2 down vote accepted

Your code works fine for me as-is, assuming you actually have a video post type, however there are a couple of things you can correct-

  1. shortcodes should return their value, otherwise you will see shortcode content appear before any post content, not where you actually placed the shortcode.

  2. do_action('wp_insert_post', 'wp_insert_post'); is unnecessary, wp_insert_post already does that action within the function, so you're invoking it twice.

share|improve this answer
    
Thanks Milo but I just tried to get post id using $pid = wp_insert_post($new_post); and used add_shortcode( 'wpvideogen', 'apl_video' ); to call the shortcode. Please help me to recognize if I did any mistake. –  Enthusiast Aug 12 '12 at 15:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.