different excerpt length on wordpress - php

I want to register a custom length of excerpt on my wordpress plugin. If I add my custom excerpt length on my plugin and if user's theme have another custom excerpt length registered, will they make conflict? I noticed that fucntion name will be different but the filter's tag will be same('excerpt_length').
So, please let me clear about that.
Here is my excerpt length's code.
function custom_excerpt_length( $length ) {
return 40;
}
add_filter( 'excerpt_length', 'custom_excerpt_length');
Thanks.

I use this for a highly customized excerpt output (modified from Aaron Russell's code). It won't conflict with anything else you have. You can remove text values at will. It basically removes any filters for the excerpt output and overrides them.
// better excerpt output
function improved_trim_excerpt($text) {
global $post;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace('\]\]\>', ']]>', $text);
$text = preg_replace('#<script[^>]*?>.*?</script>#si', '', $text);
$text = strip_tags($text, '<p>');
$excerpt_length = 40;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words)> $excerpt_length) {
array_pop($words);
array_push($words, '... [<a href="' . get_permalink(). '" >Read More</a>]');
$text = implode(' ', $words);
}
}
return $text;
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'improved_trim_excerpt');

If you use a function called custom_excerpt_length() and so does the theme, then you could have a conflict on the function name, but not the call to add_filter(). To mitigate the former you can either use an uncomment prefix (myplugin_custom_excerpt_length() for example) or better yet use a class to protect the namespace like $myplugin->custom_excerpt_length().
Adding multiple callbacks to actions/filters in WordPress won't cause a conflict, per se. If there are multiple actions/filters they are run in the order of their priority, which is the third argument to these functions. To give your filter hook a better chance of running last and thus be the value is used, set it to a high priority such as 999 - or at least higher than the function hooked to the filter you want to override, or lower than the default of 10 if you only want to set it if something else does. You can use this same class to add other actions/filters as well.
if ( ! class_exists( 'MyPlugin' ) ):
class MyPlugin(){
private $priority = 999;
private $excerpt_length = 40;
function __construct(){
// Use an array to pass $this and the function to add_filter()
add_filter( 'excerpt_length', array( $this, 'custom_excerpt_length' ) , $this->priority );
}
// The function name won't conflict since it is in the context of $this->excerpt_length()
function custom_excerpt_length( $length ){
return $this->excerpt_length;
}
}
new MyPlugin();
endif; // class_exists()

Related

Change text on page to title Wordpress with functions

I know how to write a function that will change a specific text to another in the pages of the site.
For example:
function replace_text($text)
{
$text = str_replace('WP', '<strong>WP</strong>', $text);
$text = str_replace('WordPress', 'WordPress', $text);
return $text;
}
add_filter('the_content', 'replace_text');
Please tell me how can I make it so that a specific word always changes to the page title? I do not understand how should I use the get_the_title () function
get_the_title() requires a postID - which can be taken from get_the_ID():
function replace_text($text)
{
$post_title = get_the_title(get_the_ID());
return str_replace('SOME_WORD', $post_title, $text);
}
add_filter('the_content', 'replace_text');

Remove type='text/javascript' from contact form 7 tags

I removed most of the type attributes from my wordpress site but still I can see this on the contact form 7 tags, how do I remove them?
I was searching this on google but didn't find the solution. Please help!
Note: I am validating html on validator.w3.org it gives warning, please see below.
Warning: The type attribute is unnecessary for JavaScript resources.
You've mentioned that your client wants these tags removed. I assume you've explained to him that they're benign (and arguably sometimes helpful). Barring that, if you still need to remove these tags, you can just use a "run time replace" instead of trying to find every. single. instance. of a script that has that attribute, especially if one plugin or another doesn't enqueue scripts properly.
Take this function for instance:
add_action( 'template_redirect', function(){
ob_start( function( $buffer ){
$buffer = str_replace( array( 'type="text/javascript"', "type='text/javascript'" ), '', $buffer );
return $buffer;
});
});
The template_redirect hook effectively has access to all of the markup for the page. So you can instead just add it to an Output Buffer, run a simple str_replace on it, and return that output.
You'll notive I've added both quote notation types since either could be in your source code at some point. You could use a regex for it if you wanted, but the array of both notations should suffice.
This is how you remove type="text/javascript" from all JavaScript files with the WordPress filter script_loader_tag.
add_filter('script_loader_tag', 'your_slug_remove_type_attr', 10, 2);
function your_slug_remove_type_attr($tag, $handle) {
return preg_replace("/type=['\"]text\/(javascript)['\"]/", '', $tag);
}
//w3c error wordpress
//we use 'init' action to use ob_start()
add_action( 'init', 'process_post' );
function process_post() {
ob_start();
}
add_action('shutdown', function() {
$final = '';
// We'll need to get the number of ob levels we're in, so that we can iterate over each, collecting
// that buffer's output into the final output.
$levels = ob_get_level();
for ($i = 0; $i < $levels; $i++) {
$final .= ob_get_clean();
}
// Apply any filters to the final output
echo apply_filters('final_output', $final);}, 0);add_filter('final_output', function($output) {
//this is where changes should be made $find=array('type="rocketlazyloadscript"',"type='rocketlazyloadscript'",'type="text/javascript"',"type='text/javascript'",'type="application/javascript"',"type='application/javascript'",'type="text/css"',"type='text/css'" );
return str_replace($find, '', $output);});
function theme_name_setup() {
add_theme_support( 'html5', array( 'script', 'style' ) );}
add_action( 'after_setup_theme', 'theme_name_setup' );
Source code
I added CSS to the function and it solved my JS and CSS W3C validation warnings. Please let me know if you see errors.
//Remove JS and CSS types
add_action( 'template_redirect', function(){
ob_start( function( $buffer ){
$buffer = str_replace( array( 'type="text/javascript"', "type='text/javascript'", 'type="text/css"', "type='text/css'" ), '', $buffer );
return $buffer;
});
});

Remove echo from function - WordPress

I'm trying to accomplish an hard task. The task is removing a echo from a Yoast SEO file using the available WP hooks.
class-frontend.php
public function head() {
global $wp_query;
$old_wp_query = null;
if ( ! $wp_query->is_main_query() ) {
$old_wp_query = $wp_query;
wp_reset_query();
}
/**
* Action: 'wpseo_head' - Allow other plugins to output inside the Yoast SEO section of the head section.
*/
do_action( 'wpseo_head' );
echo '<!-- / ', $this->head_product_name(), ". -->\n\n"; // <-- remove this
if ( ! empty( $old_wp_query ) ) {
$GLOBALS['wp_query'] = $old_wp_query;
unset( $old_wp_query );
}
return;
}
Is there a way to override this function and remove the echo using the available WP hooks? Or is there a better way to do this?
Many thanks.
There is a post at wordpress.stackexchange that indicates you can output buffer using the hooks get_header and wp_head to filter that out:
add_action('get_header', 'ad_ob_start');
add_action('wp_head', 'ad_ob_end_flush', 100);
function ad_ob_start() {
ob_start('ad_filter_wp_head_output');
}
function ad_ob_end_flush() {
ob_end_flush();
}
function ad_filter_wp_head_output($output) {
if (defined('WPSEO_VERSION')) {
$output = str_ireplace('<!-- / Yoast WordPress SEO plugin. -->', '', $output);
}
return $output;
}
That expects your $this->head_product_name() to be / Yoast WordPress SEO plugin. so you may have to alter that...or if you want it to remove "anythere" there, you can change your replacement to something like this:
$output = preg_replace('/<!-- .* -->/Us', '', $output);
But that will remove ALL comments, and it will be slower than the string replace.

How to detect if manual excerpt exists in WordPress?

I need to show excerpts of different lengths, so I use
function custom_excerpt($length) {
get_the_content();
... clean up and trim
return $excerpt;
}
however, I want to detect if a manual excerpt was entered in order to use that instead of the custom one. Is there a way to do this?
I tried by using
$wp_excerpt = get_the_excerpt();
But that returns the manual excerpt, and if the manual excerpt is empty, it automatically generates an excerpt of 55 characters, which doesn't help, because it will always be "true" (can't check if empty).
The reason for approaching it this way is because I have multiple excerpts on a single page (different lengths), and if the length needed is longer than the WordPress excerpt (55), I want to show my excerpt, unless a manual excerpt was written, in which case I want to show that.
It would be perfect if I could simply
if ( manual_excerpt() == true ) {
}
You need only replace the excerp_length wordpress default function, just follow the above code, then you can call this custom function and set the length:
<?php
// custom excerpt length
function custom_excerpt_length( $length = 20 ) {
return $length;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
?>
ANSWER UPDATED II
Using inside a function:
<?php
function custom_excerpt( $length = 55 ) {
if( $post->post_excerpt ) {
$content = get_the_excerpt();
} else {
$content = get_the_content();
$content = wp_trim_words( $content , $length );
}
return $excerpt;
}
?>
This is an old question but I was looking for this, ended up here and didn't see the following function in the answers. To know if a post has a custom excerpt you can use the has_excerpt function:
<?php has_excerpt( $id ); ?>
Where $id is the post id. If non is given then the current post id will be used.
Check if the post_excerpt slot in the post object is empty or not:
global $post;
if( '' == $post->post_excerpt )
{
// this post does NOT have a manual excerpt
}
To turn this into a function:
function so19935351_has_manual_excerpt( $post )
{
if( '' == $post->post_excerpt )
return false;
return true;
}

Wordpress strip single shortcode from posts

I want to strip just the [gallery] shortcodes in my blog posts. The only solution I found is a filter that I added to my functions.
function remove_gallery($content) {
if ( is_single() ) {
$content = strip_shortcodes( $content );
}
return $content;
}
add_filter('the_content', 'remove_gallery');
It removes all shortcodes including [caption] which I need for images. How can I specify a single shortcode to exclude or include?
To remove only the gallery shortcode , register a callback function that returns an empty string:
add_shortcode('gallery', '__return_false');
But this will only work with the callbacks. To do it statically, you can temporarily change the global state of wordpress to fool it:
/**
* #param string $code name of the shortcode
* #param string $content
* #return string content with shortcode striped
*/
function strip_shortcode($code, $content)
{
global $shortcode_tags;
$stack = $shortcode_tags;
$shortcode_tags = array($code => 1);
$content = strip_shortcodes($content);
$shortcode_tags = $stack;
return $content;
}
Usage:
$content = strip_shortcode('gallery', $content);
For me, worked with:
add_shortcode('shortcode_name', '__return_false');
If i try to strip_shortcode, they are remove all shortocodes and changing the final result
If you want to get only the content, excluding any shortcodes, try something like that
global $post;
$postContentStr = apply_filters('the_content', strip_shortcodes($post->post_content));
echo $postContentStr;

Categories