为你的WordPress主题框架开发插件

山有山的沉稳厚重,一年四季却能以不同的色彩风光展示山的美丽。活泼的有单调的一面,安静的有变换的色彩。静与动的搭配,单调与精彩的结合,也就组成了最美的风景。
本文是《开发你的 WordPress 主题框架》专题的第 8 篇,共 10 篇:
  • 为你的WordPress主题框架添加动作挂钩
  • WordPress 主题框架是如何工作的
  • 决定如何开发你的WordPress主题框架
  • 为你的WordPress主题框架建立起始文件
  • 为你的WordPress主题框架添加函数
  • 为你的WordPress主题框架添加过滤挂钩
  • 为你的WordPress主题框架创建子主题
  • 为你的WordPress主题框架开发插件
  • 发布你的WordPress主题框架
  • 为你的WordPress主题框架编写文档

在上一课,我们探讨了使用主题框架中子主题创建网站的方式。这节课,让我们来看看需要创建插件的一些情况。

何时创建插件

当你想要给一个建立在框架上的网站添加功能的时候,有时会很难决定是否要使用插件或子主题中的functions.php文件。

在决定做什么之前,我都会问自己几个问题,如以下信息图表所示:

这将帮助你决定是否使用你的子或父主题函数文件、插件,或初始子主题。

如果您添加的功能增加了大量的代码,并能用于你开发的其他但非所有的网站上,那么编写一个插件一般来说是最好的主意。

创建你的插件

如果你决定需要一个插件,那么你可以利用之前添加到框架中的挂钩,使它们变得更强大。例如:

如果你的插件添加了浏览路径记录的功能,你可以将输出钩连到wptp_above_content行动挂钩上,以显示每一网页内容上的浏览路径。

如果你的插件能创建一个更强大的或相关的搜索框,你可以将它附加到wptp_in_header或wptp_sidebar行动挂钩上。

一个创建调用动作框(就像上节课中子主题中的函数)的插件,将附加到wptp_sidebar或wptp_after_content挂钩上。

这样的例子不胜枚举!

显然,也有些插件不会使用框架中的挂钩,而会通过核心WordPress挂钩激活,但是你自己的挂钩会带给你更多的选择。

实例——导航插件

其中一个实例是导航插件,我曾创建此插件并运用到了我自己的框架之中。这个插件仅能在当前页面中被激活,它首先会检查当前页面是否在层次结构中。如果当前页面有子或父页面,它就会显示其层次结构中的顶层页面和其子页面的列表,让你可以进行本地导航。

我在一些客户网站上使用过这个插件,运用其他的一些条件标签将插件附加到before_content挂钩或sidebar挂钩上,或同时附加到两个挂钩上。

该插件使用了两个函数:第一个是wptp_check_for_page_tree(),用于找到页面树中的当前页面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function wptp_check_for_page_tree() {

    //start by checking if we're on a page
    if( is_page() ) {

        global $post;

        // next check if the page has parents
        if ( $post->post_parent ) {

            // fetch the list of ancestors
            $parents = array_reverse( get_post_ancestors( $post->ID ) );

            // get the top level ancestor
            return $parents[0];

        }

        // return the id  - this will be the topmost ancestor if there is one, or the current page if not
        return $post->ID;

    }

}

function wptp_check_for_page_tree() { //start by checking if we're on a page if( is_page() ) { global $post; // next check if the page has parents if ( $post->post_parent ) { // fetch the list of ancestors $parents = array_reverse( get_post_ancestors( $post->ID ) ); // get the top level ancestor return $parents[0]; } // return the id - this will be the topmost ancestor if there is one, or the current page if not return $post->ID; } }

接下来是wptp_list_subpages(),用于检查我们是否在某个页面上(而不是在主页上),然后运行wptp_check_for_page_tree()函数,并根据其结果,输出页面列表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function wptp_list_subpages() {

    // don't run on the main blog page
    if ( is_page() && ! is_home() ) {

        // run the wptp_check_for_page_tree function to fetch top level page
        $ancestor = wptp_check_for_page_tree();

        // set the arguments for children of the ancestor page
        $args = array(
            'child_of' => $ancestor,
            'depth' => '-1',
            'title_li' => '',
        );

        // set a value for get_pages to check if it's empty
        $list_pages = get_pages( $args );

        // check if $list_pages has values
        if ( $list_pages ) {

            // open a list with the ancestor page at the top
            ?>
            <ul class="page-tree">
                <?php // list ancestor page ?>
                <li class="ancestor">
                    <a href="<?php echo get_permalink( $ancestor ); ?>"><?php echo get_the_title( $ancestor ); ?></a>
                </li>

                <?php
                // use wp_list_pages to list subpages of ancestor or current page
                wp_list_pages( $args );;

                // close the page-tree list
                ?>
            </ul>

        <?php
        }
    }

}

function wptp_list_subpages() { // don't run on the main blog page if ( is_page() && ! is_home() ) { // run the wptp_check_for_page_tree function to fetch top level page $ancestor = wptp_check_for_page_tree(); // set the arguments for children of the ancestor page $args = array( 'child_of' => $ancestor, 'depth' => '-1', 'title_li' => '', ); // set a value for get_pages to check if it's empty $list_pages = get_pages( $args ); // check if $list_pages has values if ( $list_pages ) { // open a list with the ancestor page at the top ?> <ul class="page-tree"> <?php // list ancestor page ?> <li class="ancestor"> <a rel="nofollow noopener noreferrer" href="<?php echo get_permalink( $ancestor ); ?>"><?php echo get_the_title( $ancestor ); ?></a> </li> <?php // use wp_list_pages to list subpages of ancestor or current page wp_list_pages( $args );; // close the page-tree list ?> </ul> <?php } } }

安装并激活插件后,你需要在你的子主题中激活它,添加以下内容到functions.php文件:

1
add_action( 'wptp_sidebar', 'wptp_list_subpages' );

add_action( 'wptp_sidebar', 'wptp_list_subpages' );

当然,如果你想在其他位置输出列表的话,可以使用别的动作挂钩。

小结

插件是这个生态系统的另一部分,而这个生态系统就是你已创建的那部分框架。您可以创建一些插件,并专门设计成通过一些已添加到框架中的挂钩来激活,就像我上面所展示的那样。

在编写一个插件之前,值得花费一些时间去考虑并确定这样做正确与否。如有疑问,请参阅本文前面的信息图表来帮助你下定决心。

  • 原文出自:http://code.tutsplus.com/tutorials/developing-plugins-for-your-wordpress-theme-framework--cms-21934
  • 由 stonetan@WordPress大学 原创翻译,未经允许,禁止转载和采用本译文。

本文为你的WordPress主题框架开发插件到此结束。如果你独自一人笑了,那是真心的笑。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
WordPress站点Gravatar头像前后台不显示的如何解决办法

WordPress主题需要支持https吗?WordPress站点如何如何实现https?

WordPress站点的页面/标签/分类URL地址如何添加.html?

WordPress站点更换了域名后数据库应该如何操作替换新旧域名?

WordPress安装在主机空间的什么目录里面?根目录在哪里?