我应该在这张前端邮寄表格的邮寄动作中写些什么?

时间:2012-01-26 作者:janoChen

我在跟踪this 构建前端表单的教程:

<?php
/**
 *
 * New Post Form for Custom Post Types for the Frontend of Your Site
 * By Jared Williams - http://new2wp.com
 *
 * Last Updated: 8/30/2010
 */

// Check if the form was submitted
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] )) {

    // Do some minor form validation to make sure there is content
    if (isset ($_POST[\'title\'])) { 
        $title =  $_POST[\'title\']; 
    } else { 
        echo \'Please enter a title\';
    }
    if (isset ($_POST[\'description\'])) { 
        $description = $_POST[\'description\']; 
    } else { 
        echo \'Please enter the content\'; 
    }
    $tags = $_POST[\'post_tags\'];

    // Add the content of the form to $post as an array
    $post = array(
        \'post_title\'    => $title,
        \'post_content\'  => $description,
        \'post_category\' => $_POST[\'cat\'],  // Usable for custom taxonomies too
        \'tags_input\'    => $tags, 
        \'post_status\'   => \'publish\',           // Choose: publish, preview, future, etc.
        \'post_type\'     => $_POST[\'post_type\']  // Use a custom post type if you want to
    );
    wp_insert_post($post);  // Pass  the value of $post to WordPress the insert function
                            // http://codex.wordpress.org/Function_Reference/wp_insert_post
    wp_redirect( home_url() ); // redirect to home page after submit

} // end IF
// Do the wp_insert_post action to insert it
do_action(\'wp_insert_post\', \'wp_insert_post\'); 
?>

<!-- New Post Form -->
<div id="postbox">
    <form id="new_post" name="new_post" method="post" action="">
        <p><label for="title">Title</label><br />
        <input type="text" id="title" value="" tabindex="1" size="20" name="title" />
        </p>
        <p><label for="description">Description</label><br />
        <textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
        </p>
        <p><?php wp_dropdown_categories( \'show_option_none=Category&tab_index=4&taxonomy=category\' ); ?></p>
        <p><label for="post_tags">Tags</label>
            <input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>
        <p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>

        <input type="hidden" name="post_type" id="post_type" value="post" />
        <input type="hidden" name="action" value="post" />
        <?php wp_nonce_field( \'new-post\' ); ?>
    </form>
</div>
<!--// New Post Form -->
现在行动部分:

    <form id="new_post" name="new_post" method="post" action="">
为空

我尝试了两件事:

我尝试将整个代码放在一个名为page submit的文件中。php。我明白了:

看来我们找不到你要找的东西。也许搜索或下面的链接可以有所帮助。

我还尝试将php部分放在一个名为frontpost的文件中。php。并添加以下内容:

<form id="new_post" name="new_post" method="post" action="<?php echo get_template_directory_uri(); ?>/frontpost.php">
到行动。但我得到一个服务器错误。

有什么建议可以解决这个问题吗?

1 个回复
最合适的回答,由SO网友:mor7ifer 整理而成

如果希望wordpress识别表单,则需要将处理表单设置为模板并将其应用于页面。然后,您可以将您的操作设置为页面名称,并将模板应用于该页面和viola!

结束

相关推荐

Home.php或FrontPage(通过设置)用于主题?

好啊我(再次)从培训资源那里得到了相互矛盾的指示。这次我的问题是\"how to set a default homepage\".一个教程(Coyers“WordPress 3:创建和编辑自定义主题”)uses home.php, 而另一位(专业WordPress)建议设置“首页显示…”通过WordPress(设置>阅读)。。。啊哈!在查看论坛和博客时,我看到了同样的问题:conflicting advice. 一个用户建议NEVER 使用主页。主题中的php模板页为“It can cause p