0

I want to make dynamic the content inside a shortcode so I can modify it in the customizer. I created the customizer tabs. Now, I want to create a shortcode to place it anywhere in the page while being able to manipulate in the customizer.

This is the code I'm working on. Thanks!

<?php
/*
Plugin Name: ....
 */

// Exit if accessed directly
if(!defined('ABSPATH')){
    exit;
  }

function customizer_inject_css()
{
?>
    <style type="text/css">
        :root {

            --theme-page-width: <?php echo get_theme_mod('theme_page_width', '1366px');
            ?>!important;
        }

        .stm-template-car_dealer_two .container {
            max-width: var(--theme-page-width)!important;
        }
    </style>


<?php
}



add_shortcode( 'ui_gallery','function_gallery_shortcode' );
function function_gallery_shortcode(){
    $dynamic_h1 = "<h1>" . get_theme_mod('dymanich1') . "</h1>";
    return $dynamic_h1;
}


    add_action( 'wp_head', 'customizer_inject_css');

    add_action("customize_register","register_customizer");

    function register_customizer( WP_Customize_Manager $wp_customize) {

        $wp_customize->add_panel( 'theme_extension_panel',
        array(
            'priority'            => 2,
            'capability'        => 'edit_theme_options',
            'title'                => __('Theme Extensions'),
            'description'        => __(''),
        ) );

            $wp_customize->add_section("general_section", array(
                "title"            => __("General"),
                "priority"    => 1,
                "panel"        => "theme_extension_panel"
            ));

                $wp_customize->add_setting( 'theme_page_width', array(
                    "transport"        => "refresh",
                    "default"        => "",
                ) );
                $wp_customize->add_control(
                    new WP_Customize_Control(
                    $wp_customize,
                    'theme_page_width_control',
                    array(
                        'label'         => __(''),
                        'section'    => 'general_section',
                        'settings'   => 'theme_page_width',
                        "type"         => "text",
                        "description" => "Max Width",
                        'priority'      => 1
                    ) )
                );

               $wp_customize->add_setting( 'dynamich1', array(
                    "transport"        => "refresh",
                    "default"        => "",
                ) );
                $wp_customize->add_control(
                    new WP_Customize_Control(
                    $wp_customize,
                    'dynamich1_control',
                    array(
                        'label'         => __(''),
                        'section'    => 'general_section',
                        'settings'   => 'dynamich1',
                        "type"         => "text",
                        "description" => "display h1",
                        'priority'      => 1
                    ) )
                );

            }

2

I would comment, but lack the pointage... You seem to be on the right track:

I see that your shortcode callback and the function are named differently: (f_gallery_shortcode / function_gallery_shortcode).

Try this:

add_shortcode( 'ui_gallery','function_gallery_shortcode' );
function function_gallery_shortcode(){
    $dynamic_h1 = "<h1>" . get_theme_mod('dynamich1') . "</h1>";
    return $dynamic_h1;
}

now using [ui_gallery] in your posts, should return the results of your Customizer value <h1>dymanich1</h1>

  • unfortunately, that didn't work, but its good to know I'm in a good track – andrew rico Feb 12 at 20:26
  • I just tested the code on my end and it works perfectly, it even insert <h1></h1> if the setting is not set. Are you sure you have named the settings correctly in both code sections? – MediaFormat Feb 12 at 20:47
  • In the customizer code the setting is named dynamich1, in the shortcode it was named dymanich1. I have updated my codeblock to 'dynamich1'. – MediaFormat Feb 12 at 20:50
  • 1
    Hello Sir!!!! Wow, that was epic, I've been trying to figure it out for 3 days, my head hurts, but now thanks to your help I'm ready to move on. Thank you so much for take your time and help me out!!!!! ;) – andrew rico Feb 12 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.