Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to add two custom field to table wp_posts, and that I need for my plugin. now to enable these fields I have changed core file of 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' ) );

here I have added two fields which I want.

Now I want these things as installable( I have manually added two fields here).

So how to do that within plugin.

I have read one post http://wp.tutsplus.com/tutorials/creative-coding/add-a-custom-column-in-posts-and-custom-post-types-admin-screen/ here hooks are used in theme function.php,but i want to do in plugin itself.

I am using wordpress 3.6.

still any confusion please comment I will update.

share|improve this question
    
put the code in a plugin. a hook is a hook, doesn't matter if it's in a theme or a plugin. I should point out here that modifying a core file should never be done, as well as modifying a core table. use post meta if you need to store additional data for a post. –  Milo Aug 19 '13 at 16:33

1 Answer 1

You should not be editing core files, it makes it very difficult to upgrade.

Use the add_meta_box() function and put it in a plugin file.

See example below, this plugin will add a meta box to all pages in your wordpress installation. Just edit it for your own means...

<?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']));
    }
}


?>
share|improve this answer
    
An example would be great with a short explanation. Link only answers are useless, specially if that links gets removed in future (we have no control over links outside WPSE). Please edit your answer and add relevant info. Just one more thing, the filename is functions.php, not function.php. I have already corrected that –  Pieter Goosen May 26 at 12:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.