Skip to content

Commit 7d36a68

Browse files
committed
Version 2.0 – Add shortcode UI, field detection, copy-to-clipboard button
1 parent 16655c0 commit 7d36a68

File tree

1 file changed

+66
-26
lines changed

1 file changed

+66
-26
lines changed

acf-dynamic-shortcodes.php

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22
/**
33
* Plugin Name: ACF Dynamic Shortcodes
4-
* Description: Dynamically register shortcodes for ACF fields from a selected page.
5-
* Version: 1.1
4+
* Description: Dynamically register shortcodes for ACF fields from a selected page using a clean admin interface.
5+
* Version: 2.0
66
* Author: Daniel Goulyk (danielgoulyk.com)
77
*/
88

@@ -17,30 +17,30 @@ function acfds_acf_check() {
1717
return true;
1818
}
1919

20-
// 1. Register plugin settings
20+
// Register plugin settings
2121
function acfds_register_settings() {
2222
if (!acfds_acf_check()) return;
2323

2424
add_option('acfds_page_id', '');
25-
add_option('acfds_shortcode_map', '');
25+
add_option('acfds_shortcode_custom', []);
2626
register_setting('acfds_settings_group', 'acfds_page_id');
27-
register_setting('acfds_settings_group', 'acfds_shortcode_map');
27+
register_setting('acfds_settings_group', 'acfds_shortcode_custom');
2828
}
2929
add_action('admin_init', 'acfds_register_settings');
3030

31-
// 2. Admin settings page
31+
// Admin settings page
3232
function acfds_settings_page() {
3333
if (!acfds_acf_check()) return;
3434

35+
$selected_page = get_option('acfds_page_id');
36+
$shortcode_map = get_option('acfds_shortcode_custom');
3537
?>
3638
<div class="wrap">
3739
<h1>ACF Dynamic Shortcodes</h1>
3840
<form method="post" action="options.php">
3941
<?php
4042
settings_fields('acfds_settings_group');
4143
do_settings_sections('acfds_settings_group');
42-
$selected_page = get_option('acfds_page_id');
43-
$shortcode_map = get_option('acfds_shortcode_map');
4444
?>
4545
<table class="form-table">
4646
<tr valign="top">
@@ -56,44 +56,84 @@ function acfds_settings_page() {
5656
}
5757
?>
5858
</select>
59-
</td>
60-
</tr>
61-
<tr valign="top">
62-
<th scope="row">Shortcode to ACF Field Mapping</th>
63-
<td>
64-
<textarea name="acfds_shortcode_map" rows="10" cols="50" placeholder="Example:&#10;price_basic = starting_price_basic&#10;price_pro = starting_price_pro"><?php echo esc_textarea($shortcode_map); ?></textarea>
65-
<p class="description">Format: <code>shortcode_name = acf_field_name</code>, one per line.</p>
59+
<p class="description"><strong>Note:</strong> This is the page where your ACF fields live. Values from this page will be used to populate shortcodes across your entire site.</p>
6660
</td>
6761
</tr>
6862
</table>
63+
64+
<?php if ($selected_page): ?>
65+
<h2>Shortcode Mapping</h2>
66+
<table class="widefat">
67+
<thead>
68+
<tr>
69+
<th>ACF Field Name</th>
70+
<th>Shortcode Name</th>
71+
<th>Copy</th>
72+
</tr>
73+
</thead>
74+
<tbody>
75+
<?php
76+
$fields = get_fields($selected_page);
77+
if ($fields) {
78+
foreach ($fields as $field_name => $val) {
79+
$shortcode = $shortcode_map[$field_name] ?? '';
80+
echo "<tr>
81+
<td><code>{$field_name}</code></td>
82+
<td><input type='text' name='acfds_shortcode_custom[{$field_name}]' value='{$shortcode}' /></td>
83+
<td>";
84+
if ($shortcode) {
85+
echo "<button type='button' class='button copy-button' data-copy='[{$shortcode}]'>Copy</button>";
86+
}
87+
echo "</td>
88+
</tr>";
89+
}
90+
} else {
91+
echo "<tr><td colspan='3'><em>No ACF fields found on this page.</em></td></tr>";
92+
}
93+
?>
94+
</tbody>
95+
</table>
96+
<?php endif; ?>
97+
6998
<?php submit_button(); ?>
7099
</form>
71100
</div>
101+
<script>
102+
document.addEventListener('DOMContentLoaded', function () {
103+
const buttons = document.querySelectorAll('.copy-button');
104+
buttons.forEach(button => {
105+
button.addEventListener('click', () => {
106+
const shortcode = button.dataset.copy;
107+
navigator.clipboard.writeText(shortcode).then(() => {
108+
button.innerText = 'Copied!';
109+
setTimeout(() => button.innerText = 'Copy', 1500);
110+
});
111+
});
112+
});
113+
});
114+
</script>
72115
<?php
73116
}
74117
function acfds_register_settings_page() {
75118
add_options_page('ACF Dynamic Shortcodes', 'ACF Shortcodes', 'manage_options', 'acfds-settings', 'acfds_settings_page');
76119
}
77120
add_action('admin_menu', 'acfds_register_settings_page');
78121

79-
// 3. Register dynamic shortcodes if ACF is present
122+
// Register dynamic shortcodes if ACF is present
80123
function acfds_register_dynamic_shortcodes() {
81124
if (!acfds_acf_check()) return;
82125

83126
$page_id = get_option('acfds_page_id');
84-
$mapping = get_option('acfds_shortcode_map');
85-
86-
if (!$page_id || !$mapping) return;
87-
88-
$lines = explode("\n", $mapping);
127+
$custom_map = get_option('acfds_shortcode_custom');
89128

90-
foreach ($lines as $line) {
91-
if (strpos($line, '=') === false) continue;
129+
if (!$page_id || !is_array($custom_map)) return;
92130

93-
[$shortcode, $field] = array_map('trim', explode('=', $line, 2));
131+
foreach ($custom_map as $field_name => $shortcode) {
132+
$shortcode = trim($shortcode);
133+
if (!$shortcode) continue;
94134

95-
add_shortcode($shortcode, function() use ($field, $page_id) {
96-
$value = get_field($field, $page_id);
135+
add_shortcode($shortcode, function() use ($field_name, $page_id) {
136+
$value = get_field($field_name, $page_id);
97137
if (!$value) return 'Enquire for pricing';
98138
if (strpos($value, '$') === 0) return $value;
99139
return '$' . $value;

0 commit comments

Comments
 (0)