0

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?

  • How does run() get executed? – Milo May 18 '17 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 May 18 '17 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 May 18 '17 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 May 18 '17 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 May 18 '17 at 4:04
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.