CMS总是能让人抓狂的,太乱,但是乱中也有自己的规律。本文主要是解决的问题是:
当你在添加文章或者页面时,发现原来的博客系统并不能满足你的要求,要添加字段,或者是新增表,你又不想用插件。这个时候看看这篇博客,我相信会对你有很大的帮助。
要实现我说的功能,必须要改4个文件,在3.4和3.5上没有问题,其他版本没试过。
- /wp-admin/edit-form-advanced.php
- /wp-admin/includes/meta-boxes.php
- /wp-admin/includes/post.php
- /wp-includes/post.php
一,注册我们要添加的数据块。修改/wp-includes/post.php文件
- function create_initial_post_types() {
- register_post_type( 'post', array(
- 'labels' => array(
- 'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
- ),
- 'public' => true,
- '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
- '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
- 'capability_type' => 'post',
- 'map_meta_cap' => true,
- 'hierarchical' => false,
- 'rewrite' => false,
- 'query_var' => false,
- 'delete_with_user' => true,
- 'supports' => array( 'title',.............,'revisions', 'post-formats','product-category' ),//product-category自定义模块
- ) );
- 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
上面product-category是我添加一个模块,仔细看一下代码,感觉有点像授权。
二,创建product-category的数据模块。修改/wp-admin/includes/meta-boxes.php
- /**
- * add by tank
- * Display product category attributes form fields.
- *
- * @since 2.7.0
- *
- * @param object $post
- */
- function product_category_meta_box($post) {
- $product_father = 5;
- $child_page = wp_get_pages("sort_order=desc&title_li=&child_of=".$product_father);
- 。。。。。。。。。。。。。。。。。。。。
- ?>
- <ul>
- <?php foreach($child_list as $k=>$v):
- ?>
- <li>
- 。。。。。。。。。。。。。。。。。。。。
- </li>
- <?php endforeach;?>
- </ul>
- <?php
- }
三,数据模块准备发了,提供给/wp-admin/edit-form-advanced.php调用
- if ( post_type_supports($post_type, 'product-category') )
- 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
- function edit_post( $post_data = null ) {
- 。。。。。。。。。。。。。。。。。。。。。
- if ( 'post' == $post_data['post_type'] ){
- if(!emptyempty($post_data['product_category'])){
- update_post_meta( $post_ID, '_product_category_for_flow', implode(',',$post_data['product_category']));
- }
- }
- 。。。。。。。。。。。。。。。。。。。。。。
- }
在edit_post方法里面添加上面一段代码,在这里只是说个意思,把数据放到post_meta表中。
转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/wordpress/1487.html