$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - why str_replace targeting pages instead just targeting post?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin7PV0评论

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;
}
Post a comment

comment list (0)

  1. No comments so far