Shortcode used to on the archive template to add a “Add New Category Post” that should capture the current category and automatically check it off in the editor sidebar.
functions.php
/**
* 1. The FSE Button Component (Generates a clean URL parameter)
*/
function design_garden_fse_quick_add_button() {
if ( ! is_category() ) {
return '';
}
$current_cat = get_queried_object();
$cat_id = $current_cat->term_id;
$cat_name = $current_cat->name;
// We change the URL key here to preset_cat
$add_post_url = admin_url( 'post-new.php?preset_cat=' . $cat_id );
$output = '<div class="wp-block-button quick-add-wrapper">';
$output .= ' <a href="' . esc_url( $add_post_url ) . '" class="wp-block-button__link quick-add-btn">';
$output .= ' + Add New ' . esc_html( $cat_name ) . ' Post';
$output .= ' </a>';
$output .= '</div>';
return $output;
}
add_shortcode( 'category_quick_add', 'design_garden_fse_quick_add_button' );
/**
* 2. The Block Editor Interceptor (Forces Gutenberg to check the box)
*/
function design_garden_preselect_editor_category() {
global $pagenow;
// Only run this script when creating a BRAND NEW post
if ( 'post-new.php' !== $pagenow || ! isset( $_GET['preset_cat'] ) ) {
return;
}
$cat_id = intval( $_GET['preset_cat'] );
if ( ! $cat_id ) {
return;
}
?>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
// Safely watch the Gutenberg JavaScript Data Registry
if ( window.wp && wp.data ) {
const wpData = wp.data;
// Wait for the block editor data store to cycle open
const unsubscribe = wpData.subscribe(() => {
const editorSelector = wpData.select('core/editor');
if ( editorSelector ) {
const currentCategories = editorSelector.getEditedPostAttribute('categories');
// If the post loads with no categories checked, check our preset ID
if ( currentCategories && currentCategories.length === 0 ) {
wpData.dispatch('core/editor').editPost({ categories: [<?php echo $cat_id; ?>] });
// Immediately stop listening to save performance
unsubscribe();
}
}
});
}
});
</script>
<?php
}
add_action( 'admin_footer', 'design_garden_preselect_editor_category' );
style.css
/* ==========================================================================
Quick Add Button Layout
========================================================================== */
/* Container Spacing */
.quick-add-wrapper {
margin-top: 1.5rem;
margin-bottom: 2rem;
}
/* Button Cosmetics matching your global variables */
.wp-block-button__link.quick-add-btn {
background-color: var(--wp--preset--color--background) !important;
color: var(--wp--preset--color--background) !important;
border: 1px solid var(--wp--preset--color--primary) !important;
padding: 0.6rem 1.4rem !important;
border-radius: 8px !important;
font-size: var(--wp--preset--font-size--small) !important;
font-weight: 600 !important;
text-decoration: none !important;
display: inline-block;
transition: all 0.2s ease-in-out;
}
/* Hover Adjustments */
.wp-block-button__link.quick-add-btn:hover {
background-color: var(--wp--preset--color--background) !important;
color: var(--wp--preset--color--primary) !important;
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}