Skip to content

Instantly share code, notes, and snippets.

@joelworsham
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelworsham/4b4fd6a3921be1bd23b7 to your computer and use it in GitHub Desktop.
Save joelworsham/4b4fd6a3921be1bd23b7 to your computer and use it in GitHub Desktop.
Render: Add options to the Logic shortcode.
<?php
/**
* First, we need to actually add our new options to each selectbox.
*/
add_filter( 'render_att_pre_loop', function( $atts, $code ) {
// Make sure we only do this for the Logic shortcode
if ( $code == 'render_logic' ) {
// Here's an option for the first argument
$atts['arg1']['properties']['options']['new_argument'] = __( 'New argument', 'Render_Extension' );
// And the second
$atts['arg2']['properties']['options']['new_argument_2'] = __( 'New argument 2', 'Render_Extension' );
// And even a new operator!
$atts['operator']['properties']['options']['new_operator'] = __( 'New operator', 'Render_Extension' );
}
// We have to return the new atts (or unmodified atts) back through
return $atts;
}, 10, 2 ); // Very important to pass "2". This tells the filter to pass 2 arguments through (in this case, $atts and $code)
/**
* Next we need to filter the argument coming through the actual shortcode callback.
*/
add_filter( 'render_sc_logic_arg1', function( $argument_1, $atts ) {
// Test our argument 1 against NEW argument(s)
switch ( $atts['arg1'] ) {
case ('new_argument'):
// Obviously the way we figure out what to return is totaly up to whatever the use-case is
return some_special_function_or_something();
break;
default:
// It's very important to have a default of simply passingn the argument_1 result back through (for if the user does NOT select our new argument)
return $argument_1;
}
}, 10, 2 );
/**
* Here we're doing the exact same thing as with argument 1.
*/
add_filter( 'render_sc_logic_arg2', function( $argument_2, $atts ) {
switch ( $atts['arg2'] ) {
case ('new_argument'):
return some_other_special_function_or_something( isset( $atts['parameter'] ) ? $atts['parameter'] : '' );
break;
default:
return $argument_2;
}
}, 10, 2 );
/**
* If we added a new operator to the dropdown, we need to also check for that in the shortcode callback.
*/
add_filter( 'render_sc_logic_operator', function( $output, $content, $argument_1, $argument_2, $atts ) {
// Switch through our new operator(s)
switch ( $atts['operator'] ) {
case ('new_operator'):
// This is ALSO totally dependent on the use-case. Just compare the arguments in some sort of way.
// We also need to make sure we return empty content back if the arguments did not pass.
if ( $argument_1 {new_operator} $argument_2 ) {
return $content;
} else {
return '';
}
break;
default:
// Again, very important to default to not modifying the original output
return $output;
}
}, 10, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment