Wordpress url to html


Wordpress url to html

我正在尝试以干净的方式ajaxize wordpress。这个想法是这样的:

  1. Ajax 请求是从客户端到 ajax 操作 url 发出的(例如 /wp/wp-admin/admin-ajax.php?action=my_plugin_fetch_page&page=http://example.com/about )。
  2. 该请求的响应应采用 JSON 格式,并包含最少的页面标题和页面内容(相当于 http://example.com/about )。

我无法通过只知道页面的网址来找到获取页面内容的好方法。

我试过使用wp_remote_get.所以我只是从服务器向 url http://example.com/about发出另一个请求。这有效,但它很慢(将请求减慢约 2 秒)。

我尝试使用url_to_postid,它将网址转换为post_id。不幸的是,这不适用于存档页面,无论如何都可能毫无用处,因为我需要来自 url http://example.com/about 的完整响应正文,而不仅仅是等效页面的 id。

这是它目前与wp_remote_get的工作方式:

function my_plugin_fetch_page () {
    /*
     *
     * Getting url of target page from GET parameters
     *
     */
    $url = $_GET['page'];
    $title = '';
    $content = '';
    /*
     *
     * Url must be set in request parameters and it must be internal link
     *
     */
    if ( $url && my_plugin_is_internal_page( $url ) ) {
        /*
         *
         * Format relative urls to absolute urls.
         *
         * test/example -> http://example.com/test/example
         *
         */
        $url = nolife_single_format_url( $url );
        /*
         *
         * Make GET request.
         *
         */
        $page_response = wp_remote_get( $url, array(
            'user-agent'  => 'InternalRequester'
        ) );
        if ( ! is_wp_error( $page_response ) ) {
            /*
             *
             * Response contains title and page content seperated by some
             * seperator. By splitting response from that seperator, we
             * get title and contents of page in seperate values.
             *
             */
            $content_parts = explode( MY_PLUGIN_SEPERATOR, wp_remote_retrieve_body( $page_response ) );
            // title comes first
            $title = $content_parts[0];
            // then content of page
            $content = $content_parts[1];
        }
    }
    wp_send_json( array(
        'url'      => $url,
        'title'    => $title,
        'content'  => $content
    ) );
    die;
}

谁能帮我解决这个问题?

此代码段检查字符串是否存在于任何定义的分类中,包括自定义分类:

$term = "maybe_a_term";
$taxonomies = get_taxonomies(['show_ui' => true]);
$term_exists = array_reduce($taxonomies, function($carry, $item) use ($term) {
  return $carry || get_term_by('slug', $term, $item);
}, false);

$taxonomies数组简化为布尔值,因此如果找到该术语$term_exists将为真。然后,您可以使用基于分类的查询来收集所需的帖子。

注意:争论['show_ui' => true]隐藏了内部分类法,如post_formatnav_menu

WordPress不能反向解析自己的永久链接,这太疯狂了。