Last active
April 16, 2021 06:11
-
-
Save DanielFloeter/7745f85aec0546002eb3e07180652963 to your computer and use it in GitHub Desktop.
Custom excerpt, Jean Michel
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 | |
/* | |
Plugin Name: Custom-placeholder Add-on | |
Plugin URI: http://tiptoppress.com/downloads/term-and-category-based-posts-widget/ | |
Description: Add-on to handle custom placeholder like custom fields for the premium widget Term and Category Based Posts Widget. | |
Author: TipTopPress | |
Version: 1.1 | |
Author URI: http://tiptoppress.com | |
Installation: | |
- Download the file and rename it to Custom-placeholder-Add-on.php | |
- Upload it to the plugins folder [your-wordpress-installation]/wp-content/plugins/Custom-placeholder-Add-on.php | |
- Go to WordPress > Admin > PlugIns and activate the Extension | |
- Now, there is a new sort by: 'Name' visible | |
*/ | |
namespace termCategoryPostsPro\CustomPlaceholderAddOn; | |
// Don't call the file directly | |
if ( !defined( 'ABSPATH' ) ) exit; | |
/** | |
* Applied to the list of sort orders some new ones. | |
* | |
* @return array of sort orders | |
* | |
* @since 1.1 | |
*/ | |
function custom_field ( $custom_field_names, $that, $instance ) { | |
global $post; | |
/** | |
* Custom variables which can adapt for own purpose | |
*/ | |
// Length in words | |
$custom_excerpt_length = 20; | |
// Custom text if not the normal excerpt should be shown | |
$custom_excerpt = "About the permissions you can login to read the post text."; | |
/** | |
* For debug purpose | |
*/ | |
// uncomment for debug output to /wp-content/debug.log and set define( 'WP_DEBUG, true' ) in your wp-config.php | |
// error_log("Filter debug cpwp_custom_placeholder: " . var_dump( $custom_field_names )); | |
// Get user roles which intersect Members roles | |
$user = get_userdata( get_current_user_id() ); | |
$user_roles = empty( $user ) ? array() : $user->roles; // WordPress user roles | |
$post_access_roles = get_post_meta( get_the_ID(), '_members_access_role', false ); // Members roles (https://de.wordpress.org/plugins/members/) | |
$intersect = array_intersect( $user_roles, $post_access_roles ); | |
$excerpt = ""; | |
if ( $custom_field_names === 'custom_excerpt' ) { | |
if ( ( get_post_status ( get_the_ID() ) === 'publish' ) || ( is_user_logged_in() && ! empty( $intersect ) ) ) { | |
$excerpt = strip_tags( $that->itemExcerpt( $instance, false ) ); | |
} else { | |
$excerpt = $custom_excerpt; | |
} | |
$a_excerpt = explode( ' ', $excerpt, $custom_excerpt_length ); | |
array_pop($a_excerpt); | |
$excerpt = implode( $a_excerpt, ' ' ); | |
} else if ( $custom_field_names === 'custom_author' ) { | |
return strip_tags( $that->itemAuthor( $instance, true ) ); | |
} else if ( $custom_field_names === 'custom_category' ) { | |
return strip_tags( $that->itemTerms( $instance, 'category', true ) ); | |
} else if ( $custom_field_names === 'custom_commentnum' ) { | |
return strip_tags( $that->itemCommentNum( $instance, true ) ); | |
} else if ( $custom_field_names === 'custom_date' ) { | |
return strip_tags( $that->itemDate( $instance, true) ); | |
} | |
return $excerpt; | |
} | |
add_filter( 'cpwp_custom_placeholder', __NAMESPACE__.'\custom_field', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment