Change text on page to title Wordpress with functions - php

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');

Related

Remove type attribute from script and style tags added by WordPress plugins

I am using the function below to remove type attribute from a script and style tags but this function doesn't remove the type attribute from the scripts/styles added by the plugins. It only works on my theme files.
add_filter('style_loader_tag', 'codeless_remove_type_attr', 10, 2);
add_filter('script_loader_tag', 'codeless_remove_type_attr', 10, 2);
function codeless_remove_type_attr($tag, $handle) {
return preg_replace( "/type=['\"]text\/(javascript|css)['\"]/", '', $tag );
}
The following code worked for me, please try this by pasting the code into you function.php file.
add_action('wp_loaded', 'prefix_output_buffer_start');
function prefix_output_buffer_start() {
ob_start("prefix_output_callback");
}
add_action('shutdown', 'prefix_output_buffer_end');
function prefix_output_buffer_end() {
ob_end_flush();
}
function prefix_output_callback($buffer) {
return preg_replace( "%[ ]type=[\'\"]text\/(javascript|css)[\'\"]%", '', $buffer );
}
Hope this will work for you too.

how to remove read more after call the_content function in wordpress?

I want to remove read-more link after calling the_content() function of wordpress. I want only to show content of each post in a loop and read-more link is redundant.
I try this code but read-more link remains after post content:
add_filter( 'the_content_more_link', 'disable_more_link', 10, 2 );
function disable_more_link( $more_link, $more_link_text ) {
return;
}
where I can find the code that adds this read-more link after the content?
You can use a filter to remove the read-more link from content. I have updated your filter. Please try it, It's working fine.
function disable_more_link( $link ) {
$link = preg_replace( '|#more-[0-9]+|', '', '' );
return $link;
}
add_filter( 'the_content_more_link', 'disable_more_link', 10, 2 );
More info
function new_excerpt_more($more) {
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');

different excerpt length on wordpress

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()

Wordpress if is_page function not working

I'm trying to add a function to my Wordpress theme that will add content before and after each post, but not on any pages. This does not seem to work, but not sure why.
if(!is_page()) {
function custom_content($content) {
$content = 'before post content' . $content . 'after post content';
return $content;
}
add_filter('the_content','custom_content');
}
EDIT: Solution I ended up with that does the trick for me, using is_single to only include on single posts. If you want to include on single pages use is_singular
function custom_content($content) {
if (is_single()) {
$content = 'before post content' . $content . 'after post content';
}
return $content;
}
add_filter ('the_content', 'custom_content', 0);
You might be using it inside the loop. is_page() works only outside the loop.
https://codex.wordpress.org/Function_Reference/is_page

How to Add a filter to single.php only in wordpress?

I want to add nofollow rel to tag cloud on single post only. This function code in functions.php works fine but I couldn't figure it out how to restrict it to single.php only.
function szub_nofollow_tag($text) {
return str_replace('<a href=', '<a rel="nofollow" href=', $text);
}
add_filter('wp_tag_cloud', 'szub_nofollow_tag');
Could you please tell me how to do it??
http://codex.wordpress.org/Function_Reference/is_singular
Should do the trick, remember to use it inside the function itself as I don't think WordPress knows if it's true/false while parsing functions.php
function szub_nofollow_tag($text) {
if ( is_singular() )
return str_replace('<a href=', '<a rel="nofollow" href=', $text);
else
return $text;
}
add_filter('wp_tag_cloud', 'szub_nofollow_tag');

Categories