web-dev-qa-db-fra.com

Créer une colonne personnalisée dans post table dans wordpress

Je veux ajouter deux champs personnalisés à la table wp_posts, et dont j'ai besoin pour mon plugin. maintenant pour activer ces champs, j'ai changé le fichier de base de 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' ) );

ici j'ai ajouté deux champs que je veux.

Maintenant, je veux que ces choses soient installables (j'ai ajouté manuellement deux champs ici).

Alors, comment faire cela dans le plugin.

J'ai lu un post http://wp.tutsplus.com/tutorials/creative-coding/add-a-custom-column-in-posts-and-custom-post-types-admin-screen/ ici les hooks sont utilisés dans le thème function.php, mais je veux le faire dans le plugin lui-même.

J'utilise wordpress 3.6.

toujours aucune confusion s'il vous plaît commenter je vais mettre à jour.

3
anytime

Vous ne devriez pas éditer de fichiers de base, cela rend la mise à niveau très difficile.

Utilisez la fonction add_meta_box() et mettez-la dans un fichier plugin.

Voir l'exemple ci-dessous, ce plugin ajoutera une boîte à méta à toutes les pages de votre installation wordpress. Il suffit de le modifier pour vos propres moyens ...

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


?>
1
Edd Smith

Dans ce code, je change les colonnes de ma page edit.php. En fait, je ne conserve que la case à cocher, l'auteur et la date. J'ajoute des champs acf des articles à la page edit.php, mais vous pouvez y ajouter d'autres méta.

/* -------------------------------------------------------------------------------
  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
  ------------------------------------------------------------------------------- */
0
knif3r