1

This might be a php question more than it is a wordpress question. I am not sure though, but please guide me if so. I am working on a trial plugin that I use for my training in the first stages of plugin development.

Here is a simplified example of the structure:

class trial{

    function trial() {
        add_shortcode( 'shs_ref', array( &$this, 'shortcode' ) );
        add_filter( 'the_content', array( &$this, 'after_content' ));
    }

    /*Run the shortcode*/
    function shortcode( $atts , $content = null ) {

        if ( !isset( $this->entries ) )
            $this->entries = array();

        array_push($this->entries,$content);

        return 'What number of shortcode is this: ',count($this->entries);

    }

    /*Add after the page content*/
    function after_content( $content ) {

        foreach($this->entries as $entry) /*This one is empty?*/
           $content .= $entry;

        return $content;
    }

}

new trial();

In the shortcode function, an array $this->entries is being filled with the content $content that the user has wrapped inside the shortcode clamps. For every new occasion of the shortcode, its content is appended to the array via php's array_push. I can then do something with this growing array $this->entries (for example count its contents, as in the example).

All this is fine and works.

The issue comes when I want a filter-function to use that same array. I want a list of all the collected shortcode contents to be added at the bottom of the page. At the top of the code, I use an add_filter which passes the_content to my other function called after_content. This works and the entire page content can be handled by the function. But the $this->entries array is empty. It kept its content while being used iteratively in the shortcode function, but it loses its content when it is used in the after_content function afterwards.

I have tried setting the $this->entries array as a global variable, for instance by adding var $stored_entries outside the function and then global $stored_entries; $stored_entries = $this->entries inside the shortcode-function, but it makes no difference. The content of $this->entries as well as $stored_entries is NULL.

The question is mainly:

  • How can I pass the array on to this other function?

And secondarily:

  • Why is that array stored between iterations of the shortcode-function but forgotten when we enter the filter-function?
0

Okay, it turns out that I was missing a tiny little number in the add_filter command. The line:

add_filter( 'the_content', array( &$this, 'after_content' ));

should be:

add_filter( 'the_content', array( &$this, 'after_content' ), 12);

And now all works. The array is accessible inside the filter-function. This number is a priority variable: https://developer.wordpress.org/reference/functions/add_filter/, but if anyone will add a comment on the usefulness of it, please do so.

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.