Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2561250
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: PHP

2013-05-17 13:03:06

参考API:
$post = array(
  'ID'             => [ ] //Are you updating an existing post?
  'menu_order'     => [ ] //If new post is a page, it sets the order in which it should appear in the tabs.
  'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments.
  'ping_status'    => [ 'closed' | 'open' ] // 'closed' means pingbacks or trackbacks turned off
  'pinged'         => [ ? ] //?
  'post_author'    => [ ] //The user ID number of the author.
  'post_category'  => [ array(, <...>) ] //post_category no longer exists, try wp_set_post_terms() for setting a post's categories
  'post_content'   => [ ] //The full text of the post.
  'post_date'      => [ Y-m-d H:i:s ] //The time post was made.
  'post_date_gmt'  => [ Y-m-d H:i:s ] //The time post was made, in GMT.
  'post_excerpt'   => [ ] //For all your post excerpt needs.
  'post_name'      => [ ] // The name (slug) for your post
  'post_parent'    => [ ] //Sets the parent of the new post.
  'post_password'  => [ ? ] //password for post?
  'post_status'    => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | 'custom_registered_status' ] //Set the status of the new post.
  'post_title'     => [ ] //The title of your post.
  'post_type'      => [ 'post' | 'page' | 'link' | 'nav_menu_item' | 'custom_post_type' ] //You may want to insert a regular post, page, link, a menu item or some custom post type
  'tags_input'     => [ ', , <...>' ] //For tags.
  'to_ping'        => [ ? ] //?
  'tax_input'      => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies. 
);  




$defaults = array(
  'post_status'           => 'draft', 
  'post_type'             => 'post',
  'post_author'           => $user_ID,
  'ping_status'           => get_option('default_ping_status'), 
  'post_parent'           => 0,
  'menu_order'            => 0,
  'to_ping'               =>  '',
  'pinged'                => '',
  'post_password'         => '',
  'guid'                  => '',
  'post_content_filtered' => '',
  'post_excerpt'          => '',
  'import_id'             => 0
);

wp_insert_post( $my_post );

在wordpress的根目录新建以下文件:savepost.php

点击(此处)折叠或打开

  1. <?php

  2. require_once("wp-load.php");
  3. //global $wpdb;

  4. $title = $_POST['title'];
  5. $content = $_POST['content'];
  6. $id =$_POST['id'];

  7. // Create post object
  8. $my_post = array(
  9.   'post_title' => $title,
  10.   'post_content' => $content,
  11.   'post_status' => 'publish',
  12.   'post_author' => 1,
  13.   'post_name' => $id,
  14.   'post_category' => array(8,39)
  15. );

  16. // Insert the post into the database
  17. wp_insert_post( $my_post );
  18. echo "success";
  19. ?>

客户端的代码片段:

点击(此处)折叠或打开

  1. var id = 123;
  2. var content = 'content...';
  3. var title = 'my title';
  4. $.ajax({
  5.      url: "",
  6.       type: "post",
  7.       //dataType: "json",
  8.      data: {"id":id,"title":title,"content":content},
  9.       success: function(response){
  10.         //alert(response);
  11.           alert("商品信息保存成功");
  12.       },
  13.       error:function(){
  14.           alert("商品信息保存失败");
  15.       }
  16.    });


如果客户端代码和wordpress不在一个域下,可以正常执行发布文章,但是服务端响应的数据,在客户端无法读取到。



阅读(1771) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

CU博客助理2013-06-09 15:50:18

嘉宾点评:原创性不强,很少部分的原创,下期请选择最好的几篇参与评选,提高中奖率,感谢您对博客的支持!
(感谢您参与“原创博文评选”获奖结果即将公布)