在WordPress的POST表中创建自定义列

时间:2013-08-19 作者:anytime

我想在wp\\u posts表中添加两个自定义字段,这是我的插件所需要的。现在为了启用这些字段,我更改了wordpress的核心文件

wordpress/wp-admin/post.php
$data = compact( array( \'post_author\', \'post_date\', \'post_date_gmt\', \'post_content\', \'post_content_filtered\', \'post_title\', \'post_excerpt\', \'post_status\', \'post_type\', \'comment_status\', \'ping_status\', \'post_password\', \'post_name\', \'to_ping\', \'pinged\', \'post_modified\', \'post_modified_gmt\', \'post_parent\', \'menu_order\', \'post_mime_type\', \'guid\' ) );
这里我添加了两个我想要的字段。

现在我希望这些东西是可安装的(我在这里手动添加了两个字段)。

那么如何在插件中做到这一点。

我读过一篇帖子http://wp.tutsplus.com/tutorials/creative-coding/add-a-custom-column-in-posts-and-custom-post-types-admin-screen/ 这里的钩子用于主题函数。php,但我想在插件本身中执行。

我正在使用wordpress 3.6。

仍然有任何困惑,请评论,我会更新。

2 个回复
SO网友:Edd Smith

您不应该编辑核心文件,这会使升级非常困难。

使用add_meta_box() 函数并将其放入插件文件中。

请参见下面的示例,此插件将向wordpress安装中的所有页面添加一个元框。只需根据自己的方式进行编辑。。。

<?php 
/*
Plugin Name: META Data Plugin
Plugin URI: http://eddsmith.me
Description: Adds a META description box to all pages.
Version: 1.0
Author: Edd Smith
Author URI: http://eddsmith.me
*/
?>
<?php

add_action(\'admin_init\',\'metadescription_meta_box_init\');

function metadescription_meta_box_init() {

    // The Function that creates the meta box
    add_meta_box(\'metadescription-meta\',__(\'META description\',\'metadescription-plugin\'), \'metadescription_meta_box\', \'page\',\'advanced\',\'default\'); 

    // hook to save our meta box data when the page is saved
    add_action(\'save_post\',\'metadescription_save_meta_box\');  

}


function metadescription_meta_box($post,$box) {

    // retrieve our custom meta box values
    $metadescription = get_post_meta($post->ID,\'_metadescription\',true);

    // custom meta box form elements
    echo \'<p>\' .__(\'Enter META description below. Remember... <br/>#1 -Search engines truncate the description at 160 characters<br/>#2 - Leave blank and search engines will choose text for you<br/>#3 - Since 2009 META descriptions do not influence Googles ranking algorithms. \',\'metadescription-plugin\'). \': <input type="text" name="metadescription" value="\'.esc_attr($metadescription).\'" style="width:100%;"></p>\';    

}


function metadescription_save_meta_box($post_id,$post) {
    // if post is a revision skip saving our meta box data
    if($post->post_type == \'revision\') { return; }


    // process form data if $_POST is set
    if(isset($_POST[\'metadescription\'])) {

    // save the meta box data as post meta using the post ID as a unique prefix
    update_post_meta($post_id,\'_metadescription\', esc_attr($_POST[\'metadescription\']));
    }
}


?>

SO网友:knif3r

在这段代码中,我正在更改编辑的列。php页面,我基本上只保留了复选框、作者和日期,我正在将帖子中的acf字段添加到编辑中。php页面,但您可以在那里添加其他元。

/* -------------------------------------------------------------------------------
  Custom Columns
  ------------------------------------------------------------------------------- */

function my_posts_columns($columns) {
    $columns = array(
        \'cb\' => \'<input type="checkbox" />\',
        \'thumbnail\' => \'Снимка\',
        \'title\' => \'Заглавие на обявата\',
        \'category\' => \'Категория\',
        \'sub-cat\' => \'Под-категория\',
        \'author\' => \'Автор\',
        \'date\' => \'Дата\',
    );
    return $columns;
}

function my_custom_columns($column) {
    global $post;
    if ($column == \'thumbnail\') {
        if (get_field(\'photos\')) {
            $images = get_field(\'photos\');
            $image_1 = $images[0];
            echo \'<img src="\' . $image_1[\'url\'] . \'" style="max-width:150px;" alt="\' . $images[\'alt\'] . \'" />\';
        } else {
            echo \'Няма изображение..\';
        }
    }

    if ($column == \'category\') {
        if (get_field(\'select-category\')) {
            echo the_field(\'select-category\');
        } else {
            echo \'Unknown\';
        }
    }
    if ($column == \'sub-cat\') {
        if (get_field(\'sub-cat-auto\')) {
            echo the_field(\'sub-cat-auto\');
        }
    }
}

add_action("manage_posts_custom_column", "my_custom_columns");
add_filter("manage_posts_columns", "my_posts_columns");
/* -------------------------------------------------------------------------------
  Sortable Columns
  ------------------------------------------------------------------------------- */

function cat_column_register_sortable($columns) {
    $columns [\'category\'] = \'category\';
    return $columns;
}

add_filter("manage_edit-post_sortable_columns", "cat_column_register_sortable");

function subcat_column_register_sortable($columns) {
    $columns [\'sub-cat\'] = \'sub-cat\';
    return $columns;
}

add_filter("manage_edit-post_sortable_columns", "subcat_column_register_sortable");
/* -------------------------------------------------------------------------------
  Custom Columns END
  ------------------------------------------------------------------------------- */

结束

相关推荐

How many databases do I need?

我被要求为其他人建立另一个网站。最终,我会把它交给他们。我将在本地开发它,然后现场使用,然后移交完全控制权。同时,我需要一个数据库供WP使用。我是将新网站连接到我个人网站已有的现有数据库中,还是每个网站都有一个数据库?我不想把我的数据库和别人的数据库一起导出。