Modify ‘Read more’ link adding a new class

What (exactly) happens

When calling the_content() inside your template, you are able to call it without any parameters. This means, that the function already has the defaults of null for both arguments: The “more” link text and the boolean switch that allows you to strip teaser content before the “more” link text.

The the_content() function calls get_the_content() internally (and passes the arguments in). It then runs all callback functions that are attached to the the_content-filter. So basically nothing that is related to the link or anything else (aside from the filter) happens inside the_content() – it happens inside get_the_content().

And there we got the the_content_more_link-filter.

The filter in core

apply_filters( 
    'the_content_more_link',
    ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>",
    $more_link_text
);

Filters in action

The two arguments after the filter name (1st arg), are the accessible arguments inside attached callback functions.

// Example: Somewhere in core, a plugin or a theme
$output = apply_filters( 'some_filter', $arg_one, $arg_two );

// Example: Somewhere in a custom plugin
// Attach the filter: Attach filter name, callback function name, priority, number of taken args
add_filter( 'some_filter', 'wpse63748_callback_function', 10, 2 );
function wpse63748_callback_function( $arg_one, $arg_two )
{
    // Do something with Arg One = modify the output
    // You have Arg two to help you

    // Always return the first arg for filter callback functions.
    return $arg_one;
}

Modify the »more«-link

Now the actual/real-life plugin:

/** Plugin Name: (#63748) Modify the »more«-link. */
add_action( 'the_content_more_link', 'wpse63748_add_morelink_class', 10, 2 );
function wpse63748_add_morelink_class( $link, $text )
{
    return str_replace(
        'more-link',
        'more-link CUSTOM_CLASSES_HERE',
        $link
    );
}

Simply add your classes where you read CUSTOM_CLASSES_HERE and then upload it to your plugins directory or remove the comment Plugin Name: ... and use it in your themes functions.php file.

Leave a Comment