| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: WP-ShowHide |
| 4 |
Plugin URI: http://lesterchan.net/portfolio/programming/php/ |
| 5 |
Description: Allows you to embed content within your blog post via WordPress ShortCode API and toggling the visibility of the cotent via a link. By default the content is hidden and user will have to click on the "Show Content" link to toggle it. Similar to what Engadget is doing for their press releases. Example usage: <code>[showhide type="pressrelease"]Press Release goes in here.[/showhide]</code> |
| 6 |
Version: 1.02 |
| 7 |
Author: Lester 'GaMerZ' Chan |
| 8 |
Author URI: http://lesterchan.net |
| 9 |
Text Domain: wp-showhide |
| 10 |
*/ |
| 11 |
|
| 12 |
|
| 13 |
/* |
| 14 |
Copyright 2014 Lester Chan (email : lesterchan@gmail.com) |
| 15 |
|
| 16 |
This program is free software; you can redistribute it and/or modify |
| 17 |
it under the terms of the GNU General Public License as published by |
| 18 |
the Free Software Foundation; either version 2 of the License, or |
| 19 |
(at your option) any later version. |
| 20 |
|
| 21 |
This program is distributed in the hope that it will be useful, |
| 22 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
GNU General Public License for more details. |
| 25 |
|
| 26 |
You should have received a copy of the GNU General Public License |
| 27 |
along with this program; if not, write to the Free Software |
| 28 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 29 |
*/ |
| 30 |
|
| 31 |
|
| 32 |
### Function: Enqueue JavaScripts |
| 33 |
add_action('wp_enqueue_scripts', 'showhide_scripts'); |
| 34 |
function showhide_scripts() { |
| 35 |
wp_enqueue_script('jquery'); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
### Function: Short Code For Inserting Press Release Into Post |
| 40 |
add_shortcode('showhide', 'showhide_shortcode'); |
| 41 |
function showhide_shortcode($atts, $content = null) { |
| 42 |
// Variables |
| 43 |
$post_id = get_the_id(); |
| 44 |
$word_count = number_format_i18n(sizeof(explode(' ', strip_tags($content)))); |
| 45 |
|
| 46 |
// Extract ShortCode Attributes |
| 47 |
extract(shortcode_atts(array( |
| 48 |
'type' => 'pressrelease', |
| 49 |
'more_text' => __('Show Press Release (%s More Words)'), |
| 50 |
'less_text' => __('Hide Press Release (%s Less Words)'), |
| 51 |
'hidden' => 'yes' |
| 52 |
), $atts)); |
| 53 |
|
| 54 |
// More/Less Text |
| 55 |
$more_text = sprintf($more_text, $word_count); |
| 56 |
$less_text = sprintf($less_text, $word_count); |
| 57 |
|
| 58 |
// Determine Whether To Show Or Hide Press Release |
| 59 |
$hidden_class = 'sh-hide'; |
| 60 |
$hidden_css = 'display: none;'; |
| 61 |
if($hidden == 'no') { |
| 62 |
$hidden_class = 'sh-show'; |
| 63 |
$hidden_css = 'display: block;'; |
| 64 |
$tmp_text = $more_text; |
| 65 |
$more_text = $less_text; |
| 66 |
$less_text = $tmp_text; |
| 67 |
} |
| 68 |
|
| 69 |
// Format HTML Output |
| 70 |
$output = '<div id="'.$type.'-link-'.$post_id.'" class="'.$type.'-link '.$hidden_class.'"><a href="#" onclick="showhide_toggle(\''.$type.'\', '.$post_id.', \''.esc_js($more_text).'\', \''.esc_js($less_text).'\'); return false;"><span id="'.$type.'-toggle-'.$post_id.'">'.$more_text.'</span></a></div>'; |
| 71 |
$output .= '<div id="'.$type.'-content-'.$post_id.'" class="'.$type.'-content '.$hidden_class.'" style="'.$hidden_css.'">'.$content.'</div>'; |
| 72 |
|
| 73 |
return $output; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
### Function: Add JavaScript To Footer |
| 78 |
add_action('wp_footer', 'showhide_footer'); |
| 79 |
function showhide_footer() { |
| 80 |
?> |
| 81 |
<?php if(WP_DEBUG): ?> |
| 82 |
<script type="text/javascript"> |
| 83 |
function showhide_toggle(type, post_id, more_text, less_text) { |
| 84 |
var $link = jQuery("#"+ type + "-link-" + post_id) |
| 85 |
, $content = jQuery("#"+ type + "-content-" + post_id) |
| 86 |
, $toggle = jQuery("#"+ type + "-toggle-" + post_id) |
| 87 |
, show_hide_class = 'sh-show sh-hide'; |
| 88 |
$link.toggleClass(show_hide_class); |
| 89 |
$content.toggleClass(show_hide_class).toggle(); |
| 90 |
if($toggle.text() === more_text) { |
| 91 |
$toggle.text(less_text); |
| 92 |
} else { |
| 93 |
$toggle.text(more_text); |
| 94 |
} |
| 95 |
} |
| 96 |
</script> |
| 97 |
<?php else : ?> |
| 98 |
<script type="text/javascript">function showhide_toggle(a,b,c,d){var e=jQuery("#"+a+"-link-"+b),f=jQuery("#"+a+"-content-"+b);a=jQuery("#"+a+"-toggle-"+b);e.toggleClass("sh-show sh-hide");f.toggleClass("sh-show sh-hide").toggle();a.text()===c?a.text(d):a.text(c)};</script> |
| 99 |
<?php endif; ?> |
| 100 |
<?php |
| 101 |
} |