• Hi,

    I have created a short-code

    function show_current_year() {
    
    return '<span class="current-year">' . date('Y') . '</span>';
    }
    add_shortcode( 'year', 'show_current_year' );

    Now it’s working fine.

    Now I want to hook this shortcode [year] into my footer.php along with another function

    function mt_text() {
    
    	/* Retrieve the text from the database */
    	$some_text = get_option( 'theme_text', $default );
    
    	/* Filter and return the text */
    	echo apply_filters( 'get_text',  '<div id="footer-info-test">' . $some_text . '</div>' );
    }
    add_filter ('wp_footer', 'my_text');

    I want to show both the shortcode value (which is 2015) as well as the content that is being retrieved from the database.

    Hope I made sense. Any suggestion or correction will be appreciated.

    Thanks for your time

Viewing 2 replies - 1 through 2 (of 2 total)
  • anonymized-13749270

    (@anonymized-13749270)

    do_shortcode should let you output shortcode content in PHP function :

    function mt_text() {
    
    	/* Retrieve the text from the database */
    	$some_text = get_option( 'theme_text', $default );
    	$some_text .= do_shortcode('[year]');
    	// or simply:
    	//$some_text .= '<span class="current-year">' . date('Y') . '</span>';
    
    	/* Filter and return the text */
    	echo apply_filters( 'get_text',  '<div id="footer-info-test">' . $some_text . '</div>' );
    }
    add_filter ('wp_footer', 'my_text');

    hope it works.

    Thread Starter klmnweb

    (@klmnweb)

    Thanks Samuel for this. I tried that earlier and it’s really working.. but the catch is the date(year) shows up even without me adding the shortcode [year]. Let me explain.

    I am creating a plugin option page where I have used wp_editor and the text/input of that editor is pulled into the footer.php file using the function give above.

    Now when someone puts ‘SOME TEXT’ in the wysiwyg editor, the content is being successfully fetched and shown on my designated place.

    But I want, if someone only adds the shortcode [year], the value shud be fetched, else just the content.

    In short, fetch the content from the wysiwyg editor, if there’s a shortcode, then show the value of the shortcode as well.

    Hope I made myself clear here. Possible to add a condition here.?

    Many thanks once again for your time.

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

The topic ‘How to return content of a shortcode inside a function?’ is closed to new replies.