Changing Edit Screen Columns

Since you've added the college taxonomy to the course content type, it would be great if you could see the colleges on the Edit Courses screen. Adding columns to the edit screen is a two-step process: first, define the column headers, and second, define the contents of the new column.

Changing Column Headers

The code to add a new column is fairly simple. Listing 12-15 shows how to add a column for the college taxonomy to the Edit Courses screen.

Listing 12-15. Changing column headersfor the course content type /* Custom Edit Columns */

add_filter("manage_edit-course_columns", "course_taxonomy_columns");

// rearrange the columns on the Edit screens function course_taxonomy_columns($defaults) { // insert college taxonomy column $defaults['colleges'] = __('Colleges'); return $defaults;

First, you call the appropriate filter, in this case manage_edit-course_columns(). (Replace 'course' with the name of your content type.) You're filtering an array containing the names of the column headings. To add a column, all you have to do is add an item to the array. However, that would place this column at the far right side of the table, after the comment and date columns. To make this page look more like the Edit Posts screen, you need to rearrange the columns, as shown in Listing 12-16.

Listing 12-16. Rearranging column headers add_filter("manage_edit-course_columns", "course_taxonomy_columns");

// rearrange the columns on the Edit screens function course_taxonomy_columns($defaults) {

// preserve the default date and comment columns $comments = $defaults['comments']; $date = $defaults['date'];

// remove default date and comments unset($defaults['comments']); unset($defaults['date']);

// insert college taxonomy column $defaults['colleges'] = __('Colleges');

// restore default date and comments $defaults['comments'] = $comments; $defaults['date'] = $date;

return $defaults;

In this example, the first thing you need to do is preserve the contents of the comment and date columns, so you don't have to recreate them from scratch later. (The comment column contains an image rather than just text.) Next, remove these two columns from the array. Then, add your custom column, colleges. Finally, add back the original columns in the order in which you want them to appear. The resulting column arrangement is shown in Figure 12-18.

Figure 12-18. The new Edit screen column

Displaying Column Contents

Now that your new column is in place, you need to fill it with some information—in this case, the list of colleges chosen for each course. Listing 12-17 shows the necessary filter.

Listing 12-17. Displaying the college taxonomy terms in the new Edit screen column add_action("manage_pages_custom_column", "course_taxonomy_custom_column"); // for non-hierarchical content types, use the following instead: // add_action("manage_posts_custom_column", "course_taxonomy_custom_column");

// print the college taxonomy terms, linked to filter the posts to this taxonomy term only function course_taxonomy_custom_column($column) { global $post;

$colleges = get_the_terms($post->ID, 'college'); if (!empty($colleges)) {

$thecolleges = array(); foreach ($colleges as $college) { $thecolleges[] = '<a href="edit.php?post_type=course&college='.$college->slug.'>' .

esc_html(sanitize_term_field('name', $college->name, $college->term_id, 'college', 'display')) . "</a>";

Here, you use a couple of if() statements to make sure that you're working with the proper column and that the course is assigned to at least one college. Then, you build an array of links based on the URLs on the Edit screens for other content types. These links would filter the list of courses to the chosen college, just as you'd expect. Finally, once the foreach() loop has finished, you use implode() to print the list of links separated by commas.

Figure 12-19 shows the completed Edit Courses screen.

Figure 12-19. The Edit Courses screen with the completed college taxonomy column

Was this article helpful?

0 0

Post a comment