' . esc_html__( 'WidgetCalc: add your calculator key in the shortcode (key="…") or under Settings → WidgetCalc.', 'widgetcalc', ) . '

'; } return ''; } $handle = 'widgetcalc-' . substr(md5($key), 0, 12); if (!wp_script_is($handle, 'enqueued')) { wp_enqueue_script( $handle, WIDGETCALC_EMBED_HOST . '/embed/' . rawurlencode($key) . '.js', [], WIDGETCALC_VERSION, true, ); wp_script_add_data($handle, 'strategy', 'async'); } return ''; } /** * Shortcode [widgetcalc key="…"]. Falls back to the site-wide default key * set under Settings → WidgetCalc when no key attribute is given, so a * single-calculator site can drop the shortcode in with no attributes. */ function widgetcalc_shortcode($atts): string { $atts = shortcode_atts(['key' => get_option('widgetcalc_default_key', '')], $atts, 'widgetcalc'); return widgetcalc_render((string)$atts['key']); } add_shortcode('widgetcalc', 'widgetcalc_shortcode'); /** * Settings page: Settings → WidgetCalc, one field for the default key used * when a shortcode or block is placed without its own key. */ function widgetcalc_register_settings(): void { register_setting('widgetcalc', 'widgetcalc_default_key', [ 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => '', ]); } add_action('admin_init', 'widgetcalc_register_settings'); function widgetcalc_add_settings_page(): void { add_options_page( __('WidgetCalc', 'widgetcalc'), __('WidgetCalc', 'widgetcalc'), 'manage_options', 'widgetcalc', 'widgetcalc_render_settings_page', ); } add_action('admin_menu', 'widgetcalc_add_settings_page'); function widgetcalc_render_settings_page(): void { if (!current_user_can('manage_options')) { return; } ?>

[widgetcalc key="your-calculator-key"]

[ 'key' => ['type' => 'string', 'default' => ''], ], 'render_callback' => function (array $attributes): string { $key = $attributes['key'] !== '' ? $attributes['key'] : get_option('widgetcalc_default_key', ''); return widgetcalc_render((string)$key); }, 'editor_script' => 'widgetcalc-block-editor', ]); } add_action('init', 'widgetcalc_register_block'); function widgetcalc_register_block_editor_assets(): void { if (!is_admin()) { return; } wp_register_script( 'widgetcalc-block-editor', plugins_url('block.js', __FILE__), ['wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n'], WIDGETCALC_VERSION, true, ); } add_action('init', 'widgetcalc_register_block_editor_assets');