class WC_Dynamic_Shortcodes_Generator { public function __construct() { add_action('init', [$this, 'register_shortcodes']); } public function get_dynamic_items() { $items = []; // Get product attributes $attributes = wc_get_attribute_taxonomies(); foreach ($attributes as $attribute) { $taxonomy = wc_attribute_taxonomy_name($attribute->attribute_name); $items[] = [ 'type' => 'attribute', 'name' => $attribute->attribute_name, 'label' => $attribute->attribute_label, 'taxonomy' => $taxonomy, 'shortcode' => '[product_' . sanitize_title($attribute->attribute_name) . ']' ]; } // Get taxonomies $taxonomies = get_object_taxonomies('product', 'objects'); foreach ($taxonomies as $taxonomy) { if ($taxonomy->public && !str_starts_with($taxonomy->name, 'pa_')) { $items[] = [ 'type' => 'taxonomy', 'name' => $taxonomy->name, 'label' => $taxonomy->label, 'shortcode' => '[product_' . sanitize_title($taxonomy->name) . ']' ]; } } return $items; } public function register_shortcodes() { foreach ($this->get_dynamic_items() as $item) { add_shortcode('product_' . sanitize_title($item['name']), function() use ($item) { return apply_filters('wc_dynamic_shortcode_content', '', $item); }); } // Register generic shortcode add_shortcode('product_attribute', function($atts) { $atts = shortcode_atts([ 'name' => '', 'default' => '' ], $atts); return apply_filters('wc_dynamic_attribute_content', $atts['default'], $atts['name']); }); } }class WC_Dynamic_Shortcodes_Admin { public function __construct() { add_action('admin_menu', [$this, 'add_admin_menu']); add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']); } public function add_admin_menu() { add_submenu_page( 'woocommerce', __('Dynamic Content Shortcodes', 'wc-dynamic-shortcodes'), __('Shortcode Reference', 'wc-dynamic-shortcodes'), 'manage_woocommerce', 'wc-dynamic-shortcodes', [$this, 'render_admin_page'] ); } public function enqueue_assets($hook) { if ('woocommerce_page_wc-dynamic-shortcodes' !== $hook) { return; } wp_enqueue_style( 'wc-dynamic-shortcodes-admin', plugin_dir_url(__FILE__) . '../assets/css/admin.css' ); wp_enqueue_script( 'clipboard', plugin_dir_url(__FILE__) . '../assets/js/clipboard.js', [], '2.0.11', true ); wp_enqueue_script( 'wc-dynamic-shortcodes-admin', plugin_dir_url(__FILE__) . '../assets/js/admin.js', ['clipboard'], '1.0.0', true ); } public function render_admin_page() { $generator = new WC_Dynamic_Shortcodes_Generator(); $items = $generator->get_dynamic_items(); echo '
' . esc_html__('Use these shortcodes to display product attributes and taxonomies in your content:', 'wc-dynamic-shortcodes') . '
'; echo '' . esc_html__('Type', 'wc-dynamic-shortcodes') . ' | '; echo '' . esc_html__('Name', 'wc-dynamic-shortcodes') . ' | '; echo '' . esc_html__('Shortcode', 'wc-dynamic-shortcodes') . ' | '; echo '' . esc_html__('Action', 'wc-dynamic-shortcodes') . ' | '; echo '
---|---|---|---|
' . esc_html(ucfirst($item['type'])) . ' | '; echo '' . esc_html($item['label']) . ' | '; echo '' . esc_html($item['shortcode']) . ' | ';
echo ''; echo ' |
' . esc_html__('Generic Shortcode:', 'wc-dynamic-shortcodes') . ' ';
echo '[product_attribute name="attribute-slug" default="N/A"] ';
echo ' |