Last active
February 21, 2024 21:32
-
-
Save igorbenic/a93fc46d03aa90411523 to your computer and use it in GitHub Desktop.
How to add a Custom Column in WordPress with OOP | http://www.ibenic.com/add-custom-column-wordpress-oop/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Action Hook to add column value for posts | |
add_action( 'manage_posts_custom_column' , 'your_function', 10, 2 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Action Hook to add column value for custom post types | |
add_action( 'manage_{$post_type}_posts_custom_column' , 'your_function', 10, 2 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Action Hook to add columns for posts | |
add_action( 'manage_pages_custom_column' , 'your_function', 10, 2 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WordPressColumn { | |
// ... | |
/** | |
* Add the column to the columns array | |
* @param array $columns | |
*/ | |
public function add_column( $columns ) { | |
$columns[ $this->options['column_key'] ] = $this->options['column_title']; | |
return $columns; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WordPressColumn { | |
/** | |
* Default definitions | |
* @var array | |
*/ | |
public $defaults = array( | |
'post_type' => 'post', | |
'column_key' => '', | |
'column_title' => '', | |
'column_display' => 'column_key' | |
); | |
/** | |
* Container of our options for the column | |
* @var array | |
*/ | |
public $options = array(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WordPressColumn { | |
// ... | |
/** | |
* Merge added options with the defaults. If the column key is not defined throw and exception. | |
* If we do not have a title for the column, create one from the column key | |
* @param array $options | |
*/ | |
public function __construct( $options ){ | |
$this->options = array_merge( $this->defaults, $options ); | |
if( $this->options['column_key'] == '' ){ | |
$message = __( 'Column key is not defined', 'yourtextdomain' ); | |
throw new Exception( $message ); | |
} | |
if( $this->options['column_title'] == '' ) { | |
$this->options['column_title'] = ucfirst( $this->options['column_key'] ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WordPressColumn { | |
// ... | |
/** | |
* Attach this column by using WordPress filters and actions | |
* If the post type is different from 'post' and 'page' then add a new word to filters and actions to dynamically target our columns | |
* If the post type is a page, then change the for_type variable so that the column is added to the pages section | |
* @return void | |
*/ | |
public function attach() { | |
$post_type = ''; | |
if( $this->options['post_type'] != 'post' && $this->options['post_type'] != 'page' ){ | |
$post_type = '_' . $this->options['post_type']; | |
} | |
$for_type = 'posts'; | |
if( $this->options['post_type'] == 'page' ) { | |
$for_type = 'pages'; | |
} | |
add_filter('manage' . $post_type . '_' . $for_type . '_columns' , array( $this, 'add_column' ) ); | |
add_action( 'manage' . $post_type . '_' . $for_type . '_custom_column', array( $this, 'column_data' ), 10, 2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WordPressColumn { | |
// ... | |
/** | |
* Render a column | |
* @param string $column Column slug/key | |
* @param string $post_id | |
* @return void | |
*/ | |
public function column_data( $column, $post_id ){ | |
if( $column == $this->options['column_key'] ){ | |
if( $this->options['column_display'] == 'column_key' ){ | |
echo get_post_meta( $post_id, $this->options['column_key'], true ); | |
} else { | |
$function_name = $this->options['column_display']; | |
call_user_func_array($function_name, array( $post_id, $this->options['column_key'] ) ); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function render_user_by_id( $post_id, $column_key) { | |
$user_id = get_post_meta( $post_id, $column_key, true ); | |
$userdata = get_userdata( $user_id ); | |
echo $userdata->user_login; | |
} | |
$column = new WordPressColumn( array( | |
'column_key' => '_edit_last', | |
'column_title' => __( 'Last Edited', 'yourtextdomain' ), | |
'column_display' => 'render_user_by_id' | |
)); | |
$column->attach(); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function render_user_by_id( $post_id, $column_key) { | |
$user_id = get_post_meta( $post_id, $column_key, true ); | |
$userdata = get_userdata( $user_id ); | |
echo $userdata->user_login; | |
} | |
$column1 = new WordPressColumn( array( | |
'column_key' => '_edit_last', | |
'column_title' => __( 'Last Edited', 'yourtextdomain' ), | |
'column_display' => 'render_user_by_id' | |
)); | |
$columns = new WordPressColumns ( array( | |
$column1 | |
)); | |
$columns->add_columns(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function render_user_by_id( $post_id, $column_key) { | |
$user_id = get_post_meta( $post_id, $column_key, true ); | |
$userdata = get_userdata( $user_id ); | |
echo $userdata->user_login; | |
} | |
$columns = new WordPressColumns ( array( | |
array( | |
'column_key' => '_edit_last', | |
'column_title' => __( 'Last Edited', 'yourtextdomain' ), | |
'column_display' => 'render_user_by_id' | |
), | |
)); | |
$columns->add_columns(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WordPressColumns { | |
/** | |
* All columns | |
*/ | |
public $columns = array(); | |
public function __construct( $columns ) { | |
$this->columns = $columns; | |
} | |
public function add_columns() { | |
foreach ( $this->columns as $column ) { | |
$column->attach(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WordPressColumns { | |
public $columns = array(); | |
public function __construct( $columns ) { | |
$this->columns = $columns; | |
} | |
public function add_columns() { | |
foreach ( $this->columns as $column ) { | |
$the_column = new WordPressColumn( $column ); | |
$the_column->attach(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Filter Hook to add columns for posts | |
add_filter('manage_posts_columns' , 'your_function'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Filter Hook to add columns for custom post types | |
// {$post_type} is replaced by custom post type slug | |
add_filter('manage_{$post_type}_posts_columns' , 'your_function'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Filter Hook to add columns for pages | |
add_filter('manage_pages_columns' , 'your_function'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment