1
1
<?php
2
2
/**
3
3
* 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
6
6
* Author: Daniel Goulyk (danielgoulyk.com)
7
7
*/
8
8
@@ -17,30 +17,30 @@ function acfds_acf_check() {
17
17
return true ;
18
18
}
19
19
20
- // 1. Register plugin settings
20
+ // Register plugin settings
21
21
function acfds_register_settings () {
22
22
if (!acfds_acf_check ()) return ;
23
23
24
24
add_option ('acfds_page_id ' , '' );
25
- add_option ('acfds_shortcode_map ' , '' );
25
+ add_option ('acfds_shortcode_custom ' , [] );
26
26
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 ' );
28
28
}
29
29
add_action ('admin_init ' , 'acfds_register_settings ' );
30
30
31
- // 2. Admin settings page
31
+ // Admin settings page
32
32
function acfds_settings_page () {
33
33
if (!acfds_acf_check ()) return ;
34
34
35
+ $ selected_page = get_option ('acfds_page_id ' );
36
+ $ shortcode_map = get_option ('acfds_shortcode_custom ' );
35
37
?>
36
38
<div class="wrap">
37
39
<h1>ACF Dynamic Shortcodes</h1>
38
40
<form method="post" action="options.php">
39
41
<?php
40
42
settings_fields ('acfds_settings_group ' );
41
43
do_settings_sections ('acfds_settings_group ' );
42
- $ selected_page = get_option ('acfds_page_id ' );
43
- $ shortcode_map = get_option ('acfds_shortcode_map ' );
44
44
?>
45
45
<table class="form-table">
46
46
<tr valign="top">
@@ -56,44 +56,84 @@ function acfds_settings_page() {
56
56
}
57
57
?>
58
58
</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: price_basic = starting_price_basic 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>
66
60
</td>
67
61
</tr>
68
62
</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
+
69
98
<?php submit_button (); ?>
70
99
</form>
71
100
</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>
72
115
<?php
73
116
}
74
117
function acfds_register_settings_page () {
75
118
add_options_page ('ACF Dynamic Shortcodes ' , 'ACF Shortcodes ' , 'manage_options ' , 'acfds-settings ' , 'acfds_settings_page ' );
76
119
}
77
120
add_action ('admin_menu ' , 'acfds_register_settings_page ' );
78
121
79
- // 3. Register dynamic shortcodes if ACF is present
122
+ // Register dynamic shortcodes if ACF is present
80
123
function acfds_register_dynamic_shortcodes () {
81
124
if (!acfds_acf_check ()) return ;
82
125
83
126
$ 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 ' );
89
128
90
- foreach ($ lines as $ line ) {
91
- if (strpos ($ line , '= ' ) === false ) continue ;
129
+ if (!$ page_id || !is_array ($ custom_map )) return ;
92
130
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 ;
94
134
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 );
97
137
if (!$ value ) return 'Enquire for pricing ' ;
98
138
if (strpos ($ value , '$ ' ) === 0 ) return $ value ;
99
139
return '$ ' . $ value ;
0 commit comments