0
votes

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!

1

1 Answers

2
votes

Well, modifying somebody else's plugin is usually a bad idea, because if they update it you could lose your changes or you wouldn't be able to update (although accidents happen).

Another option than modifying could be setting the max height or width of the span or div. Or using JavaScript/jquery when the page is loading to grab that content and shorten it. Or using a wordpress filter to filter the post content after it's been loaded but before it shows up.

I personally would find the div around it and set max dimensions. It's easy, fast, and configurable.

If you absolutely have to modify the plugin. I would suggest copying it, renaming it, and making your changes there. Then, when there is an update, you can update, copy again, and manually add back your changes. Or something along this lines.

You can also look into any filters the plugin uses. Hooks are not just used in the core. All plugins should use them (I wouldn't use any that didn't, typically), and any plugin that does can be easily modified using hooks. That is what they are there for. Which is awesome because that means anything in wordpress can be changed.