Skip to content
dreaddymck edited this page Jan 27, 2018 · 1 revision

A shortcode visibility example for Wordpress plugins/themes development.

Filter

add_filter( 'widget_text', 'do_shortcode');

Shortcode support

add_shortcode( 'widget-content', array($this, 'widget_content') );

Useful method to test conditional

function widget_content($atts){

`if( ! isset( $atts['show_if'] )) { return true; }`
`if( $atts ['show_if'] == "" ) { return true; }		`

`$show_if = explode("|", $atts ['show_if']);`

`$response = false;`
`foreach ( $show_if as $a ) `
`{`
	`$a = stripslashes($a); `
	`try {`
		`$response = eval( "return ". $a . ";"  );			`
		`if( $response ){`
			`break;	`
		`}`
	`} catch (Exception $e) {`
		`$this->_log('Caught exception: ',  $e->getMessage(), "\n") ;`
	`}`
`}		`
`return $response;`

}

Shortcode with Wordpress conditionals

[widget-content show_if="is_home()|is_single(array('some-slug','108'))" param="params1" title="A Widget" title_url="#"]

Clone this wiki locally