• Resolved makosful

    (@makosful)


    What I’m trying to do is to allow the user to pass a picture and and a link through the customize menu, then have them appear as a picture linking to the specified external web page.

    I’ve attempted to use the WP_Customize_Control and Image_control for the user input. I’ve used the following code in my functions.php

    //Sections
    $wp_customize->add_section( 'icon_section', array(
    	'title' => __('Icons', 'My_Theme'),
    	'priority' => 30,
    	)
    );
    
    // Settings
    $wp_customize->add_setting( 'icon_setting', array(
    	'transport' => 'refresh',
    	)
    );
    
    $wp_customize->add_setting( 'icon_text_setting', array(
    		'default' => '#',
    		'transport' => 'refresh',
    	)
    );
    
    // Controls
    $wp_customize->add_control( new WP_Customize_Image_Control ( $wp_customize, 'icon_control', array(
    	'label' => __('Custom logo', 'My_Theme'),
    	'section' => 'icon_section',
    	'settings'	=> 'icon_setting'
    	)
    ));
    
    $wp_customize->add_control( new WP_Customize_Control ( $wp_customize, 'icon_text_control', array(
    	'label' => __('Custom Web Page', 'My_Theme'),
    	'section' => 'icon_section',
    	'settings'	=> 'icon_text_setting'
    	)
    ));

    I’ve confirmed the code works as it’s intended, I’m just including it because it could prove to be relevant.

    I’m then trying to call the values as the image linking to the site using the code below.

    <?php if ( get_theme_mod( 'icon_setting' )) : ?><a href= "<?php echo get_theme_mod( 'icon_text_setting' ); ?>" ><img src="<?php echo esc_url( get_theme_mod( 'icon_setting' ) ); ?>" ></a>
    <?php else : ?>
    <a href="<?php echo get_theme_mod( 'icon_text_setting' ); ?>" >Test</a>
    <?php endif; ?>

    In theory this should work, and it does to some extend. However, calling it like this makes WordPress try to link to a local site instead.

    Say I pasted the link to Google in the text input in the Customize Menu. If I then called the link alone using the <?php echo get_theme_mod( 'icon_text_setting' ); ?> it pastes out the link as plain text.
    However, if I try to call it inside a link (<a href="<?php echo get_theme_mod( 'icon_text_setting' ); ?>">Test</a>), instead of linking to Google, it tries to link to Google on my site (http://localhost:8888/wordpress/http/:www.google.com/).

    I can’t figure out what exactly I’m doing wrong, but it has to be something.

    If you have an alternative way of achieving the same result (changing images and their link through the admin panel) then I’m open to suggestions.

    I can achieve the desired result by hard-coding it in, but I’d rather avoid that as much as possible.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Bet Hannon

    (@bethannon1)

    You have marked this resolved. Care to share your solution for any others who come along with a similar issue?

    Hey makosful,
    I am stuck with same problem of url being appended to the home path. Can you please post what you did to solve it?

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Passing an external url through get_theme_mod()’ is closed to new replies.