Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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');
share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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