Last active
October 8, 2015 13:37
-
-
Save gyrus/3339396 to your computer and use it in GitHub Desktop.
WordPress Widget Logic plugin code generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
These days I prefer the Dynamic Widgets plugin (http://wordpress.org/extend/plugins/dynamic-widgets/), because | |
it can accommodate sophisticated widget display rules in a GUI that clients can use. However, the Widget Logic | |
plugin (http://wordpress.org/extend/plugins/widget-logic/) still has its place. This bit of code will add | |
a "Widget Logic generator" to the Widgets page, which is useful if you use this plugin and have some users | |
who aren't comfortable with writing the simple PHP. | |
I don't use it anymore, it's provided as-is and won't be developed any more - but let me know if you find a bug! | |
*/ | |
if ( is_admin() && function_exists( "widget_logic_expand_control" ) ) { | |
add_action( 'sidebar_admin_page', 'slt_widget_logic_generator' ); | |
function slt_widget_logic_generator() { ?> | |
<div class="wrap"> | |
<h2><?php _e( 'Widget Logic code generator' ); ?></h2> | |
<form action=""> | |
<div> | |
<p> | |
<label for="wl-display"><?php _e( 'I want to...' ); ?></label> | |
<select id="wl-display" name="wl-display"> | |
<option value="yes"><?php _e( 'display the widget' ); ?></option> | |
<option value="no"><?php _e( 'not display the widget' ); ?></option> | |
</select> | |
</p> | |
</div> | |
<div> | |
<p> | |
on... | |
<?php | |
foreach ( array( "front", "posts", "pages", "categories" ) as $slt_object ) { | |
echo '<label for="wl-object-' . $slt_object . '"><input type="checkbox" name="wl-object" id="wl-object-' . $slt_object . '" value="' . $slt_object . '" />'; | |
if ( $slt_object == "front" ) { | |
echo ' ' . __( 'the front page' ); | |
} else if ( $slt_object == "categories" ) { | |
echo ' ' . __( 'particular category archive lists' ); | |
} else { | |
echo ' ' . __( "particular", 'slt-custom' ) . ' ' . $slt_object; | |
} | |
echo '</label> '; | |
} | |
?> | |
</p> | |
</div> | |
<?php foreach ( array( "post", "page" ) as $slt_postType ) { ?> | |
<div id="wl-<?php echo $slt_postType; ?>s-select" style="display:none"> | |
<p> | |
<label for="wl-<?php echo $slt_postType; ?>s"><?php _e( 'Select' ); ?> <?php echo $slt_postType; ?>(s)</label><br /> | |
<select id="wl-<?php echo $slt_postType; ?>s" name="wl-<?php echo $slt_postType; ?>s" multiple="multiple" size="10" style="height:auto"> | |
<?php | |
if ( $slt_postType == "post" ) { | |
$slt_posts = get_posts( 'numberposts=-1&posts_per_page=-1&orderby=date&order=desc' ); | |
} else { | |
$slt_posts = get_pages( 'sort_column=menu_order' ); | |
} | |
foreach ( $slt_posts as $slt_post ) { | |
echo '<option value="' . $slt_post->ID . '">'; | |
if ( strlen( $slt_post->post_title ) > 50 ) { | |
echo substr( $slt_post->post_title, 0, 50 ) . "…"; | |
} else { | |
echo $slt_post->post_title; | |
} | |
echo '</option>'; | |
} | |
?> | |
</select> | |
</p> | |
</div> | |
<?php } ?> | |
<div id="wl-categories-select" style="display:none"> | |
<p> | |
<label for="wl-categories"><?php _e( 'Select category / categories' ); ?></label><br /> | |
<select id="wl-categories" name="wl-categories" multiple="multiple" size="10" style="height:auto"> | |
<?php | |
$slt_cats = get_categories(); | |
foreach ( $slt_cats as $slt_cat ) { | |
echo '<option value="' . $slt_cat->term_id . '">'; | |
if ( strlen( $slt_cat->name ) > 50 ) { | |
echo substr( $slt_cat->name, 0, 50 ) . "…"; | |
} else { | |
echo $slt_cat->name; | |
} | |
echo '</option>'; | |
} | |
?> | |
</select> | |
</p> | |
</div> | |
<div> | |
<p><input type="button" id="wl-generate" name="wl-generate" value=" <?php _e( 'Generate Widget Logic!' ); ?> " style="font-weight:bold" /></p> | |
</div> | |
<div> | |
<p><input type="text" id="wl-output" name="wl-output" size="100" /></p> | |
</div> | |
</form> | |
<script type="text/javascript">/* <![CDATA[ */ | |
jQuery( document ).ready( function($) { | |
$( "#wl-output" ).click( function() { $( this ).select(); }); | |
$( "input[name='wl-object']" ).click( function() { | |
if ( $( this ).val() == "posts" || $( this ).val() == "pages" || $( this ).val() == "categories" ) { | |
if ( $( this ).is( ":checked" ) ) { | |
$( "#wl-" + $( this ).val() + "-select" ).slideDown( "fast" ); | |
} else { | |
$( "#wl-" + $( this ).val() + "-select" ).slideUp( "fast" ); | |
} | |
} | |
}); | |
$( "#wl-generate" ).click( function() { | |
var output = []; | |
var values; | |
var conditional; | |
$( "input[name='wl-object']:checked" ).each( function() { | |
if ( $( this ).val() == "front" ) { | |
output.push( "is_front_page()" ); | |
} else { | |
switch ( $( this ).val() ) { | |
case "posts": { conditional = "is_single"; break; } | |
case "pages": { conditional = "is_page"; break; } | |
case "categories": { conditional = "is_category"; break; } | |
} | |
values = $( '#wl-' + $( this ).val() + ' :selected' ).map( function() { return $(this).val(); } ).get(); | |
output.push( conditional + "( array( " + values + " ) )" ); | |
} | |
}); | |
output = output.join( ' || ' ); | |
if ( $( "#wl-display" ).val() == "no" ) output = "! ( " + output + " )"; | |
$( "#wl-output" ).val( output ); | |
}); | |
}); | |
/* ]]> */</script> | |
</div> | |
<?php | |
} | |
} // if ( function_exists( "widget_logic_expand_control" ) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment