0

I need to modify the way a Wordpress plugin (Paid Memberships Pro) displays an excerpt, which has actually become a 2 part question.

The first part: Can someone point me in the direction of info on properly modifying a plugin? Is there something similar to hooks/filters like you can use on core?

The fun part:

I want to modify this bit of code to make sure that only 1 paragraph is displayed, regardless of its length. If the paragraph is shorter than 55 characters, display the whole thing and nothing more. If more than 55 characters, display those 55 characters and nothing more.

Here's the code from the plugin:

//if show excerpts is set, return just the excerpt
    if(pmpro_getOption("showexcerpts"))
    {           
        //show excerpt
        global $post;
        if($post->post_excerpt)
        {                               
            //defined exerpt
            $content = wpautop($post->post_excerpt);
        }
        elseif(strpos($content, "<span id=\"more-" . $post->ID . "\"></span>") !== false)
        {               
            //more tag
            $pos = strpos($content, "<span id=\"more-" . $post->ID . "\"></span>");
            $content = wpautop(substr($content, 0, $pos));
        }
        elseif(strpos($content, 'class="more-link">') !== false)
        {
            //more link
            $content = preg_replace("/\<a.*class\=\"more\-link\".*\>.*\<\/a\>/", "", $content);
        }
        else
        {
            //auto generated excerpt. pulled from wp_trim_excerpt
            $content = strip_shortcodes( $content );
            $content = str_replace(']]>', ']]&gt;', $content);
            $content = strip_tags($content);
            $excerpt_length = apply_filters('excerpt_length', 55);
            $words = preg_split("/[\n\r\t ]+/", $content, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
            if ( count($words) > $excerpt_length ) {
                array_pop($words);
                $content = implode(' ', $words);
                $content = $content . "... ";
            } else {
                $content = implode(' ', $words) . "... ";
            }

            $content = wpautop($content);
        }
    }

Thanks!

4

1 回答 1

2

好吧,修改别人的插件通常是个坏主意,因为如果他们更新它,您可能会丢失您的更改或者您将无法更新(尽管发生意外)。

除了修改之外的另一个选项可能是设置跨度或 div 的最大高度或宽度。或者在页面加载时使用 JavaScript/jquery 来抓取该内容并缩短它。或者使用 wordpress 过滤器在加载后但显示之前过滤帖子内容。

我个人会在它周围找到 div 并设置最大尺寸。它简单、快速且可配置。

如果您绝对必须修改插件。我建议复制它,重命名它,然后在那里进行更改。然后,当有更新时,您可以更新、再次复制并手动添加您的更改。或者类似的东西。

您还可以查看插件使用的任何过滤器。钩子不仅仅用于核心。所有插件都应该使用它们(通常我不会使用任何没有使用的插件),并且任何使用的插件都可以使用钩子轻松修改。这就是他们的目的。这太棒了,因为这意味着 wordpress 中的任何内容都可以更改。

于 2013-04-24T06:38:26.667 回答