带有wp_redirect($ url)的functions.php;出口();使WordPress的网站空白 [英] functions.php with wp_redirect($url); exit(); makes wordpress site blank

查看:92
本文介绍了带有wp_redirect($ url)的functions.php;出口();使WordPress的网站空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为用户创建一个表单,以便从前端提交帖子。
表单提交后,应将用户重定向到他们刚创建的职位。



我在我的functions.php中有这段代码。然而,它使我的网站变为空白...

我认为它与exit()行有关,我试图修改它,但它不起作用,发生在所有。它只是显示一个白页。

 <?php 
wp_register_script('validation','http://ajax.aspnetcdn.com/ajax /jquery.validate/1.9/jquery.validate.min.js',array('jquery'));
wp_enqueue_script('validation');


$ post_information =阵列(
'POST_TITLE'=> wp_strip_all_tags($ _POST [ 'postTitle']),
'POST_CONTENT'=> $ _POST ['postContent'],
'post_type'=>'post',
'post_status'=>'publish'
);

$ post_id = wp_insert_post($ post_information);
$ url = get_permalink($ post_id);
wp_redirect($ url);
exit();

?>

您有什么想法吗?我该如何解决这个问题?谢谢!

解决方案

好吧,它不会像那样工作。
首先,在加载functions.php时不应该添加这样的脚本(因为在WP已经确定如何处理来自浏览器的请求之前,它太早加载) - 使用对于该wp_enqueue_scripts:

 < PHP 
功能add_my_scripts(){
wp_register_script(验证','http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js',array('jquery'));
wp_enqueue_script('validation');
}
add_action('wp_enqueue_scripts',add_my_scripts);
?>

创建新帖子会在每个请求上运行 - 即使您的浏览器想要显示该请求这个新帖子。



根据你需要的东西,你可能也想把它放到一个动作钩子中,但是如果你检查它是否已经有帮助实际上是包含postTitle的POST请求,如下所示:

 <?php 
if($ _SERVER [ REQUEST_METHOD] == POST &安培;&安培; array_key_exists( postTitle,$ _ POST)){
$ post_information =阵列(
'POST_TITLE'=> wp_strip_all_tags($ _POST ['postTitle ']),
'post_content'=> $ _POST ['postContent'],
'post_type'=>'post',
'post_status'=>'publish'
);

$ post_id = wp_insert_post($ post_information);
if(is_wp_error($ post_id)){
print发生了一个错误:(\\\
;
var_export($ post_id);
}
else {
$ url = get_permalink($ post_id);
wp_redirect($ url);
}
exit();
}
?>


I'm creating a form for the users to submit posts from the front end. Once the form is submitted the users should be redirected to the post they have just created.

I have this code in my functions.php. However it makes my website blank...

I think it's related to the exit() line, I've tried to modify it but it doesn't work, nothing happens at all. It just shows a white page.

  <?php 
    wp_register_script( 'validation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js', array( 'jquery' ) );
    wp_enqueue_script( 'validation' );


    $post_information = array(
        'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
        'post_content' => $_POST['postContent'],
        'post_type' => 'post',
        'post_status' => 'publish'
    );

    $post_id = wp_insert_post($post_information);
    $url = get_permalink( $post_id );
    wp_redirect($url);
    exit();

    ?>

Do you have any ideas? How can I fix that? Thanks!

解决方案

OK, it won't work like that. First of all, you shouldn't add scripts like that when the functions.php is loaded (because it's loaded much too early, before WP has actually decided what to do with the request that comes from the browser) - use the wp_enqueue_scripts for that:

<?php
function add_my_scripts() {
    wp_register_script( 'validation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js', array( 'jquery' ) );
    wp_enqueue_script( 'validation' );
}
add_action( 'wp_enqueue_scripts', "add_my_scripts");
?>

Your creation of a new post runs on every request - even for that request when your browser wants to show that new post.

Depending on what exactly you need, you might want to put this into an action hook as well, but it should help already if you check that it was actually a POST request that contains a postTitle, something like this:

<?php
if( $_SERVER["REQUEST_METHOD"] == "POST" && array_key_exists("postTitle", $_POST)) {
    $post_information = array(
        'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
        'post_content' => $_POST['postContent'],
        'post_type' => 'post',
        'post_status' => 'publish'
    );

    $post_id = wp_insert_post($post_information);
    if(is_wp_error($post_id)) {
        print "An error occured :(\n";
        var_export($post_id);
    }
    else {
            $url = get_permalink( $post_id );
            wp_redirect($url);
    }
    exit();
}
?>

这篇关于带有wp_redirect($ url)的functions.php;出口();使WordPress的网站空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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