how does including wp-blog-header.php change the f

2019-09-09 14:50发布

问题:

I have this javascript:

 <script type="text/javascript">            
$('.publish_post').click (function(){   
                    var info = 'some info';                     
                    $.ajax({
                       type: "POST",
                       url: "../actions/addpost.php",
                       data: info,
                       success: function(data){
                          console.log(data);
                        }
                     });                            
                })
                </script>

I have this file called addpost.php that is located in actions folder that is in the root install folder of WP.

<?php  

require('../wp-blog-header.php');    

$post = array(
  'comment_status' =>  'closed', 
  'ping_status' => 'closed' ,
  'post_category' => 1,
  'post_content' => 'some content',
  'post_name' => 'some post',
  'post_status' => 'publish', 
  'post_title' => 'some post',
  'post_type' => 'post'
);      

$the_post_id = wp_insert_post( $post, $wp_error );  

?> 

The problem is the fallowing: if I delete require('../wp-blog-header.php'); I receive no error, otherwise I receive this error in the console:

POST http://localhost/wp-content/themes/sometheme/actions/addpost.php 404 (Not Found)

However I need wp-blog-header.php in order to make the post insert.

UPDATE: The script in addpost.php gets executed but the error appears when the answer is sent.

Any ideas?

回答1:

It is not good practice to include the wp-blog-header.php into non-wordpress pages. This is why you are getting the 404 error.

You should try including wp-load.php.