elodie
elodie

Reputation: 13

Add shortcode child theme WordPress

I work in multisite I have a child theme of my parent theme to customize a page template. In this customize page template I would like to display a shortcode, which allows to retrieve the url address of the site in question.

My function in my child theme file, function.php:

add_action( 'init', function() {
add_shortcode( 'site_url', function( $atts = null, $content = null ) {
    return site_url();
} );

} );

I would like to declare this shortcode in my template customizer, like this:

/*
Template name: Test 
*/
get_header(); ?>
<div>
    <h1>Notre site web : [site_url]</h1>
        <?php
            if ( have_posts() ) :
                while ( have_posts() ) :
                    the_post();
                    get_template_part( 'content', 'page' );
                endwhile;
            endif;
        ?> 
</div>
<?php get_footer(); ?>

````but the shotcode doesn't generate anything at all in my customize template.
I must have missed something... :-(
Can you help me ?
Thanks

Upvotes: 1

Views: 1040

Answers (1)

Jon Walter
Jon Walter

Reputation: 189

This is because shortcodes that are hardcoded into templates don't get rendered by default. What you want to do is put the shortcode in the function do_shortcode() and then it should output correctly.

echo do_shortcode('[site_url]');

Upvotes: 2

Related Questions