• Hello,

    my plugins are working as/with classes. no problem so far and well working for small plsugins.

    now i want to add a content-filter with:

    apply_filters(“the_content”, array($this, “checkContent”));

    as you can see, this is done within a class-function.
    with non-parameter-functions (f.eg. “wp_loaded”) it’s working fine.

    but in this case the function checkContent is not called.
    same class with “wp_loaded” is working and called.

    how can i call “the_content” with the content as parameter within my class =

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Dion

    (@diondesigns)

    You must provide the variable containing the content. Example:

    apply_filters('the_content', array($this, 'checkContent'), $content);

    The above is not complete; you’ll need to tailor it to your needs. I strongly suggest you do some reading to learn how to use WordPress filters. Here is a start:

    https://developer.wordpress.org/reference/functions/apply_filters/

    Thread Starter kimjo

    (@kimjo)

    @diondesigns
    Thank you for your time and answer, but i develop software for 30+ years now and have developed several wp plugins. most of them are quite big and needs no communication with the wp core. always with classes, because i am absolutly not a fan of the proc-style WP is developed in … but every developer has it’s own way.

    the docs of WP are by far the worst i have seen. they are pretty (by colors) and structured well – but the sense of some functions is not clear for foreign developers. i hear that from a lot of my collegues and most of them will not develop for WP projects any more; just because of these (small) problems. no exmaples and quick to understand docs. even big frameworks have quick references where you can quickly look up for some details.

    pls (to all WP core developers): other developers (like me) have their own projects and customers. reading several docs for (normally self-explaining) calls is frustrating and a time killer. even finding this forum is not quick and direct.

    i think WP is a good project and helpful for a lot of (smaller) users. we would like to help them with (free) plugins – but for that the documentation must be more helpful for complex usages (like this class / OOP calls).

    your answer attached the $content to the call, pointed me to pls. RTFM.

    Dion

    (@diondesigns)

    And here I thought you were a newbie who was looking for someone to write their code for them. 🙂

    My code above is incorrect. If you want to filter the variable $content through the WP Core the content filter, here is what you need:

    $content = apply_filters('the_content', $content);

    If you want to hook into the WP core the content filter, here is what you need:

    add_filter('the_content', [$this, 'checkContent']);

    Your plugin class would require something like this:

    class OOPs {
    	public function __construct() {
    		add_filter('the_content', [$this, 'checkContent']);
    	}
    
    	public function checkContent($content) {
    		// whatever you want to do goes here
    		return $content;
    	}
    }
    Thread Starter kimjo

    (@kimjo)

    @diondesigns

    no thanks, no code writing needed 😉 just a syntax HOW to pass params to class called function 🙂 !

    Your example (thanks for your time) is not working (but pls read to the end).

    so far it’s not new and i use that form of code (a little expanded with calling a private “init” class by “plugins_loaded” in the constructor, which calls the add_filters).

    problem:
    – the contructor is called (ok, otherwise none of my plugins would work).
    – the private init calss is called (also like expected and evythg fine)
    – there i define the ‘apply_filters(“the_content”, array($this, “checkContent”));’
    also called as i debugged WP core and checked if this code is executed
    but the fct “checkContent” is never called

    of course i checked the template and “the_content” is used there (otherwise that could/would be the problem).

    changing fct “init “from private to public changes nothing (i would wonder, but you never know) 😉

    copying (to prevent typo-mistakes) to a proceduale style like “function.php” is working fine … but is no solution for my system.

    so after some hours of debugging (well, its sunday and why not … 🙁 ) i found the problem:

    your example uses [] to define the array. we (i teach it to all my junior developers) uses array() becauses it’s easier to read and maintain … but that may be a religion-like-discussion. using [] instead of array() will bring the code to work. using the previous WP version, also array() is working. kind of annoying, right ?

    pls allow me my two cents for the WP docs:
    as i wrote earlies, that docs are not helpful for complex projects. theylook good and have potential; for small syntax researches they are find. but all of my developers and collegues are struggeling by looking for more complex environment questions. the php manual is better (in THAT part; the layout could be copied from WP). there you’ll find proc and OOP examples and some ideas of other developers for the commands. maybe somebody from the WP core team reads this and they may overthink a bit of their documentation.

    +1 to diondesigns for keeping the ball rolling ! 🙂

    Dion

    (@diondesigns)

    I’m glad you got it working. I’m surprised you had issues with [], I suspect it could be due to your development environment.

    I use [] instead of array() because it is a standard representation for arrays in many/most other programming languages. That makes code easier for me to read and maintain since I also do a lot of work with C, Javascript, and Perl. Maybe some day PHP will allow the use of {} for indexed arrays…one can hope.

    OOP-vs-procedural is also a religion-like discussion. I use what works best for a project…playing the “hammer square pegs into round holes” game never appealed to me. I guess that makes me a programming agnostic. 🙂

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘apply_filters ‘the_content’ from class’ is closed to new replies.