184 lines
5.6 KiB
PHP
184 lines
5.6 KiB
PHP
<?php
|
||
|
||
/**
|
||
* Plugin Name: Page Categories Taxonomy
|
||
*/
|
||
|
||
function add_page_categories_taxonomy()
|
||
{
|
||
register_taxonomy('page_category', ['page', 'post'], [
|
||
'label' => 'Page Categories',
|
||
'hierarchical' => true,
|
||
'public' => true,
|
||
'show_ui' => true,
|
||
'show_in_rest' => true,
|
||
]);
|
||
}
|
||
add_action('init', 'add_page_categories_taxonomy');
|
||
|
||
// For WP-CLI (where 'init' may not trigger reliably)
|
||
if (defined('WP_CLI') && WP_CLI) {
|
||
WP_CLI::add_hook('after_wp_load', function () {
|
||
register_page_category_taxonomy();
|
||
});
|
||
}
|
||
|
||
// Add a new column to the Pages list
|
||
add_filter('manage_page_posts_columns', 'add_page_category_column');
|
||
function add_page_category_column($columns)
|
||
{
|
||
$new_columns = array();
|
||
foreach ($columns as $key => $value) {
|
||
$new_columns[$key] = $value;
|
||
if ('author' === $key) {
|
||
$new_columns['page_category'] = 'Categories';
|
||
}
|
||
}
|
||
return $new_columns;
|
||
}
|
||
|
||
|
||
// Populate the custom column with taxonomy terms
|
||
add_action('manage_page_posts_custom_column', 'show_page_category_column', 10, 2);
|
||
function show_page_category_column($column, $post_id)
|
||
{
|
||
if ('page_category' === $column) {
|
||
$terms = get_the_terms($post_id, 'page_category');
|
||
if (!empty($terms) && !is_wp_error($terms)) {
|
||
$links = array();
|
||
foreach ($terms as $term) {
|
||
$url = admin_url('edit.php?post_type=page&page_category=' . $term->slug);
|
||
$links[] = '<a href="' . esc_url($url) . '">' . esc_html($term->name) . '</a>';
|
||
}
|
||
echo implode(', ', $links);
|
||
} else {
|
||
echo '—';
|
||
}
|
||
}
|
||
}
|
||
|
||
// filter taxonomy
|
||
add_action('restrict_manage_posts', 'filter_page_by_top_level_category');
|
||
function filter_page_by_top_level_category()
|
||
{
|
||
global $typenow;
|
||
|
||
if ($typenow !== 'page') return;
|
||
|
||
$taxonomy = 'page_category';
|
||
$terms = get_terms([
|
||
'taxonomy' => $taxonomy,
|
||
'parent' => 0, // 👈 Only top-level categories
|
||
'hide_empty' => false,
|
||
]);
|
||
|
||
if (!empty($terms)) {
|
||
echo '<select name="' . $taxonomy . '" style="max-width:160px">';
|
||
echo '<option value="">Filter by Category</option>';
|
||
foreach ($terms as $term) {
|
||
$selected = isset($_GET[$taxonomy]) && $_GET[$taxonomy] === $term->slug ? ' selected="selected"' : '';
|
||
echo '<option value="' . esc_attr($term->slug) . '"' . $selected . '>' . esc_html($term->name) . '</option>';
|
||
}
|
||
echo '</select>';
|
||
}
|
||
}
|
||
|
||
add_filter('parse_query', 'apply_top_level_category_filter');
|
||
function apply_top_level_category_filter($query)
|
||
{
|
||
global $pagenow;
|
||
|
||
if (
|
||
$pagenow === 'edit.php' &&
|
||
$query->is_main_query() &&
|
||
isset($_GET['page_category']) &&
|
||
!empty($_GET['page_category']) &&
|
||
$query->get('post_type') === 'page'
|
||
) {
|
||
$taxonomy = 'page_category';
|
||
$term_slug = sanitize_text_field($_GET['page_category']);
|
||
|
||
// Get full term object by slug
|
||
$term = get_term_by('slug', $term_slug, $taxonomy);
|
||
|
||
if ($term && !is_wp_error($term)) {
|
||
$query->set('tax_query', [[
|
||
'taxonomy' => $taxonomy,
|
||
'field' => 'term_id',
|
||
'terms' => [$term->term_id],
|
||
'include_children' => true, // 👈 Includes all subcategories too
|
||
'operator' => 'IN' // Matches pages with this term, even with others
|
||
]]);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
// 1️⃣ Add the Last Modified column
|
||
add_filter('manage_page_posts_columns', function($columns) {
|
||
$columns['modified_date'] = 'Last Modified';
|
||
return $columns;
|
||
});
|
||
|
||
// 2️⃣ Display the modified time
|
||
add_action('manage_page_posts_custom_column', function($column, $post_id) {
|
||
if ($column === 'modified_date') {
|
||
echo get_post_modified_time('F j, Y g:i a', false, $post_id);
|
||
}
|
||
}, 10, 2);
|
||
|
||
// 3️⃣ Make it sortable
|
||
add_filter('manage_edit-page_sortable_columns', function($columns) {
|
||
$columns['modified_date'] = 'modified_date';
|
||
return $columns;
|
||
});
|
||
|
||
// 4️⃣ Handle the sorting logic
|
||
add_action('pre_get_posts', function($query) {
|
||
if (!is_admin() || !$query->is_main_query()) {
|
||
return;
|
||
}
|
||
|
||
if ($query->get('orderby') === 'modified_date') {
|
||
$query->set('orderby', 'modified');
|
||
}
|
||
});
|
||
|
||
// Taxonomy based templates
|
||
add_action('admin_init', 'generate_page_category_templates');
|
||
function generate_page_category_templates()
|
||
{
|
||
// Define plugin directory and template folder
|
||
$plugin_dir = plugin_dir_path(__FILE__);
|
||
$template_dir = $plugin_dir . 'templates/';
|
||
|
||
if (!is_dir($template_dir)) {
|
||
mkdir($template_dir); // Create if not exists
|
||
}
|
||
|
||
// Get all top-level terms
|
||
$terms = get_terms([
|
||
'taxonomy' => 'page_category',
|
||
'parent' => 0,
|
||
'hide_empty' => false,
|
||
]);
|
||
|
||
foreach ($terms as $term) {
|
||
$slug = $term->slug;
|
||
$filename = "taxonomy-page_category-{$slug}.php";
|
||
$filepath = $template_dir . $filename;
|
||
|
||
if (!file_exists($filepath)) {
|
||
// Create a basic scaffold file
|
||
file_put_contents($filepath, "<?php\n// Template for '{$term->name}'\nget_header();\n?>
|
||
<h1>Category: <?php echo esc_html('{$term->name}'); ?></h1>
|
||
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
|
||
<h2><a href=\"<?php the_permalink(); ?>\"><?php the_title(); ?></a></h2>
|
||
<p><?php the_excerpt(); ?></p>
|
||
<?php endwhile; endif; ?>
|
||
<?php get_footer(); ?>
|
||
");
|
||
}
|
||
}
|
||
}
|