Shortcode Manager via Options Page

Authored by

Easily Output Custom HTML or JavaScript with ACF-Powered Shortcodes

Create and manage dynamic shortcodes in WordPress using Advanced Custom Fields Pro and a simple shortcode function. This flexible snippet adds a Custom Shortcode Usages admin page under Settings, where you can define named shortcodes and their output via a repeater field. Perfect for developers and site managers, this solution lets you insert custom HTML, JavaScript, or any content anywhere on your site with [custom_shortcode location="your_shortcode_name"]. Ideal for reusable code blocks, scripts, embeds, and more—just add it to your functions.php or convert it into a plugin for site-wide use.

add_shortcode('custom_shortcode', 'custom_shortcode');
function custom_shortcode( $atts ) {
    $atts = shortcode_atts([
        'location' => '',
    ], $atts, 'custom_shortcode');
    $shortcode_name = $atts['location'];
    if ($shortcode_name == '' || $shortcode_name === null) return '';
    
    $custom_shortcodes = get_field('shortcodes', 'option');
    foreach ($custom_shortcodes as $id => $custom_shortcode ) {
        if ($custom_shortcode['shortcode_name'] == $shortcode_name) {
            return $custom_shortcode['shortcode_output'];
        }
    }
    return '';
}

add_action( 'acf/init', function() {
    acf_add_options_page( array(
        'page_title' => 'Custom Shortcode Usages',
        'menu_slug' => 'custom-shortcode-usages',
        'parent_slug' => 'options-general.php',
        'position' => '',
        'redirect' => false,
    ) );
} );

add_action( 'acf/include_fields', function() {
    if ( ! function_exists( 'acf_add_local_field_group' ) ) {
        return;
    }

    acf_add_local_field_group( array(
    'key' => 'group_6822698e6c06a',
    'title' => 'Shortcodes Output',
    'fields' => array(
        array(
            'key' => 'field_6822698e650e9',
            'label' => 'Shortcodes',
            'name' => 'shortcodes',
            'aria-label' => '',
            'type' => 'repeater',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'layout' => 'table',
            'pagination' => 0,
            'min' => 0,
            'max' => 0,
            'collapsed' => '',
            'button_label' => 'Add Row',
            'rows_per_page' => 20,
            'sub_fields' => array(
                array(
                    'key' => 'field_68226997650ea',
                    'label' => 'Shortcode Name',
                    'name' => 'shortcode_name',
                    'aria-label' => '',
                    'type' => 'text',
                    'instructions' => 'This is what will be used within the [custom_shortcode location="name"].',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array(
                        'width' => '30',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => '',
                    'maxlength' => '',
                    'allow_in_bindings' => 0,
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'parent_repeater' => 'field_6822698e650e9',
                ),
                array(
                    'key' => 'field_682269c8650eb',
                    'label' => 'Shortcode Output',
                    'name' => 'shortcode_output',
                    'aria-label' => '',
                    'type' => 'textarea',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array(
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => '',
                    'maxlength' => '',
                    'allow_in_bindings' => 0,
                    'rows' => '',
                    'placeholder' => '',
                    'new_lines' => '',
                    'parent_repeater' => 'field_6822698e650e9',
                ),
            ),
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'options_page',
                'operator' => '==',
                'value' => 'custom-shortcode-usages',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
    'active' => true,
    'description' => '',
    'show_in_rest' => 0,
  ) );
} );

 

Other Code Snippets

Change Permissions or Ownership of Files or Directories Recursively

Code Snippet | Linux

Sometimes when you are moving files from one host to another, you have to change the files owner or permissions so the web server can utilize it properly.

Read More
Find Files by Size

Code Snippet | Linux

Here is a Linux command line that searches for files that are over a certain size so that you can find where you might be able to free up some space. Useful to find error_logs that have run amok.

Read More
Import MySQL from Command Line

Code Snippet | Linux | MySQL

Here is a method to import a SQL file into a database via the Linux command line.

Read More
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram