wordpress 后台 无插件 自定义数据录入

张映 发表于 2013-01-06

分类目录: wordpress

标签:,

CMS总是能让人抓狂的,太乱,但是乱中也有自己的规律。本文主要是解决的问题是:

当你在添加文章或者页面时,发现原来的博客系统并不能满足你的要求,要添加字段,或者是新增表,你又不想用插件。这个时候看看这篇博客,我相信会对你有很大的帮助。

要实现我说的功能,必须要改4个文件,在3.4和3.5上没有问题,其他版本没试过。

  1. /wp-admin/edit-form-advanced.php  
  2. /wp-admin/includes/meta-boxes.php  
  3. /wp-admin/includes/post.php  
  4. /wp-includes/post.php  

一,注册我们要添加的数据块。修改/wp-includes/post.php文件

  1. function create_initial_post_types() {  
  2.     register_post_type( 'post'array(  
  3.         'labels' => array(  
  4.             'name_admin_bar' => _x( 'Post''add new on admin bar' ),  
  5.         ),  
  6.         'public'  => true,  
  7.         '_builtin' => true, /* internal use only. don't use this when registering your own post type. */  
  8.         '_edit_link' => 'post.php?post=%d', /* internal use only. don'use this when registering your own post type. */  
  9.         'capability_type' => 'post',  
  10.         'map_meta_cap' => true,  
  11.         'hierarchical' => false,  
  12.         'rewrite' => false,  
  13.         'query_var' => false,  
  14.         'delete_with_user' => true,  
  15.         'supports' => array'title',.............,'revisions''post-formats','product-category' ),//product-category自定义模块  
  16.     ) );  
  17. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。  

上面product-category是我添加一个模块,仔细看一下代码,感觉有点像授权。

二,创建product-category的数据模块。修改/wp-admin/includes/meta-boxes.php

  1. /** 
  2.  * add by tank 
  3.  * Display product category attributes form fields. 
  4.  * 
  5.  * @since 2.7.0 
  6.  * 
  7.  * @param object $post 
  8.  */  
  9. function product_category_meta_box($post) {  
  10.     $product_father = 5;  
  11.     $child_page = wp_get_pages("sort_order=desc&title_li=&child_of=".$product_father);  
  12.     。。。。。。。。。。。。。。。。。。。。  
  13. ?>  
  14.   
  15. <ul>  
  16. <?php foreach($child_list as $k=>$v):  
  17. ?>  
  18. <li>  
  19. 。。。。。。。。。。。。。。。。。。。。  
  20. </li>  
  21. <?php endforeach;?>  
  22. </ul>  
  23. <?php  
  24. }  

三,数据模块准备发了,提供给/wp-admin/edit-form-advanced.php调用

  1. if ( post_type_supports($post_type'product-category') )  
  2.     add_meta_box('pageproductcategory''post' == $post_type ? __('Product Category') : __('Attributes'), 'product_category_meta_box', null, 'side''core');  

解释:

1,product-category是/wp-includes/post.php文件里面添加模块名。

2,pageproductcategory是html的div的id名称,可自定义,但不是根其他模块重了就行。

3,'post'==$post_type,判断是不是post,因为我上面加product-category是在post里面加的。'Product Category'标签名。

4,product_category_meta_box,是在/wp-admin/includes/meta-boxes.php自定义的方法名。

四,数据入库,修改/wp-admin/includes/post.php

  1. function edit_post( $post_data = null ) {  
  2. 。。。。。。。。。。。。。。。。。。。。。  
  3.     if ( 'post' == $post_data['post_type'] ){  
  4.         if(!emptyempty($post_data['product_category'])){  
  5.             update_post_meta( $post_ID'_product_category_for_flow', implode(',',$post_data['product_category']));  
  6.         }  
  7.     }  
  8. 。。。。。。。。。。。。。。。。。。。。。。  
  9. }  

在edit_post方法里面添加上面一段代码,在这里只是说个意思,把数据放到post_meta表中。

wordpress 后台 添加自定义模块

wordpress 后台 添加自定义模块



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/wordpress/1487.html