$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'); ?>filters - add_filter the_content doesn't work|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)

filters - add_filter the_content doesn't work

matteradmin10PV0评论

I just made a plugin with some meta data and such. I need to alter the content when, but my add_filter('the_content') doesn't do anything...

Code:

public function run() { 
    add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
    add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
    add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );

    add_action( 'save_post', array( $this, 'save_post' ) );
    // HERE IS MY PROBLEM:
    add_filter( 'the_content', array( $this, 'some_content_method' ) );

}


public function some_content_method( $content ) {

    var_dump('Why u no?');
    $content .= 'Why u no work?';
    return $content;
}

If I write 'add_meta_boxes' the dump will work.

    // HERE IS MY PROBLEM:
    add_filter( 'add_meta_boxes', array( $this, 'some_content_method' ) );

If I change it to add_action instead of add_filter nothing happens either

    // HERE IS MY PROBLEM:
    add_action( 'the_content', array( $this, 'some_content_method' ) );

everything else works - can anybody help?

I just made a plugin with some meta data and such. I need to alter the content when, but my add_filter('the_content') doesn't do anything...

Code:

public function run() { 
    add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
    add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
    add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );

    add_action( 'save_post', array( $this, 'save_post' ) );
    // HERE IS MY PROBLEM:
    add_filter( 'the_content', array( $this, 'some_content_method' ) );

}


public function some_content_method( $content ) {

    var_dump('Why u no?');
    $content .= 'Why u no work?';
    return $content;
}

If I write 'add_meta_boxes' the dump will work.

    // HERE IS MY PROBLEM:
    add_filter( 'add_meta_boxes', array( $this, 'some_content_method' ) );

If I change it to add_action instead of add_filter nothing happens either

    // HERE IS MY PROBLEM:
    add_action( 'the_content', array( $this, 'some_content_method' ) );

everything else works - can anybody help?

Share Improve this question edited May 18, 2017 at 1:51 John asked May 18, 2017 at 1:36 JohnJohn 134 bronze badges 8
  • How does run() get executed? – Milo Commented May 18, 2017 at 3:01
  • It's in a admin/class.php un by the main plugin php file: function run_acme_plugin() { $plugin = new Acme_Plugin(); $plugin->run(); } run_acme_plugin(); And that works just fine, because it works when I substitute the content for add_meta_boxes. everything works just fine, just not the 'the_content' – John Commented May 18, 2017 at 3:15
  • OK, just making sure it wasn't only added on admin requests. So just to clarify, you're using the_content() function on a front end request and not seeing the filtered content there? – Milo Commented May 18, 2017 at 3:46
  • I am using: add_filter( 'the_content', array( $this, 'some_content_method' ) ); and I've create some_content_method() in which I var_dump() something - and nothing happens.... If I call: add_filter( 'add_meta_boxes', array( $this, 'some_content_method' ) ); instead, my var_dump is shown I don't understand why. – John Commented May 18, 2017 at 3:58
  • 1 My question is where you are expecting to see this filtered content. It's not typically used anywhere on admin screens, it's a front end function/filter for templates. It seems like you're doing admin screen stuff. – Milo Commented May 18, 2017 at 4:04
 |  Show 3 more comments

1 Answer 1

Reset to default 1

With help from Milo I realised that 'the_content' wasn't meant for the admin part of WP, which is why I didn't see my var_dump as I expected.

Conclusion: A rookie mistake!

Post a comment

comment list (0)

  1. No comments so far