0

Recently I need Help regarding this permalink shortcode

function my_permalink(){
ob_start();
the_permalink();
return ob_get_flush();}

add_shortcode('my_permalink','my_permalink');

for this, I get a shortcode [my_permalink], it's working fine

But the problem is when I use this shortcode on my WordPress Posts, Pages, CPT, CT or any custom Archive Page it shows me some unnecessary link text which is at top of the loop and also it does not include any HTML tag

I am showing the screenshot and the link appearing position in the posts

Code:

    </div>
        <div class="entry-content">
            https://careerfeed.net/jobs/kfri-recruitment-2021-for-1-project-assistant/<div class="wprt-container"><p>KFRI Recruitment 2021 for 1 Project Assistant Applications are invited from eligible candidates for the post of one <b>Project Assistant </b>under the project KFRI/RP 823/2021 entitled “Ecological studies on post restoration success of threatened plants in situ.&#8221;</p>
https://careerfeed.net/jobs/kfri-recruitment-2021-for-1-project-assistant/
<p>&nbsp;</p>

Screenshot: The URL appears:

https://prnt.sc/15jcg6d

I just paste the shortcode:

https://prnt.sc/15jckza

Inside inspect Elements:

https://prnt.sc/15jct1q

1
  • 1
    You know you could just use add_shortcode( 'my_permalink', 'get_the_permalink' ); Since there's already a function for returning the permalink. Commented Jun 16, 2021 at 6:43

1 Answer 1

0

ob_get_flush doesn't do what you think it does. You're assuming that it only ends the output buffer and returns its contents, which is incorrect, it also flushes the output buffer to the browser.

So this:

return ob_get_flush();

Is the same as this:

$content = ob_get_clean();
echo $content;
return $content;

Use ob_get_clean(); instead, which erases the output buffer instead of flushing it.

6
  • I think i understood what's actually wrong with my code
    – GOST
    Commented Jun 15, 2021 at 23:37
  • I don't understand the last comment you left, can you clarify?
    – Tom J Nowell
    Commented Jun 15, 2021 at 23:44
  • it's nothing , i says i was wrong with ob_get_flush(); which flushes the output buffer inside the buffer, and now it's ok I replace ob_get_flush() with ob_get_clean();
    – GOST
    Commented Jun 17, 2021 at 0:17
  • I see, if this answers your question could you mark it as the accepted answer?
    – Tom J Nowell
    Commented Jun 17, 2021 at 9:45
  • Yes Sir I accept this answer, your answer was 100% current , and also make my doubt clear
    – GOST
    Commented Jun 18, 2021 at 10:53

Your Answer

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

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