I have put the shortcode for an extension I am building in the same file where that extension (meta box) is located, at the end of the file like:

function my_shortcode_callback( $atts ) {
    $atts = shortcode_atts( array(
            'post_id' => '',
            'class'   => '',
        ), $atts, 'event_talks' );

    $post_id = $atts['post_id'];
    $class   = $atts['class'];

    $out = '';

    $out .= '<div class="event_talk_wrapper '.$class.' '.$post_id.'">

    return $out;

add_shortcode( 'event_talks', 'my_shortcode_callback' );

And when I put the shortcode in my post/page/CPT I get this:

[event_talks post_id="453"]

just written out. So I looked if I wrote anything wrong, but couldn't find the reason why it wasn't working. So I put this code in functions.php and first I got the error about having 2 functions in the same place and redeclaring it, so I commented out the one in the included file, and the shortcode works! It print's out the BLAAAAAAAAAAAAAAAAAAAAAAAA with the html tags around it.

The file is included in functions.php, the metabox works as it should.

So why isn't my shortcode working when I put it inside my included file? There aren't any conditionals in it that would prevent it from rendering. Do I need to hook the whole thing to something so that it would load faster? I don't get it.

I am including my additional file like this:

add_action('after_setup_theme', 'mytheme_theme_setup');

function mytheme_theme_setup(){
    if( in_array('the-events-calendar/the-events-calendar.php', get_option('active_plugins')) ){
        require_once( TEMPDIR. '/inc/event_additionals.php' );

where TEMPDIR is define('TEMPDIR', get_template_directory());.

  • 1
    It should not be an issue. You should post how you include files into your functions.php. As a BBBBIIIIIGGGGGG sidenote, NEVER EVER use extract() anywhere, not even in pure PHP code. It's use is highly discouraged, and therefor WordPress has also removed it completely from core years ago – Pieter Goosen May 4 '16 at 10:49
  • Didn't know about extract, I've replaced it with: $atts = shortcode_atts( array( 'post_id' => '', 'class' => '', ), $atts, 'event_talks' ); – dingo_d May 4 '16 at 10:52
  • set WP_DEBUG to true and fix all the errors you get ;) – Mark Kaplun May 4 '16 at 13:09
  • Only notice I have is by bbpress that is installed (I'm currently developing this as a part of an events theme, I'll turn this into a plugin later on). I get no errors related to this :\ The notice is Notice: bbp_setup_current_user was called incorrectly. The current user is being initialized without using $wp->init(). – dingo_d May 4 '16 at 16:11
  • your file is obviously not being loaded or being loaded too early or too late. With the amount of code you put here no one will be able to guess – Mark Kaplun May 5 '16 at 7:10

Your Answer

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

Browse other questions tagged or ask your own question.