运行此页面时出现 Wordpress 404 错误 [英] Wordpress 404 error when running this page

查看:43
本文介绍了运行此页面时出现 Wordpress 404 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Wordpress 的前端发帖.我在名为 page-submit.php 的页面中有以下代码,虽然它可以工作,但在提交页面时出现 404 错误,但我不知道为什么或如何跟踪问题.

这个页面 page_submit.php 显示正常并且位于我的主题文件夹中.

有什么建议吗?

<?php//检查表单是否提交if('POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {//做一些小的表单验证以确保有内容if (isset ($_POST['title'])) {$title = $_POST['title'];} 别的 {echo '请输入标题';}if (isset ($_POST['description'])) {$description = $_POST['description'];} 别的 {echo '请输入内容';}$tags = $_POST['post_tags'];//将表单内容作为数组添加到 $post$post = 数组('post_title' =>$title,'post_content' =>$描述,'post_category' =>$_POST['cat'],//也可用于自定义分类法'tags_input' =>$标签,'post_status' =>'publish',//选择:发布、预览、未来等.'post_type' =>$_POST['post_type']//如果你想使用自定义的帖子类型);wp_insert_post($post);//将 $post 的值传递给 WordPress 的插入函数//http://codex.wordpress.org/Function_Reference/wp_insert_postwp_redirect(home_url());//提交后跳转到首页}//万一//执行 wp_insert_post 操作以插入它do_action('wp_insert_post', 'wp_insert_post');?><!-- 新的帖子表单--><div id="邮箱"><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">标签</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');?></表单>

<!--//新建帖子表单 --><?php get_sidebar();?><?php get_footer();?>

解决方案

看起来您的 POST 变量已被 Wordpress 保留.尝试使用title_x"、description_x"和post_tags_x",而不是title"、description"和post_tags".

I am trying to do a post from the frontend of Wordpress. I have the following code in a page called page-submit.php and while it has worked, I get 404 errors when the page is submitted but I don't know why or how to trace the issue.

This page page_submit.php shows up fine and is located in my theme folder.

Any suggestions?

<?php get_header(); ?>

<?php

// 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 -->


<?php get_sidebar(); ?>

<?php get_footer(); ?>

解决方案

It looks like your POST variables are reserved by Wordpress. Rather than "title", "description" and "post_tags", try "title_x", "description_x" and "post_tags_x".

这篇关于运行此页面时出现 Wordpress 404 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆