plugin development - why str_replace targeting pages instead just targeting post?

admin2025-06-02  1

I am trying to replace a word only on post pages. The issue I am running into is that WordPress page content ends up blank even though this code should only run on post pages.

<?php
/**
* Plugin Name: Wordpress plugin test esmond
* Plugin URI: 
* Description: test plugin.
* Version: 1.0
* Author: Esmond Mccain
* Author URI: 
*/
defined('ABSPATH') or die();

function esmond_enqueue_scripts_styles() {
     if(is_page()){
        //Styles
        wp_enqueue_style( 'bootstrap-css', '.3.1/css/bootstrap.min.css');

       //Scripts
       wp_enqueue_script( 'bootstrap-js', '.3.1/js/bootstrap.min.js', array('jquery'), true);
     }

}
add_action('wp_enqueue_scripts','esmond_enqueue_scripts_styles');

add_filter('the_content', 'replace_word');
function replace_word($text) {
        if (is_singular( 'post' )){
        $text = str_replace('dog', 'cat', $text);

        return $text;
        }
}

I am trying to replace a word only on post pages. The issue I am running into is that WordPress page content ends up blank even though this code should only run on post pages.

<?php
/**
* Plugin Name: Wordpress plugin test esmond
* Plugin URI: https://esmondmccain
* Description: test plugin.
* Version: 1.0
* Author: Esmond Mccain
* Author URI: https://esmondmccain
*/
defined('ABSPATH') or die();

function esmond_enqueue_scripts_styles() {
     if(is_page()){
        //Styles
        wp_enqueue_style( 'bootstrap-css', 'https://stackpath.bootstrapcdn/bootstrap/4.3.1/css/bootstrap.min.css');

       //Scripts
       wp_enqueue_script( 'bootstrap-js', 'https://stackpath.bootstrapcdn/bootstrap/4.3.1/js/bootstrap.min.js', array('jquery'), true);
     }

}
add_action('wp_enqueue_scripts','esmond_enqueue_scripts_styles');

add_filter('the_content', 'replace_word');
function replace_word($text) {
        if (is_singular( 'post' )){
        $text = str_replace('dog', 'cat', $text);

        return $text;
        }
}
Share Improve this question edited Mar 18, 2019 at 17:04 Esmond asked Mar 18, 2019 at 16:51 EsmondEsmond 256 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Your code is returning $text only for posts and nothing for other post types like pages.

Your function should be like this

add_filter('the_content', 'replace_word');
function replace_word($text) {
        if (is_singular( 'post' )){
        $text = str_replace('dog', 'cat', $text);

        return $text;
        }

        // you must return content for pages/ other post types
        return $text;

}

You aren't returning $text for any other content. Filters must always return the string whether it's changed or not.

add_filter('the_content', 'replace_word');
function replace_word($text) {
        if (is_singular( 'post' )){
        $text = str_replace('dog', 'cat', $text);

        return $text;
        }
    return $text;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748795433a313783.html

最新回复(0)