0

I'm writing a plugin to add some extra contents that contain the permanent link to the (single) post.

function abcd_add_contents($content) {
    $extra_content = the_permalink();

    if (is_single()) {
         $content .= $extra_content;
    }
    return $content;
}

add_filter('the_content', 'abcd_add_contents');

the_permalink() does not work. How can I obtain the permanent link information of the current post in this plugin function?

0

2 Answers 2

3

globalize $post to get the current post's data. also, you want get_permalink, which returns the permalink, rather than the_permalink, which directly echoes the permalink.

function abcd_add_contents($content) {

    if (is_single()) {
        global $post;
        $extra_content = get_permalink( $post->ID );
        $content .= $extra_content;
    }
    return $content;
}

add_filter('the_content', 'abcd_add_contents');
1

the_permalink function echoes the post permalink. What you want to use is get_permalink function, which returns post permalink for use as variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.