dpf7891 2014-12-05 12:32
浏览 33

我需要在Wordpress中使用PHP在文件之间传递变量

I have created two plugins. Plugin A gets the latest posts, all of them. It has an option that can remove posts by entering their ID, this is accessible via the shortcode or directly in functions.php. This works.

Plugin B gets the most popular posts using an internal analytics systems. I have a variable called $most_popular_ids which contains all of the post ID's of the highest viewed posts for that particular category. So $most_popular_ids changes depending on which page it is on. This works.

These are displayed side by side on the main pages of the website. So ideally I would like them to not show duplicate posts.

What I finally need to do is pass $most_popular_ids from plugin B to plugin A or to functions.php. This will allow me to exclude all of the most popular posts from the latest posts.

Obviously making the variable global only works in the scope of the file so that won't work. I tried creating a $_SESSION but you can't do that in Wordpress as far as I know. Most of the stuff in the plugins can't be redeclared so my include attempts didn't work either :\

Can anyone help me?

  • 写回答

2条回答 默认 最新

  • doujiufutaog59220 2014-12-05 12:49
    关注

    You can use sessions in WordPress, but it would probably be easier to use the wp_cache_set() and wp_cache_get() functions

    For example, in Plugin B you would do something like:

    wp_cache_set('mykey_most_popular_ids', $most_popular_ids);
    

    Then in Plugin A or your functions file you can do:

    $most_popular_ids = wp_cache_get('mykey_most_popular_ids);
    

    Obviously you'd need to make sure the function in Plugin B sets the cache entry before trying to get it in Plugin A of the functions file

    评论

报告相同问题?