首页 >CMS教程 >WordPress >定制的WordPress管理过滤器

定制的WordPress管理过滤器

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌原创
2025-02-18 11:11:091025浏览

>本文说明了如何使用WordPress过滤器自定义邮政管理屏幕。 我们将介绍WordPress的内置过滤器,并向您展示如何创建自定义过滤器以增强功能。

关键概念

  • 使用
  • 挂钩添加新的过滤器。这些允许通过各种标准进行过滤柱。restrict_manage_posts pre_get_posts>可以通过将代码添加到主题的
  • >文件(或插件)来创建邮政格式和作者的自定义过滤器。
  • > functions.php
  • 过滤器修改数据库查询,控制显示哪些帖子。
  • 自定义过滤器改善用户体验,添加功能并维护干净的代码。> pre_get_posts
  • >过滤帖子:基础知识
  • WordPress在邮政管理屏幕上提供默认过滤器(如日期过滤,如下所示)。 主题和插件可以添加更多。 这些过滤器根据指定的标准缩小了帖子列表。>

    创建自定义过滤器

    Customized WordPress Administration Filters > WordPress提供内置过滤器时,您通常需要自定义的过滤器才能获得更好的用户体验。 两个钩子是关键:

    :将新的过滤器控件添加到管理员屏幕的顶部。

      :在查询运行之前修改查询,过滤显示的帖子。>
    • restrict_manage_posts>示例:作者和邮政格式过滤
    • >让我们为邮政作者和格式创建过滤器。 想象一个网站,帖子手动分配了格式和作者。默认的管理屏幕可能是压倒性的。 我们将添加下拉菜单以易于过滤。pre_get_posts>
    • 添加下拉菜单(

    >将以下代码添加到您的主题的

    >文件(或插件):>

    >由作者过滤:

    restrict_manage_posts

    >按邮政格式过滤:

    functions.php

    这将两个下拉列表添加到“邮政列表”屏幕。

    >

    <code class="language-php">function add_author_filter() {
        global $post_type;
        if ($post_type == 'post') {
            $user_args = array(
                'show_option_all' => 'All Authors',
                'orderby' => 'display_name',
                'order' => 'ASC',
                'name' => 'author_filter',
                'who' => 'authors',
                'include_selected' => true
            );
            if (isset($_GET['author_filter'])) {
                $user_args['selected'] = (int) sanitize_text_field($_GET['author_filter']);
            }
            wp_dropdown_users($user_args);
        }
    }
    add_action('restrict_manage_posts', 'add_author_filter');</code>

    >过滤帖子列表(
    <code class="language-php">function add_post_format_filter() {
        global $post_type;
        if ($post_type == 'post') {
            $post_formats_args = array(
                'show_option_all' => 'All Formats',
                'orderby' => 'NAME',
                'order' => 'ASC',
                'name' => 'post_format_filter',
                'taxonomy' => 'post_format'
            );
            if (isset($_GET['post_format_filter'])) {
                $post_formats_args['selected'] = sanitize_text_field($_GET['post_format_filter']);
            }
            wp_dropdown_categories($post_formats_args);
        }
    }
    add_action('restrict_manage_posts', 'add_post_format_filter');</code>

    现在,让我们使下拉列表功能:

    Customized WordPress Administration Filters >作者过滤:

    pre_get_posts>通过邮政格式过滤:

    这些函数将查询修改为仅包含匹配所选作者或邮政格式的帖子。

    <code class="language-php">function filter_posts_by_author($query) {
        global $post_type, $pagenow;
        if ($pagenow == 'edit.php' && $post_type == 'post' && isset($_GET['author_filter'])) {
            $author_id = sanitize_text_field($_GET['author_filter']);
            if ($author_id != 0) {
                $query->set('author', $author_id);
            }
        }
    }
    add_action('pre_get_posts', 'filter_posts_by_author');</code>

    结论

    这通过自定义过滤器增强了您的WordPress管理员。 您可以通过其他帖子属性(请参阅WordPress查询类文档)进行过滤。 请记住要始终对用户输入进行消毒以防止安全漏洞。

    >

    以上是定制的WordPress管理过滤器的详细内容。更多信息请关注PHP中文网其他相关文章!

    声明:
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn