WordPress 页面加载中我可以检测到所请求的帖子的最早点是什么?

发布于 2024-11-07 02:36:03 字数 454 浏览 0 评论 0原文

最早的 WordPress 操作是什么,可以可靠地检测到最终显示的帖子? (通过使用全局 $post 或检测 wp_query 对象或其他方式)

例如。我的插件需要检测来自不同站点上另一个插件的传入请求,目前它通过使用 add_action('plugins_loaded' 尽早检查 $_POST var,并且回调函数使用 < code>$post = get_page_by_path($_SERVER['REQUEST_URI'],'','post') 获取 $post 对象,然后使用 post 数据获取用于处理响应的任何其他信息在任何标头或其他标准 WP 处理发生之前发送回来,目的是减轻接收请求的博客的负载,

有更好的方法吗?我知道没有更早的方法,因为 'plugins_loaded'。 操作在插件加载后立即被调用,但是有没有比使用 get_page_by_path 更可靠的方法?

what is the earliest wordpress action where the post that will eventually be shown can be detected reliably ? (either by using global $post or detecting the wp_query object or other way)

eg. my plugin needs to detect an incoming request from another plugin on a different site, at the moment it checks for a $_POST var as early as possible by using add_action('plugins_loaded' and the callback function uses $post = get_page_by_path($_SERVER['REQUEST_URI'],'','post') to get the $post object and then uses the post data to get any other information that is used to process the response which gets sent back before any headers or other standard WP processing happens with the intention of lessening the load on the blog receiving the request.

is there a better way?, I know there isn't an earlier way because 'plugins_loaded' action gets called right after the , well, plugins get loaded but is there a more reliable way than using get_page_by_path ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦亿 2024-11-14 02:36:03

我会尝试过滤器'the_posts'。您可以在 wp-includes/query.php 函数 get_posts() 中找到它。它将找到的帖子作为数组通过引用传递,因此您可以像操作一样使用它。

这是我用来检查钩子的插件:

<?php
/*
Plugin Name: Hook Check
Description: Inspects a hook and prints its information to the footer.
Version:     1.0
Required:    3.1
Author:      Thomas Scholz
Author URI:  http://toscho.de
License:     GPL
*/
! defined( 'ABSPATH' ) and exit;

$GLOBALS['hook_checks'] = apply_filters(
    'hook_check_filter'
,   array ( 'the_posts' )
);

foreach ( $GLOBALS['hook_checks'] as $hc_hook )
{
    add_action( $hc_hook, array( 'Hook_Check', 'catch_info' ) );
}
add_action( 'wp_footer', array( 'Hook_Check', 'print_info' ) );

class Hook_Check
{
    static $info = array ();

    public static function catch_info()
    {
        $args = func_get_args();
        self::$info[ current_filter() ] = print_r( $args, TRUE );
        return $args[0];
    }

    public static function print_info()
    {
        if ( empty ( self::$info ) )
        {
            return;
        }

        print '<pre>';
        foreach ( self::$info as $filter => $catched )
        {
            print "<b>$filter</b>\n" . htmlspecialchars( $catched );
        }
        print '</pre>';
    }
}

减少样本输出:

Array
(
    [0] => Array
        (
            [0] => stdClass Object
                (
                    [ID] => 112
                    [post_content] => The entire content …
                    [post_title] => An awesome title
                    [post_excerpt] => 
                    [post_status] => publish
                )
        )
)

这应该尽早为您提供所需的所有信息。

哦,我希望在 https://wordpress.stackexchange.com/ 上见到您提出此类问题。 :)

I’d try the filter 'the_posts'. You find it in wp-includes/query.php function get_posts(). It passes the found posts as a array by reference, so you can use it like an action.

Here is a plugin I use to inspect hooks:

<?php
/*
Plugin Name: Hook Check
Description: Inspects a hook and prints its information to the footer.
Version:     1.0
Required:    3.1
Author:      Thomas Scholz
Author URI:  http://toscho.de
License:     GPL
*/
! defined( 'ABSPATH' ) and exit;

$GLOBALS['hook_checks'] = apply_filters(
    'hook_check_filter'
,   array ( 'the_posts' )
);

foreach ( $GLOBALS['hook_checks'] as $hc_hook )
{
    add_action( $hc_hook, array( 'Hook_Check', 'catch_info' ) );
}
add_action( 'wp_footer', array( 'Hook_Check', 'print_info' ) );

class Hook_Check
{
    static $info = array ();

    public static function catch_info()
    {
        $args = func_get_args();
        self::$info[ current_filter() ] = print_r( $args, TRUE );
        return $args[0];
    }

    public static function print_info()
    {
        if ( empty ( self::$info ) )
        {
            return;
        }

        print '<pre>';
        foreach ( self::$info as $filter => $catched )
        {
            print "<b>$filter</b>\n" . htmlspecialchars( $catched );
        }
        print '</pre>';
    }
}

Reduced sample Output:

Array
(
    [0] => Array
        (
            [0] => stdClass Object
                (
                    [ID] => 112
                    [post_content] => The entire content …
                    [post_title] => An awesome title
                    [post_excerpt] => 
                    [post_status] => publish
                )
        )
)

This should give you all information you need as early as possible.

Oh, and I hope to see you on https://wordpress.stackexchange.com/ with such questions. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文