Skip to content

Instantly share code, notes, and snippets.

@smeric
Last active February 25, 2016 10:48
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 smeric/b97adaf3bd2ec3668c8e to your computer and use it in GitHub Desktop.
Save smeric/b97adaf3bd2ec3668c8e to your computer and use it in GitHub Desktop.
A wordpress shortcode to display the current post truncated content. I did this shortcode to use with Paid Member Subscriptions in the first place...
/* truncated content styles : add this to the child theme style.css */
div.truncated_content {
position: relative;
}
div.truncated_content:before {
content: '';
position: absolute;
width: 100%;
height: 100px;
height: 100%;
bottom: 0;
cursor: text;
/* "transparent" only works here because == rgba(0,0,0,0) */
background-image: -webkit-gradient( linear, left top, left bottom, color-stop( 0, transparent ), color-stop( 1, white ) );
background-image: -webkit-linear-gradient( top, transparent, white );
background-image: -moz-linear-gradient( top, transparent, white );
background-image: -ms-linear-gradient( top, transparent, white );
background-image: -o-linear-gradient( top, transparent, white );
background-image: linear-gradient( to bottom, transparent, white );
}
<?php
/**
* Plugin Name: Post truncated content
* Description: Shortcode to display current post truncated content.
* Version: 1.0
* Author: Sébastien Méric
* License: GNU General Public License v2.0 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Text Domain: sm-tcsc
* Domain Path: /languages/
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( ! defined( 'ABSPATH' ) )
exit;
if ( ! class_exists( 'SM_Truncated_Content_Shortcode' ) ) {
class SM_Truncated_Content_Shortcode {
private static $instance;
/**
* Main Instance
*
* Ensures that only one instance exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @since 1.0
*
*/
public static function instance() {
if ( ! isset ( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Start your engines
*
* @since 1.0
*
* @return void
*/
public function __construct() {
$this->setup_globals();
$this->setup_actions();
$this->load_textdomain();
}
/**
* Globals
*
* @since 1.0
*
* @return void
*/
private function setup_globals() {
// paths
$this->file = __FILE__;
$this->basename = apply_filters( 'sm_tcsc_plugin_basenname', plugin_basename( $this->file ) );
$this->plugin_dir = apply_filters( 'sm_tcsc_plugin_dir_path', plugin_dir_path( $this->file ) );
$this->plugin_url = apply_filters( 'sm_tcsc_plugin_dir_url', plugin_dir_url ( $this->file ) );
}
/**
* Setup the default hooks and actions
*
* @since 1.0
*
* @return void
*/
private function setup_actions() {
// truncated content shortcode
add_shortcode( 'truncated_content', array( $this, 'truncated_content' ) );
// action
//add_action( 'hook_name', array( $this, 'function_name' ), 10, 2 );
// filter
//add_filter( 'hook_name', array( $this, 'function_name' ) );
do_action( 'sm_tcsc_setup_actions' );
}
/**
* Loads the plugin language files
*
* @access public
* @since 1.0
* @return void
*/
public function load_textdomain() {
// Set filter for plugin's languages directory
$lang_dir = dirname( plugin_basename( $this->file ) ) . '/languages/';
$lang_dir = apply_filters( 'sm_tcsc_languages_directory', $lang_dir );
// Traditional WordPress plugin locale filter
$locale = apply_filters( 'plugin_locale', get_locale(), 'sm-tcsc' );
$mofile = sprintf( '%1$s-%2$s.mo', 'sm-tcsc', $locale );
// Setup paths to current locale file
$mofile_local = $lang_dir . $mofile;
$mofile_global = WP_LANG_DIR . '/sm-tcsc/' . $mofile;
if ( file_exists( $mofile_global ) ) {
load_textdomain( 'sm-tcsc', $mofile_global );
}
elseif ( file_exists( $mofile_local ) ) {
load_textdomain( 'sm-tcsc', $mofile_local );
}
else {
// Load the default language files
load_plugin_textdomain( 'sm-tcsc', false, $lang_dir );
}
}
/**
* Shortcode to display a post truncated content
*
* @access public
* @since 1.0
* @param array $atts Shortcode attributes
* @return string Shortcode rendered
*/
public function truncated_content( $atts ) {
// Attributes
extract( shortcode_atts(
array(
'size' => '55',
'more' => '&hellip;',
), $atts )
);
if ( is_singular() ) {
global $post;
$excerpt = $this->wp_trim_words_retain_formatting( $post->post_content, $size, $more );
$excerpt = balanceTags( $excerpt, true );
$excerpt = wpautop( $excerpt );
}
return '<div class="truncated_content">' . $excerpt . '</div>';
}
/**
* A variant of wp_trim_words that keeps html formating.
*
* @access public
* @since 1.0
* @param string $text The original text.
* @param int $num_words The number of words to trim the text to. Default 55.
* @param string $more An optional string to append to the end of the trimmed text. Default &hellip;
* @return string The trimmed text.
*/
public function wp_trim_words_retain_formatting( $text, $num_words = 55, $more = null ) {
if ( null === $more )
$more = __( '&hellip;' );
$original_text = $text;
/* translators: If your word count is based on single characters (East Asian characters),
enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
preg_match_all( '/./u', $text, $words_array );
$words_array = array_slice( $words_array[0], 0, $num_words + 1 );
$sep = '';
}
else {
$words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
$sep = ' ';
}
if ( count( $words_array ) > $num_words ) {
array_pop( $words_array );
$text = implode( $sep, $words_array );
$text = $text . $more;
}
else {
$text = implode( $sep, $words_array );
}
/**
* Filter the text content after words have been trimmed.
*
* @since 1.0
*
* @param string $text The trimmed text.
* @param int $num_words The number of words to trim the text to. Default 5.
* @param string $more An optional string to append to the end of the trimmed text, e.g. &hellip;.
* @param string $original_text The text before it was trimmed.
*/
return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
}
}
/**
* Get everything running
*
* @since 1.0
*
* @return void
*/
add_action( 'plugins_loaded', 'sm_tcsc' );
function sm_tcsc() {
$sm_tcsc = new SM_Truncated_Content_Shortcode();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment