plugins - Passing an array from shortcode-function to filter-function

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

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?

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?
Share Improve this question edited Sep 18, 2019 at 6:43 Steeven asked Sep 18, 2019 at 6:37 SteevenSteeven 14511 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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/reference/functions/add_filter/, but if anyone will add a comment on the usefulness of it, please do so.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745148331a4613749.html

相关推荐

  • plugins - Passing an array from shortcode-function to filter-function

    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

    1小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信