• Resolved erikstainsby

    (@erikstainsby)


    Driving me insane this is.

    I am working in a class file structure. I set the shortcode after the class is instantiated, at the bottom of the class file:

    if( ! isset($rsconf)) {
    	global $rsconf;
    	$rsconf = new RSConference();
    	add_shortcode('rsconf_schedule', array('RSConference', 'rsconf_schedule_shortcode'));
    }

    I have a trivial shortcode handler:

    public function rsconf_schedule_shortcode( $attrs ) {
    		error_log( __CLASS__.'::'.__FUNCTION__ );
    		return '<h2>Here</h2>';
    	}

    I get no errors and I see only the literal shortcode tag:

    [rsconf_schedule]

    This is making me mental. Anyone?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Three ideas:

    1. The way you’re referencing the method to handle the shortcode it needs to be “static”

    public static function rsconf_schedule_shortcode( $attrs ) {
    
      error_log( __CLASS__ . '::' . __FUNCTION__ );
    
    	return '<h2>Here</h2>';
    
    }

    2. Or if the add_shortcode part and the function rsconf_schedule_shortcode are defined within the same class you could make an internal reference.

    add_shortcode( 'rsconf_schedule', array( $this, 'rsconf_schedule_shortcode' ) );

    Then the method wouldn’t have to be static.

    3. Are you sure the first code block even runs? If not, why not add the “add_shortcode(…)” to the constructor?

    A 4th idea would be to pass the object instead of the class (name) like

    add_shortcode( 'rsconf_schedule', array( $rsconf, 'rsconf_schedule_shortcode' ) );

    Thread Starter erikstainsby

    (@erikstainsby)

    Thanks for the input Chris. Alas, these sugggestions seem not to have the desired effect.

    I have discovered that there seems to be an issue using shortcodes with multisite with subdirectoriies(?) I am able to get a shortcode to render if it is in a plugin activated on the base blog (blog 1). It seems that shortcodes will not work from subdirectory-activated plugins. This is really puzzling. I can’t imagine this is true for everyone. There would be a mass developer outcry against such restrictions.

    Here is a restatement of my code in case something jumps out at someone.

    if( ! class_exists('RSConferenceDisplay')) : 
    
    include_once( ABSPATH.'wp-includes/pluggable.php');
    include_once( ABSPATH.'wp-includes/shortcodes.php');
    
    class RSConferenceDisplay {
    	function __construct() {
    		add_action( 'init', array( &$this, '_init' ) );
    
    		add_shortcode('rsconf_regform',array( &$this, 'rsconf_registration_form' ));
    	}
    
    	function _init() {
    		error_log( __CLASS__.'::'.__FUNCTION__ );
    	}
    
    	public function rsconf_registration_form() {
    		error_log( __CLASS__.'::'.__FUNCTION__ );
    		$out = array( '<div class="rsc_registration">');
    		$out[] = '<h1>Shortcode</h1>';
    		$out[] = '</div><!-- .rsc_registration -->';
    		return implode("\n",$out);
    	}
    }
    
    if( ! isset( $rsdisplay )) {
    	global $rsdisplay;
    	$rsdisplay = new RSConferenceDisplay();
    }
    endif;

    I see the _init method firing when the plugin loads, but the shortcode does not get replaced in the page which calls it.

    Thread Starter erikstainsby

    (@erikstainsby)

    Solved it. The theme file was calling get_the_content() instead of using the_content().

    See also this WordPress Answers article: https://wordpress.stackexchange.com/questions/22482/loop-on-page-makes-shortcode-fail

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘shortcode fails to fire’ is closed to new replies.