feat: add option to disable posts in admin interface

Introduced new functionality to allow disabling of posts via the theme customizer. This includes removing posts from the admin menu, toolbar, and excluding them from queries when the setting is enabled. Also added a redirect to the custom post type creation page to improve user experience.
This commit is contained in:
Kumi 2024-08-03 19:56:12 +02:00
parent 1c631f5a47
commit 3c3c337ad7
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -326,7 +326,8 @@ function enqueue_color_picker($hook_suffix)
add_action('admin_enqueue_scripts', 'enqueue_color_picker');
function display_publication_status_terms($post_id) {
function display_publication_status_terms($post_id)
{
$terms = get_the_terms($post_id, 'publication_status');
if (!empty($terms) && !is_wp_error($terms)) {
foreach ($terms as $term) {
@ -1034,7 +1035,8 @@ function preprocess_mathjax_content($content)
add_filter('the_content', 'preprocess_mathjax_content');
function customize_register($wp_customize) {
function customize_register($wp_customize)
{
// Add setting to disable posts
$wp_customize->add_setting('disable_posts', array(
'default' => false,
@ -1052,7 +1054,8 @@ function customize_register($wp_customize) {
add_action('customize_register', 'customize_register');
function process_unregister_post_type() {
function process_unregister_post_type()
{
// Get the customizer setting
$disable_posts = get_theme_mod('disable_posts', false);
@ -1064,7 +1067,8 @@ function process_unregister_post_type() {
add_action('init', 'process_unregister_post_type', 20);
function remove_post_menu_item() {
function remove_post_menu_item()
{
$disable_posts = get_theme_mod('disable_posts', false);
if ($disable_posts) {
remove_menu_page('edit.php');
@ -1073,7 +1077,8 @@ function remove_post_menu_item() {
add_action('admin_menu', 'remove_post_menu_item');
function exclude_posts_from_queries($query) {
function exclude_posts_from_queries($query)
{
if (!is_admin() && $query->is_main_query()) {
$disable_posts = get_theme_mod('disable_posts', false);
if ($disable_posts) {
@ -1082,4 +1087,32 @@ function exclude_posts_from_queries($query) {
}
}
add_action('pre_get_posts', 'exclude_posts_from_queries');
add_action('pre_get_posts', 'exclude_posts_from_queries');
function remove_posts_from_toolbar($wp_admin_bar)
{
$disable_posts = get_theme_mod('disable_posts', false);
if ($disable_posts) {
$wp_admin_bar->remove_node('new-post');
}
}
add_action('admin_bar_menu', 'remove_posts_from_toolbar', 999);
function duck_behavior_journal_redirect_post_new()
{
global $pagenow;
$disable_posts = get_theme_mod('disable_posts', false);
if ($disable_posts) {
// Check if the current page is 'post-new.php' and 'post_type' parameter is missing
if ($pagenow == 'post-new.php' && !isset($_GET['post_type'])) {
wp_redirect(admin_url('post-new.php?post_type=article'));
exit;
}
}
}
add_action('admin_init', 'duck_behavior_journal_redirect_post_new');